Jump to content

Recommended Posts

Hey all,

 

I am pretty new to PHP, as you will probably tell from this question.

 

What I am trying to do is grab data from 2 columns from a table in a mySQL database and then display it in a grid as follows:

 

product1name<br>product1link product2name<br>product2link product3name<br>product3link product4name<br>product4link product5name<br>product5link product6name<br>product6link

product7name<br>product7linketcetc..[/td]

 

where product1name and product1link etc is the data that is pulled from the database and inserted into the same table cell.

 

The page will change depending on what product category is selected which means the grid can have 2 to 24 products called.

 

I have no problems getting the data from the database and displayed on a webpage in a simple 1 column table; I just can't work out how to make it display in the grid format, with a maximum of 6 columns.

 

Thanks for any pointers,

 

Paul

 

 

Link to comment
https://forums.phpfreaks.com/topic/98018-displaying-data-in-a-grid/
Share on other sites

see example here

http://www.phpfreaks.com/forums/index.php/topic,188922.msg847244.html#msg847244

 

UPDATE:

try something like this

<?php
echo "<table border=\"0\">\n<tr>\n";
$c = 4; //Rows
$t=0;
while ($row = mysql_fetch_assoc($result)) {
    $t++;    
    if(!($t % $c)) echo "</tr><tr>"; 
    echo "<td>";
    echo $row["userid"];
    echo $row["fullname"];
    echo "</td>";
}
if(!($t % $c)) echo "</tr>";
echo "</table>\n";
?>

Just want to make the comment that I don't think that code accounts for leaving the while loop early. Like if you fill up 4 boxes on the first row and then only 1 on the second, your table will be messed up since there won't be 3 more empty cells to fill the row in completely. Not tested, but just wrote it to show theory.

<?php
echo "<table><tr>";
$cells = 4; // Cells per row
$total = 0;
$early = 0;
while ($row = mysql_fetch_array($result))
{
$t++;
$mod = $total % $cells;
if (!$mod)
{
	echo "</tr><tr>";
}
echo "<td>{$row['userid']} <br /> {$row['fullname']}</td>";
$early = $cells - $mod;
}
if(!$mod)
{
echo "</tr>";
$early = 0;
}
if ($early)
{
for ($i = 1; $i <= $early; $i++)
{
	echo "<td> </td>";
}
echo "</tr>";
}
echo "</table>";
?>

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.