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
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
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
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
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.