Jump to content

Using a three column layout for table results


maddie2120

Recommended Posts

Hi everyone, I'm having real problems trying to retrieve database records in a 3 column layout, I got there eventually with a huge amount of help.

 

Unfortunately the code will only display records that are divisable by by three; for example for a database table that has 45 records there is no problem, as all records can be displayed in a 3 column layout. However if the table contains 47 records it won't display the 2 odd records.  My php skills are limited so I need all the help I can get.

 

I'm a bit desperate to sort this out for a project I'm doing, any help would be greatly appreciated - here is the php code:

 

$total = count($records);
  $nocol = 3;
  $norows = $total / $nocol;

for ($i=1; $i <= $norows; $i++) {
    $cell = 0;
    echo "<tr>";
    for($col=1; $col <= $nocol; $col++) {
       echo "<td>";
       if
       ($col == 1) {
       $cell += $i;
       echo '<strong class="navtext">'.$records[$cell - 1]['ret_name'].'</strong><br />';
       echo $records[$cell - 1]['ret_add1'].'<br />';
       echo $records[$cell - 1]['ret_add2'].'<br />';
       echo $records[$cell - 1]['ret_town'].'<br />';
       echo $records[$cell - 1]['ret_county'].'<br />';
       echo $records[$cell - 1]['ret_pcode'].'<br />';
       echo $records[$cell - 1]['ret_phone'].'<br />';
       echo $records[$cell - 1]['ret_email'].'<br />';
       echo $records[$cell - 1]['ret_web'].'<br />';
       } else {
        $cell += $norows;
       echo '<strong class="navtext">'.$records[$cell - 1]['ret_name'].'</strong><br />';
       echo $records[$cell - 1]['ret_add1'].'<br />';
       echo $records[$cell - 1]['ret_add2'].'<br />';
       echo $records[$cell - 1]['ret_town'].'<br />';
       echo $records[$cell - 1]['ret_county'].'<br />';
       echo $records[$cell - 1]['ret_pcode'].'<br />';
       echo $records[$cell - 1]['ret_phone'].'<br />';
       echo $records[$cell - 1]['ret_email'].'<br />';
       echo $records[$cell - 1]['ret_web'].'<br />';
       
       }
       echo"</td>";
    }
      echo"</tr>";
} 

 

I'm also trying to paginate the results, is this actually possible when using a three column layout?  I look forward to any suggestions.

Link to comment
Share on other sites

Thank you for the link, the code looks really good unfortunately I need to use it in an array as in my original code, the code mentioned only has one column so I would need to create an array and I don't think I have the skills to do it.

 

I also didn't understand how the '$product' variable would work as it didn't seem to be assigned anything - is that correct or is this just my lack of understanding, I have copied the code below from that forum thread written by:

 

Ober

Dated June 09 2006

 

(hope that's okay)

 

<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> </td>";
}
?>
</tr>
</table>

 

Any ideas as to how I could combine the two or if anyone could point me in the right direction I would be most grateful - many thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.