Jump to content

top 10 users


Sam1

Recommended Posts

Hello

 

I want to learn how to make a script, that reads 2 tabels to find and print top 10 users

 

Table1:

postname -  userid

Tabel2:

username  - id

 

( userid = id )

 

the script needs to count and print top 10 users with most posts

 

#1 userA 1000 Posts

#2 userB 900 Posts

 

 

Thank you

Link to comment
Share on other sites

You should avoid calculation as much as you can. Instead add a post_count to the users table and then use

 

ORDER BY post_count DESC

 

Increment the post_count on each new post the user makes. This is also usefull for other business logic for example if your post count should not increase if you post in a certain forum.

Link to comment
Share on other sites

MYSQL is perectly happy performing calculations - and why bother storing such trivial data as post count?

 

$qry = "select DISTINCT(`table2`.`username`) AS `username`, COUNT(`table1`,`userid`) AS `postcount` FROM `table2` LEFT JOIN `table1` ON `table1`.`userid` = `table2`.`userid` GROUP BY `table2`.`userid` ORDER BY `postcount` DESC LIMIT 0,10";

 

Something like that should do the trick...

Link to comment
Share on other sites

WOW...you guys are amazing....thank you so much

 

I'm Beginner so please Be patient with me

 

how to print(echo) query result?

 

$qry = "select DISTINCT(`table2`.`username`) AS `username`, COUNT(`table1`,`userid`) AS `postcount` FROM `table2` LEFT JOIN `table1` ON `table1`.`userid` = `table2`.`userid` GROUP BY `table2`.`userid` ORDER BY `postcount` DESC LIMIT 0,10";

 

 

Link to comment
Share on other sites

and why bother storing such trivial data as post count?

 

This is also usefull for other business logic for example if your post count should not increase if you post in a certain forum. Being one reason, second reason would be because most likely it's going to appear as a widget in the left or right nav of the website and when you have many visitors (and thus many posts) your using your database intensively (calculating the total post_count over and over) to provide you with data that is trivial to your website.

Link to comment
Share on other sites

I tried

get_result ("select DISTINCT(`table2`.`username`) AS `username`, COUNT(`table1`,`userid`) AS `postcount` FROM `table2` LEFT JOIN `table1` ON `table1`.`userid` = `table2`.`userid` GROUP BY `table2`.`userid` ORDER BY `postcount` DESC LIMIT 0,10");

echo $query;

 

did not work

 

Warning: mysql_fetch_assoc() expects parameter 1 to be resource

Link to comment
Share on other sites

WOW...you guys are amazing....thank you so much

 

I'm Beginner so please Be patient with me

 

how to print(echo) query result?

 

$qry = "select DISTINCT(`table2`.`username`) AS `username`, COUNT(`table1`,`userid`) AS `postcount` FROM `table2` LEFT JOIN `table1` ON `table1`.`userid` = `table2`.`userid` GROUP BY `table2`.`userid` ORDER BY `postcount` DESC LIMIT 0,10";

Link to comment
Share on other sites

JAY6390

 

I tried

 

 

$qry = "SELECT *, COUNT(userid) as `cnt` FROM table1, table2 WHERE userid = id GROUP BY userid ORDER BY cnt DESC";
$result = mysql_query($qry) or die(mysql_error());

echo $result;

 

and it prints "Resource id #68 " !!!

 

what is my mistake?

 

Link to comment
Share on other sites

Your mistake is that mysql_query returns a result resource, you can't simply echo them. You'll need to turn your result resource into an array with mysql_fetch_assoc or similar, from there you can get to your data via the arrays indexes.

 

thank you for your help

 

***

 

like this?

 

$qry = "SELECT *, COUNT(userid) as `cnt` FROM table1, table2 WHERE userid = id GROUP BY userid ORDER BY cnt DESC LIMIT 0,10";";
$result = mysql_query($qry) or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) {
    echo $row["userid"];
 echo $row["cnt"];

}

mysql_free_result($result);

 

and how can i make the result into 2 columns , because now it like this

3333225614848152238156134101321073301129436232119 

 

thank you

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.