Jump to content

PHP results question.


nauir

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

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.