StefanRSA Posted September 1, 2010 Share Posted September 1, 2010 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/212273-echo-multiple-rows-in-multiple-columns/ Share on other sites More sharing options...
wildteen88 Posted September 1, 2010 Share Posted September 1, 2010 I think the issue is you have two while loops. You only need the one. Remove the inner while loop Quote Link to comment https://forums.phpfreaks.com/topic/212273-echo-multiple-rows-in-multiple-columns/#findComment-1106035 Share on other sites More sharing options...
StefanRSA Posted September 1, 2010 Author Share Posted September 1, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/212273-echo-multiple-rows-in-multiple-columns/#findComment-1106049 Share on other sites More sharing options...
wildteen88 Posted September 1, 2010 Share Posted September 1, 2010 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/212273-echo-multiple-rows-in-multiple-columns/#findComment-1106081 Share on other sites More sharing options...
StefanRSA Posted September 1, 2010 Author Share Posted September 1, 2010 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"; Quote Link to comment https://forums.phpfreaks.com/topic/212273-echo-multiple-rows-in-multiple-columns/#findComment-1106083 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.