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
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;
    }
}

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.