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>"; Quote 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 Quote 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++; } Quote 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++; } Quote 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. Quote 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!! Quote Link to comment https://forums.phpfreaks.com/topic/151140-php-results-question/#findComment-794112 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.