Jump to content

Recommended Posts

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

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

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

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

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.