Author Topic: Multi-column Results  (Read 15767 times)

0 Members and 1 Guest are viewing this topic.

Offline oberTopic starter

  • Pandas pwn j00
  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,312
  • Gender: Male
  • 404? what!?
    • View Profile
    • Windy Hill Productions
Multi-column Results
« on: June 09, 2006, 08:48:46 AM »
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.

Code: [Select]
<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.
« Last Edit: November 10, 2006, 10:48:53 AM by ober »
PHP 5 - MySQL 5 - Win Vista 64 - Firefox 3, IE 7
Info: PHP Manual

Quote from CV: After nuclear fallout, there'll be zombies everywhere.  You can't be running from them when you're all fat and shit.  They'll just catch you and eat you and take forever to do it and you'll just have to sit there all that much longer.

Offline oberTopic starter

  • Pandas pwn j00
  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,312
  • Gender: Male
  • 404? what!?
    • View Profile
    • Windy Hill Productions
Re: Multi-column Results
« Reply #1 on: November 10, 2006, 10:49:36 AM »
Updated 11/10/2006... contained a slight error with a call to "mssql_fetch_array()" instead of "mysql_fetch_array()".
PHP 5 - MySQL 5 - Win Vista 64 - Firefox 3, IE 7
Info: PHP Manual

Quote from CV: After nuclear fallout, there'll be zombies everywhere.  You can't be running from them when you're all fat and shit.  They'll just catch you and eat you and take forever to do it and you'll just have to sit there all that much longer.

Offline .josh

  • Administrator
  • 'Insane!'
  • *
  • Posts: 13,150
  • Grumpy Old Man
    • View Profile
Re: Multi-column Results
« Reply #2 on: November 06, 2008, 08:52:42 PM »
A slight alternative to this is to use the modulus operator:

example.php

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

Did I help you? Feeling generous? Donate to me! | Donate to phpfreaks!

Offline sasa

  • Guru
  • Fanatic
  • *
  • Posts: 3,011
  • Gender: Male
    • View Profile
Re: Multi-column Results
« Reply #3 on: September 12, 2010, 01:38:30 PM »
this part of codeif($i $max_columns)
{
    for(
$j=$i$j<$max_columns;$j++)
        echo 
"<td>&nbsp;</td>";
}
 
?>
</tr>
</table>
shold beif($i 0)
{
    for(
$j=$i$j<$max_columns;$j++) echo "<td>&nbsp;</td>";
   echo 
'</tr>';
}
 
?>
</table>