SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru augathra's Avatar
    Join Date
    Jul 2004
    Location
    united states
    Posts
    826
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    For every 4, a new <tr>?

    Hello, I am trying to get 16 images to display in 4 columns, 4 rows.
    I am wondering how I can get a new <tr> every 4 <td>'s.

    Somewhat like this:
    PHP Code:
    <?php
    // connect to db
    $SQL "SELECT * FROM something WHERE field1 = '3' LIMIT 16"/* maybe 20 rows in the table */
    $results mysql_query($SQL) or die("error");
    while(
    $n mysql_fetch_array($results)) {
    // then somehow i get a tr, 4 cells...then another tr & 4 more cells
    // i would do this 4 times, for a total of 16
    }
    ?>
    If more info is needed, ill try my best to provide it. I know I could just make 4 columns & rows in HTML, then echo the contents in each cell, but that's not my goal =).

    Thanks

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    New Jersey
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    // connect to db
    $SQL "SELECT * FROM something WHERE field1 = '3' LIMIT 16"

    $results mysql_query($SQL) or die("error");

    $i=0;
    while(
    $n mysql_fetch_array($results)) 
    {
         
          if (
    $i == 0)  // true for first cell
                  
    echo "<tr>
                            <td>
    $n[..]</td>";

         else if (
    $i == 0)  // will reach here when $i == 4,8,12,16
                  
    echo "</tr>
                         <tr>
                              <td>
    $n[..]</td>";    
            
          else
                   echo  
    "<td>$n[..]></td>";
         
          
    $i++;
    }
    ?>

  3. #3
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do what rushabh said but after the while loop closes:
    PHP Code:
    echo "</tr>"

  4. #4
    SitePoint Guru augathra's Avatar
    Join Date
    Jul 2004
    Location
    united states
    Posts
    826
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, little bit of editing and works great.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •