Jump to content


Photo

Multi-column Results


  • Please log in to reply
3 replies to this topic

#1 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 09 June 2006 - 12:48 PM

Q: How do I create a multi-column layout for my gallery/store from MySQL results or an array?

A: A lot of times you'll see people suggest using CSS for layouts.  This is one time 99% of developers are going to tell you to use a table.

<table cellspacing="3" cellpadding="3">
<?php
$query = "SELECT product FROM selling_items ORDER BY prod_id";
$result = mysql_query($query) or die("There was a problem with the SQL query: " . mysql_error()); 
if($result && mysql_num_rows($result) > 0)
{
    $i = 0;
    $max_columns = 3;
    while($row = mysql_fetch_array($result))        
   {
       // make the variables easy to deal with
       extract($row);

       // open row if counter is zero
       if($i == 0)
          echo "<tr>";

       // make sure we have a valid product
       if($product != "" && $product != null)
          echo "<td>$product</td>";
    
       // increment counter - if counter = max columns, reset counter and close row
       if(++$i == $max_columns) 
       {
           echo "</tr>";
           $i=0;
       }  // end if 
   } // end while
} // end if results

// clean up table - makes your code valid!
if($i < $max_columns)
{
    for($j=$i; $j<$max_columns;$j++)
        echo "<td>&nbsp;</td>";
}
 ?>
</tr>
</table>

To modify this for an array, just replace the while loop with a for loop and use your array as the input instead of the mysql fetch results.

Info: PHP Manual


#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 10 November 2006 - 03:49 PM

Updated 11/10/2006... contained a slight error with a call to "mssql_fetch_array()" instead of "mysql_fetch_array()".

Info: PHP Manual


#3 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 07 November 2008 - 01:52 AM

A slight alternative to this is to use the modulus operator:

example.php
<?php
   $cols = 0;
   echo "<table><tr>";
   while ($cols < 20) {
      echo ($cols % 3 == 0)? "</tr><tr>" : "";
      echo "<td>$cols</td>";
      $cols++;
   }
   echo "</tr></table>";
?> 

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#4 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 12 September 2010 - 05:38 PM

this part of code
if($i < $max_columns)
{
    for($j=$i; $j<$max_columns;$j++)
        echo "<td>&nbsp;</td>";
}
 ?>
</tr>
</table>
shold be
if($i > 0)
{
    for($j=$i; $j<$max_columns;$j++) echo "<td>&nbsp;</td>";
   echo '</tr>';
}
 ?>
</table>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users