Display results 3 columns wide?

Discussion in 'PHP' started by mokimofiki, Jul 16, 2009.

  1. #1
    Currently I am searching a database for carpets and I can list them each on their own row but am not sure how to list them in 3 columns.

    Here is my code:

    include('connect.php');
    
    $result = mysql_query("SELECT * FROM standarddecor WHERE decortype = 'Carpet' && standard='yes'");
    
    while($row = mysql_fetch_array($result))
      {
      echo "<tr><td><img src=\"standarddecor/" . $row['imgname'] . "\" alt=\"" . $row['decorname'] . "\"><br />" . $row['decorname'] . "</td></tr>";
      }
    Code (markup):
    Any help would be greatly appriciated. Thank you in advance :)
     
    mokimofiki, Jul 16, 2009 IP
  2. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #2
    instead of it showing up:

    item1
    item2
    item3
    item4

    i need:

    item1 | item2 | item3
    item4

    Please help
     
    mokimofiki, Jul 16, 2009 IP
  3. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #3
    should work:

    include('connect.php');
    
    $result = mysql_query("SELECT * FROM standarddecor WHERE decortype = 'Carpet' && standard='yes'");
    $i = 1;
    while($row = mysql_fetch_array($result))
      {
    
    if($i == 1) { echo "<tr>"; }
    
      echo "<td><img src=\"standarddecor/" . $row['imgname'] . "\" alt=\"" . $row['decorname'] . "\"><br />" . $row['decorname'] . "</td>";
    
    if($i == 3) { echo "</tr>"; $i = 0; }
    $i++;
      }
    PHP:
     
    shallowink, Jul 16, 2009 IP
  4. zeronese

    zeronese Peon

    Messages:
    83
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    you should add a small peice after the while loop to check if the last row is closed, if for example we have 5 rows, the last row will not terminate.
    after the while loop add:
    if($i < 3)
    echo "</tr>";
    PHP:
     
    zeronese, Jul 16, 2009 IP
  5. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #5
    You need to use the modulus operator. The above example will only work if you have 3 rows total.

    
    $result = mysql_query("SELECT * FROM standarddecor WHERE decortype = 'Carpet' && standard='yes'");
    $total_rows = mysql_num_rows($result);
    $count = 1;
    while($row = mysql_fetch_array($result))
      {
    
        if($count==1 || $count%3=1)
        {
        	echo '<tr>';
        }
      	
        echo "<td><img src=\"standarddecor/" . $row['imgname'] . "\" alt=\"" . $row['decorname'] . "\"><br />" . $row['decorname'] . "</td>";
        
        if($count == $total_rows || $count%3 == 0)
        {
        	echo '</tr>';
        }
    
    $count++;
      }
    
    PHP:
     
    jestep, Jul 16, 2009 IP
  6. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #6
    if($count == 1 || $count%3 == 1)
    PHP:
     
    shallowink, Jul 16, 2009 IP
  7. zeronese

    zeronese Peon

    Messages:
    83
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Note: i used the statement:
    if($i < 3)echo "</tr>";
    becuase they are resetting the $i to 0 everytime.
    it will also work for more than 3 rows.
     
    zeronese, Jul 16, 2009 IP