Thread: Loop question

Page 2 of 2 First 12
  • Jump to page:
    #16
  1. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    Originally Posted by maineman
    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.
    Alright this may sound obvious but before you jump in to php, it mind be wise to first learn html.
    php outputs html to your browser, but if your not able to format html correctly in the first place you are trying to learn to walk and run at the same time.

    Your code is missing (apart from a possible better loop (foreach-loop) 2 endtags.

    The a </tr> and a </table>

    As linked in the previous comment pretty much html elements start with a start tag (like <tr> <p> <div> etc) and end with an and tag (like </tr> </p> </div> etc).

    Try this out:
    PHP Code:
    <?php

    // 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>';
    echo 
    '<tbody>';
    echo 
    '<tr>';
    $names 0;
    while (
    $names 11) {
        echo 
    "<td>$zodiacNames[$names]</td>";
        
    $names++;
    }
    echo 
    '</tr>'// end tag of the row
    echo '</tbody></table>'// end tage of the table
    ?>
    a more optimal way would be to use a foreach loop btw.
    PHP Code:
    $zodiacNames = array(
        
    "Rat""Ox""Tiger""Rabbit""Dragon""Snake""Horse""Ram""Monkey""Rooster""Dog""Pig");
    // end loading the names array
    echo '<table>';
    echo 
    '<tbody>';
    echo 
    '<tr>';

    foreach(
    $zodiacNames as $name) {
        echo 
    "<td>$name</td>";
    }
    echo 
    '</tr>'// end tag of the row
    echo '</tbody></table>'// end tage of the table
    ?> 
    I am just wondering why You use a table and not a list

    Comments on this post

    • maineman agrees
    Last edited by aeternus; September 29th, 2012 at 08:08 PM.
  2. #17
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    6
    well first of all, im taking this course by not being in a class, basically on line. i agree html would have been a better start but right now it is what it is.

    I do have those tags. I probably should have posted the entire code instead of just a snippet. I'll do that now.

    As far as the loop is concerned we are supposed to do this as a for loop and a while loop.

    here is the whole code. thanks for hanging in with me and also your patience.

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title></title>
    <meta http-equiv="content-type"
    content="text/html; charset=iso-8859-1" />
    </head>
    <body>
    <p>
    <?php
    // loads the images into an array

    $zodiacPics = array(
    "Images/rat.gif",
    "Images/ox.gif",
    "Images/tiger.gif",
    "Images/rabbit.gif",
    "Images/dragon.gif",
    "Images/snake.gif",
    "Images/horse.gif",
    "Images/ram.gif",
    "Images/monkey.gif",
    "Images/rooster.gif",
    "Images/dog.gif",
    "Images/pig.gif");

    // 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++;
          }
    echo
    "</tr>";      

    $img 0;

    echo 
    "<tr>";
    // loads the images onto the page from the array using a while loop
    while ($img 12 ) {
      echo 
    "<td><img src=$zodiacPics[$img] height='50px' width='50px' /></td>";
      
    $img++;
      }
      
    // end loading of images array from the while loop
    echo "</tr>";
    echo 
    "<tr>";
    $Columns 0;        //counter to track the number of columns across which will be 12
    $StartYear 1912;    //start table w/ a year of the rat to coinside w/ headings
    $EndYear 2012;    //current year
    while ($StartYear <= $EndYear){ //add years to table including current year
       
    echo "<td align='center'>$StartYear</td>";
       ++
    $Columns;    //increases the number of columns displayed by 1
       
    ++$StartYear;        //increases the year displayed by 1
       
    if ($Columns == 12){ //once you have displayed 12 columns (or td tags) start a new row
          
    echo "</tr><tr>";
          
    $Columns =0;    //reset the columns to 0 because you need to count 12 again
       
    //end if
    //end while

    echo "</table>\n";
    ?>
    </p>
    </body>
    </html>
  4. #18
  5. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    <$zodiacNames[$names]>

    This probably is giving troubles ( the < and the > ) Its quite likely that your browser interprets them as if they were tags.
    if you use < and > for aesthetic purposes use:
    PHP Code:
      &gt
    (greater than)
    PHP Code:
     &lt
    (less than)

    AH great I can't show them, due to the browser interpreting them lol

    have a look here it should look like & gt; & lt; but without a space between the & sign. Called html entities &gt;

    I am off now, Just leave those < > out and it works if you want an easy fix
    Last edited by aeternus; September 29th, 2012 at 08:28 PM.
  6. #19
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    6
    thanks that fixed it.
  8. #20
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    48
    Rep Power
    3
    Hi friend,

    Good to see you sticking with it to learn php/html. As aeternus mentioned a few posts ago, there are some better (both more efficient and make more sense) way's of doing loops than you are doing it. Check it out. You are doing a loop with a while loop, this way:

    PHP Code:
    <?php 

    // Load the zodiac names 
    $zodiacNames = array( "Rat""Ox""Tiger""Rabbit""Dragon""Snake",
        
    "Horse""Ram""Monkey""Rooster""Dog""Pig"); 

    echo 
    '<table>'
    echo 
    '<tbody>'
    echo 
    '<tr>'

    // This is the main row I'm talking about. Your loop. A while loop.
    $names 0
    while (
    $names 11) { 
        echo 
    "<td>$zodiacNames[$names]</td>"
        
    $names++; 


    echo 
    '</tr>'// end tag of the row 
    echo '</tbody></table>'// end tag of the table 
    ?>

    There are (at least) two better ways to do this.

    The "for" loop.
    This loop is extremely common in any modern programming language. Rather than having to define your index variable before you start the loop (as you did in your code, with $names), you can define it right in the for loop declaration.
    PHP Code:
    <?php 

    // Load the zodiac names 
    $zodiacNames = array( "Rat""Ox""Tiger""Rabbit""Dragon""Snake",
        
    "Horse""Ram""Monkey""Rooster""Dog""Pig"); 

    echo 
    '<table>'
    echo 
    '<tbody>'
    echo 
    '<tr>'

    // There are three parts to this. First, you declare the index variable and its value.
    // Next, you set the condition. Third, you tell it to increment.
    // Of course, if you wanted the initial value could be 100, the condition could be < 1435 and
    // The incrementation could be $i += 183. Or whatever you want! There is great flexibility.
    // Check out this link: http://php.net/manual/en/control-structures.for.php
    for ($i 0$i 11$i++) { 
        echo 
    "<td>$zodiacNames[$i]</td>"


    echo 
    '</tr>'// end tag of the row 
    echo '</tbody></table>'// end tag of the table 
    ?>

    The foreach loop
    This is actually what you should be using--and anytime you have non incremental/numerical data arrays. Check out the syntax, as aeternus has already shown:

    PHP Code:

    // Load the zodiac names 
    $zodiacNames = array( "Rat""Ox""Tiger""Rabbit""Dragon""Snake",
        
    "Horse""Ram""Monkey""Rooster""Dog""Pig"); 

    echo 
    '<table>'
    echo 
    '<tbody>'
    echo 
    '<tr>'

    // While this may be a little confusing at first, it's actually the simplest of them all.
    // A foreach will automatically cycle through all of the elements of your array, so
    // You don't even need to tell it how many elements there are. All you need is to tell
    // it what array to use ($zodiacNames) and what variable to use to refer to the current
    // element that it is looking at ($name). You can use any variable name you want for
    // the second part ($name could be $myname or $yourname or $coolbeans).
    // Check out this link: http://php.net/manual/en/control-structures.foreach.php
    foreach($zodiacNames as $name) { 
        echo 
    "<td>$name</td>"
    }

    // Just look at how much space that saves!!! ^

    echo '</tr>'// end tag of the row 
    echo '</tbody></table>'// end tag of the table 
    ?> 

    Comments on this post

    • aeternus agrees : good work teaching him all the loops :)
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo