nauir Posted March 25, 2009 Share Posted March 25, 2009 I have: $query = "SELECT iname, COUNT(ownerid) AS num FROM items WHERE ownerid=$sid GROUP BY iname "; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo $row['iname'] . "(x " . $row['num'] . ")<br />"; } How do I make it so that every 5 results it prints print "<tr>"; Link to comment https://forums.phpfreaks.com/topic/151140-php-results-question/ Share on other sites More sharing options...
lonewolf217 Posted March 25, 2009 Share Posted March 25, 2009 just put in a counter $query = "SELECT iname, COUNT(ownerid) AS num FROM items WHERE ownerid=$sid GROUP BY iname "; $i=1; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo $row['iname'] . "(x " . $row['num'] . ")<br />"; if($i==4) { echo "<tr>"; } $i++; } of course you'll have to format it to your exact table needs Link to comment https://forums.phpfreaks.com/topic/151140-php-results-question/#findComment-793964 Share on other sites More sharing options...
Maq Posted March 25, 2009 Share Posted March 25, 2009 He said every 5 results not the first 5. Try: $query = "SELECT iname, COUNT(ownerid) AS num FROM items WHERE ownerid=$sid GROUP BY iname "; $result = mysql_query($query) or die(mysql_error()); $x=1; while($row = mysql_fetch_array($result)){ echo ($x % 5 == 0) ? "" . $row['iname'] . "(x " . $row['num'] . ") " : $row['iname'] . "(x " . $row['num'] . ") "; $x++; } Link to comment https://forums.phpfreaks.com/topic/151140-php-results-question/#findComment-793970 Share on other sites More sharing options...
lonewolf217 Posted March 25, 2009 Share Posted March 25, 2009 ugh sorry i missed resetting the counter $query = "SELECT iname, COUNT(ownerid) AS num FROM items WHERE ownerid=$sid GROUP BY iname "; $i=1; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo $row['iname'] . "(x " . $row['num'] . ")<br />"; if($i==5) { echo "<tr>"; $i=0; } $i++; } Link to comment https://forums.phpfreaks.com/topic/151140-php-results-question/#findComment-793974 Share on other sites More sharing options...
Maq Posted March 25, 2009 Share Posted March 25, 2009 ugh sorry i missed resetting the counter Hehe, it's cool. @nauir: Either way should work, lonewolf's is probably easier to read. Link to comment https://forums.phpfreaks.com/topic/151140-php-results-question/#findComment-793977 Share on other sites More sharing options...
nauir Posted March 26, 2009 Author Share Posted March 26, 2009 Just to let lonewolf know, the $=1; makes it print 4 in the first row and then 5 in the second. I took it out and then it worked just fine. Thank you all for your wonderful help!! Link to comment https://forums.phpfreaks.com/topic/151140-php-results-question/#findComment-794112 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.