Jump to content

echo multiple rows in multiple columns


StefanRSA

Recommended Posts

I want to echo out a few subcategories per main category.

It should be a maximum of 20 entries per column

 

At this stage I have the following... It echo's the results out in 4 columns and not what I want.

Any suggestions?

 

$query = "SELECT 
adsubcat.name AS subname, adsubcat.linkname AS sublink, adcat.clinkname AS clink
FROM adsubcat 
JOIN adcat ON adcat.id=adsubcat.catid
WHERE adsubcat.catid='$catid' ORDER BY adsubcat.name ASC";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)) {

$count = 0;

print '<table style="text-align:left;margin-left:0px;width:943px;">'."\n";
while ($row = mysql_fetch_assoc($result)){
$gal = '<a href="'.$root.'/'.$row['clink'].'/'.$row['sublink'].'">'.$row['subname'].'</a>';
if ($count == 0){
print '<tr>';
}
++$count;
print '<td>'.$gal.'</td>';
if ($count == 4){
$count = 0;
print'</tr>'."\n";
}
}
print'</table>'."\n";
}

 

Link to comment
https://forums.phpfreaks.com/topic/212273-echo-multiple-rows-in-multiple-columns/
Share on other sites

Thanks for pointing that out wild...

 

My code now looks like this:

<?php
$query = "SELECT 
adsubcat.name AS subname, adsubcat.linkname AS sublink, adcat.clinkname AS clink
FROM adsubcat 
JOIN adcat ON adcat.id=adsubcat.catid
WHERE adsubcat.catid='$catid' ORDER BY adsubcat.name ASC";
$result = mysql_query($query) or die(mysql_error());
$count = 0;
print '<table style="text-align:left;margin-left:0px;width:943px;">'."\n";
while ($row = mysql_fetch_assoc($result)){
$gal = '<a href="'.$root.'/'.$row['clink'].'/'.$row['sublink'].'">'.$row['subname'].'</a>';
if ($count == 0){
print '<tr>';
}
++$count;
print '<td>'.$gal.'</td>';
if ($count == 4){
$count = 0;
print'</tr>'."\n";
}
}
print'</table>'."\n";
?>

 

But still it display as follow:

1  2  3  4

5  6  7  8

 

I want it as follow:

1  6    11

2  7    12

3  8    13

4  9    14

5  10  15

 

Basically I want a max of say 5 entries in each column... If there is more than 5 it should flow over to the next column...

 

Any suggestions?

When outputting results vertically, eg

1  6    11

2  7    12

3  8    13

4  9    14

5  10  15

 

You'll have to loop through the results a bit differently.

$query = "SELECT 
adsubcat.name AS subname, adsubcat.linkname AS sublink, adcat.clinkname AS clink
FROM adsubcat 
JOIN adcat ON adcat.id=adsubcat.catid
WHERE adsubcat.catid='$catid' ORDER BY adsubcat.name ASC";
$result = mysql_query($query) or die(mysql_error());


// add all the rows into the $results multi-dimensional array
$results = array();
while ($row = mysql_fetch_assoc($result))
    $results[] = $row;

/* Here is how we output the results vertically */

// set the number of columns
$cols = 3;
// work out how many rows will be needed
$rows = ceil(count($results) / $cols); 

echo '<table style="text-align:left;margin-left:0px;width:943px;">'."\n";

// output the rows
for($j = 0; $j < $rows; $j++)
{
    echo "  <tr>\n";
    
    // output the columns
    for($i = 0; $i < $cols; $i++)
    {
        $x = ($i*$rows)+$j;
        if(isset($results[$x]))
        {
            $row = $results[$x];
            echo '    <td><a href="'.$root.'/'.$row['clink'].'/'.$row['sublink'].'">'.$row['subname']. "</a></td>\n";
        }
        else
        {
            echo "    <td></td>\n";
        }
    }
    
    echo " </tr>\n";
}

echo '</table>';

Thanks... I got it on my own!  ::)

$query = "SELECT 
adsubcat.name AS subname, adsubcat.linkname AS sublink, adcat.clinkname AS clink
FROM adsubcat 
JOIN adcat ON adcat.id=adsubcat.catid
WHERE adsubcat.catid='$catid' ORDER BY adsubcat.name ASC";
$result = mysql_query($query) or die(mysql_error());
$count = 0;
print '<table style="text-align:left;margin-left:0px;width:943px;">'."\n";
echo '<tr>';
$trow=0;
while ($row = mysql_fetch_assoc($result)){
$gal = '<a href="'.$root.'/'.$row['clink'].'/'.$row['sublink'].'">'.$row['subname'].'</a>';
if ($trow == 0){
echo '<td style="vertical-align:top;">';
}
++$trow;
echo $gal.'<br>';
if ($trow == 6){
$trow = 0;
print'</td>';
}
}
echo '</tr>';
/*
while ($row = mysql_fetch_assoc($result)){
$gal = '<a href="'.$root.'/'.$row['clink'].'/'.$row['sublink'].'">'.$row['subname'].'</a>';
if ($count == 0){
print '<tr>';
}
++$count;
print '<td>'.$gal.'</td>';
if ($count == 4){
$count = 0;
print'</tr>'."\n";
}
}
*/
echo '</table>'."\n";

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.