Thread: Loop question

Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    6

    Loop question


    I need this loop to display the $Year 12 across the page instead of straight down the page as it does now. What am I missing? Thanks in advance for any assistance.


    PHP Code:
    echo "<tr>";

    $space 1;
    $Count 1;
    $Year 1912;
     while (
    $Year<=2019) {
                    echo 
    '<th>';
                    echo 
    "$Year";
                    echo 
    '</th>';
                    if (
    $Count>11) {   /*Evaluates the number of years/columns for each row*/
                        
    echo '</tr>';
                        
    $Count=1;
                        echo 
    '<tr>';
                    }  
    // End of if conditional
                    
    else ++$Count;
                         ++
    $Year;
         echo 
    "</tr>"
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,989
    Rep Power
    375
    you should have a brace } before the final echo </tr>;
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    6
    I tried the below and get a syntax error:

    PHP Code:
    echo "<tr>";

    $space 1;
    $Count 1;
    $Year 1912;
     while (
    $Year<=2019) {
                    echo 
    '<th>';
                    echo 
    "$Year";
                    echo 
    '</th>';
                    if (
    $Count>11) {   /*Evaluates the number of years/columns for each row*/
                        
    echo '</tr>';
                        
    $Count=1;
                        echo 
    '<tr>';
                    }  
    // End of if conditional
                    
    else ++$Count;
                         ++
    $Year;}
         echo 
    "</tr>"
  6. #4
  7. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    HI your code has no syntax errors. Just make sure you surround it with the php tags. (also if you get errors it tells you in a more precise manner on what line)

    PHP Code:
    <?php

    echo "<tr>";

    $space 1;
    $Count 1;
    $Year 1912;
    while (
    $Year <= 2019) {
        echo 
    '<th>';
        echo 
    "$Year"// <--- these quotes are not needed, but do no harm
        
    echo '</th>';
        if (
    $Count 11) { /* Evaluates the number of years/columns for each row */
            
    echo '</tr>';
            
    $Count 1;
            echo 
    '<tr>';
        }  
    // End of if conditional
        
    else
            ++
    $Count;
        ++
    $Year;
    }
    echo 
    "</tr>";
    ?>
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    6
    ok i've got this. its still a loop question so i didnt make a new post.

    in the following when I execute it, i get this:

    Rat /> Ox /> Tiger /> Rabbit />

    When I just wnt the name from the array. whats missing?

    PHP Code:
    // Begin loading zodiac characters names
    $zodiacNames = array(
    "Rat""Ox""Tiger""Rabbit""Dragon""Snake""Horse""Ram""Monkey""Rooster""Dog""Pig");
    // End loading the names array
    echo "<table>";
    for (
    $names 0$names 12; ++$names) {
         echo 
    "<td>$zodiacNames[$names] /></td>";
         } 
    Thanks for your support. BTW, this is just a snippet of the whole code.
  10. #6
  11. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    look closely at your code mate:

    what is the difference between:
    Code:
    echo "<td>$zodiacNames[$names] /></td>";
    and
    Code:
    echo "<td>$zodiacNames[$names]</td>";

    Careful with those rabbits and tigers btw, they tend to not mix very well
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    6
    thanks got it
  14. #8
  15. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    Originally Posted by maineman
    thanks got it
    NIce!
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    6
    Im getting an undefined variable in my echo statement
    PHP Code:
    // load the zodiac names
    $zodiacNames = array(
    "Rat""Ox""Tiger""Rabbit""Dragon""Snake""Horse""Ram""Monkey""Rooster""Dog""Pig");
    // end loading the names array
    echo "<table>";
    $names "";
    while (
    $names 12) {
    [
    COLOR=Red]echo "<td><$zodiacNames[$names]></td>";[/COLOR]
          
    $names++;
          } 
    i dont see it.
  18. #10
  19. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    Hi mate,

    this undefined variable-error, is probably more specific. Next time post the complete error.

    I can also really recommend that you use a decent editor to write your php in. There are some errors in it that would show up directly in pretty much all decent editors out there.
    Install (for instance) Netbeans and use that.

    The color tags () are html elements and should be echoed.
    PHP Code:
    echo 
    PHP Code:
    '; 
    All HTML should be echoed.

    But there is something very wrong with those [color] tags
    Read this: http://webdesign.about.com/od/htmltags/a/bltags_deprctag.htm
    Burn the book that thought you this stuff, or never visit the website that thought you this. You are learning it the wrong way. Separate functionality, content and styling
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    6
    was trying to highlight the line in question in red to make it stand out, so i used the forums color changer and that is what it inserted, not me.

    the error $number not being defined as 0. Now the loop runs just doesn't output the names.
  22. #12
  23. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    Originally Posted by maineman
    was trying to highlight the line in question in red to make it stand out, so i used the forums color changer and that is what it inserted, not me.

    the error $number not being defined as 0. Now the loop runs just doesn't output the names.
    AH that explains the use of that stuff, but that is not needed it can only confuse.

    Well what is wrong is the index of names.instead of
    PHP Code:
     $names ""
    make it
    PHP Code:
    $names 0// index would be a better name 
    Just keep in mind that code wont make anything visible because a <td></td> should be inside of a <tr></tr>
    Last edited by aeternus; September 29th, 2012 at 07:32 PM.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    6
    i did that and it outputs nothing. here is the currect code.
    PHP Code:
    // load the zodiac names
    $zodiacNames = array(
    "Rat""Ox""Tiger""Rabbit""Dragon""Snake""Horse""Ram""Monkey""Rooster""Dog""Pig");
    // end loading the names array
    echo "<table>";

    $names 0;
    echo 
    "<tr>";
    while (
    $names 12) {
    echo 
    "<td><$zodiacNames[$names]></td>";
          
    $names++;
          } 
  26. #14
  27. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    Originally Posted by maineman
    i did that and it outputs nothing. here is the currect code.
    PHP Code:
    // load the zodiac names
    $zodiacNames = array(
    "Rat""Ox""Tiger""Rabbit""Dragon""Snake""Horse""Ram""Monkey""Rooster""Dog""Pig");
    // end loading the names array
    echo "<table>";

    $names 0;
    echo 
    "<tr>";
    while (
    $names 12) {
    echo 
    "<td><$zodiacNames[$names]></td>";
          
    $names++;
          } 
    Mate do you look at your own code or are you just posting stuff and hoping for someone else to think. Look WHat is missing.! (hint 2 endtags which ones are they)
    P.s. if you dont see stuf in your browser, right click, view source and have a look.
    Last edited by aeternus; September 29th, 2012 at 07:39 PM.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    6
    honestly i do not see it. just started trying to learn and havent much experience lately with tags. I know but i am not just throwing code out there and asking for someone to fix it.

    if its 2 end tags I am at a loss. Sorry.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo