Jump to content

counting rows...


xwishmasterx

Recommended Posts

Hello

 

I have a table that inserts a new row with data when a member views a page.

 

I wish to count all the rows for each member, and then be able to show what the cuurent members "position" is eg. what members has the highest row count.

 

Example. counting the rows for member_A returns 1000 rows, the number of rows for member_B returns 1500 rows.

 

How can I display for member_A that he is in "second" place?

Link to comment
https://forums.phpfreaks.com/topic/248469-counting-rows/
Share on other sites

SELECT COUNT(*) AS views FROM views_table GROUP BY user_id ORDER BY views DESC

 

 

//get and loop query
$i = 1;
$n = array(1 => 'st', 2 => 'nd', 3 => 'rd');
while($row = mysql_fetch_row($var))
{
   if ( substr($i, -1) < 4 )
   {
      $i .= $n[$i];
   }
   else
   {
      $i .= 'th';
   }
//etc.
}

Link to comment
https://forums.phpfreaks.com/topic/248469-counting-rows/#findComment-1275961
Share on other sites

mysql_query("SET @rows = 0;");
$res = mysql_query("SELECT @rows:=@rows+1 AS view_rank, COUNT(*) AS views, user_id FROM views_table GROUP BY user_id ORDER BY views DESC");
$n   = array(1 => 'st', 2 => 'nd', 3 => 'rd');
while($row = mysql_fetch_row($res))
{
   if ( $row[2] == $user_id )
   {
      if ( substr($row[0], -1) < 4 )
      {
         $row[0] .= $n[$row[0]];
      }
      else
      {
         $row[0] .= 'th';
      }
      echo $row[0] . ' with ' . number_format($row[1], 2) . ' views.';
   }
}

Link to comment
https://forums.phpfreaks.com/topic/248469-counting-rows/#findComment-1275985
Share on other sites

mysql_query("SET @rows = 0;");
$res = mysql_query("SELECT @rows:=@rows+1 AS view_rank, COUNT(*) AS views, user_id FROM views_table GROUP BY user_id ORDER BY views DESC");
$n   = array(1 => 'st', 2 => 'nd', 3 => 'rd');
while($row = mysql_fetch_row($res))
{
   if ( $row[2] != $user_id )
      continue;
   if ( substr($row[0], -1) < 4 )
   {
      $row[0] .= $n[$row[0]];
   }
   else
   {
      $row[0] .= 'th';
   }
   echo $row[0] . ' with ' . number_format($row[1], 2) . ' views.';
   break;
}

 

Would be better actually, probably not the best way to do it tho.

Link to comment
https://forums.phpfreaks.com/topic/248469-counting-rows/#findComment-1276008
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.