Jump to content

Count loop


Mutley

Recommended Posts

I'm wanting to count how many "1" there are in a mysql table under a column called "xyz" then loop it to display the top 5 users (user_id) who has the mosts "1"s

 

My database table is like this:

user_id | xyz

1        |

1        | 1

2        | 1

2        | 1

 

So then on my page it shows:

 

User:  |  XYZs:

2      | 2

1      |  1

 

I hope that makes sense, I don't know how to loop a SELECT COUNT in this way.

Link to comment
https://forums.phpfreaks.com/topic/45553-count-loop/
Share on other sites

$query = "SELECT userid, SUM(xyz) AS hits FROM table_name GROUP BY userid ORDER BY hits DESC";
$result = mysql_query($query) or die(mysql_error());

echo '
  <table>
    <tr>
      <td>Userid</td>
      <td>Count</td>
    </tr>';

while ($row = mysql_fetch_assoc($result)) {
  echo '
    <tr>
      <td>' . $row['userid'] . '</td>
      <td>' . $row['hits'] . '</td>
    </tr>';
}

Link to comment
https://forums.phpfreaks.com/topic/45553-count-loop/#findComment-221405
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.