Jump to content

Displaying query results in 3 colums


e1seix

Recommended Posts

Quite a simple one really.

 

If i execute a query in my php program that will return many results, how can i get it to divide the results between 3 seperate columns to display it.

 

here's what i have so far but it just isn't working. Can anyone shed any light?

<?php
$fetch=mysql_query("SELECT * from fragrances WHERE avail='true'")or
die(mysql_error());
$numrows=mysql_num_rows($fetch);

$max = intval($numrows/3);

if ($numrows%3) {
    // has remainder so add one page
    $max++;
}

$qry = mysql_query("SELECT * from logos WHERE avail='true' ORDER BY 'brand' LIMIT 0, $max")or
die(mysql_error());

echo "<table><tr><td style='width:33%'>";
$i=0;
while ($row = mysql_fetch_array($qry))
{
    if ($i > $max)
    {
        echo "<td style='width:33%'>";
        $i=0;
    }

echo '<div id="brand">';
echo '<a href="'.$_HTTPS['PHP_SELF'].''.$row['page'].'.php?sku='.$row['sku'].'">';
echo $row ['brand'];
echo "</a></td>";
echo '</div>';

    $i++;
}

echo "</td></tr>";
echo "</table>";

for example if the query returns 60 results (to make it easy), it will display first 20 results ($max) before if will then run a <td> tag to run the next 20 and again for the final 20. the <td> tags always 33% wide.

 

 

Link to comment
https://forums.phpfreaks.com/topic/68636-displaying-query-results-in-3-colums/
Share on other sites

try

while ($row = mysql_fetch_array($qry))
{
    if ($i == 0)
    {
        echo "<td style='width:33%'>";
    }

echo '<div id="brand">';
echo '<a href="'.$_HTTPS['PHP_SELF'].''.$row['page'].'.php?sku='.$row['sku'].'">';
echo $row ['brand'];
echo "</a>";
echo '</div>';
    $i++;
    if ($i == $max){
    	echo '</td>';
    	$i = 0;
    }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.