SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    phpLD Fanatic bronze trophy dvduval's Avatar
    Join Date
    Mar 2002
    Location
    Silicon Valley
    Posts
    3,626
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Displaying Query Results in Columns the Rows

    I'm trying to figure out how to display an unknown quantity of products where 3 products are diplayed per row until all products are displayed. I have no problem diplaying one per row, but telling PHP to display 3 and the n go to the next row is somethong I can't quite fathom. Here is an example of what I'm hoping to diplay:
    Code:
    <table width="100%" border="0">
      <tr>
    	<td>Product 1</td>
    	<td>Product 2</td>
    	<td>Product 3</td>
      </tr>
      <tr>
    	<td>Product 4</td>
    	<td>Product 5</td>
    	<td>Product 6</td>
      </tr>
      <tr>
    	<td>Product 7</td>
    	<td>Product 8</td>
    	<td>Product 9</td>
      </tr>
    </table>

  2. #2
    ********* Janitor levik's Avatar
    Join Date
    Jan 2003
    Location
    New York
    Posts
    885
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    Code:
     . . .
    $column = 0;
    while ($product = mysql_fetch_array($products)) {
         if ($column == 0) { echo("<tr>"); }
         echo ("<td>$product</td>");
         $column++;
         if ($column >= 3) {
             $column = 0;
             echo("</tr>");
         }
    }
    if ($column != 0) { echo("</tr>"); }
    . . .
    This keeps count of which column you're currently on, and only opens/closes the row when you reach column 3.


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
  •