neonfish07 Posted May 9, 2007 Share Posted May 9, 2007 I'm writing a script for people to exchange cooking recipes. Basically, they submit recipes and other people view them. I want to find a way to easily display the top 5 contributors. I have a "recipes" table which stores the name of the submitter. In order to find how many recipes ONE person has submitted I have been using the following code: <?php $result = mysql_query("SELECT * FROM recipes WHERE submitter = 'Username', $link); $num_rows = mysql_num_rows($result); echo "This person has posted $num_rows Recipes\n"; ?> How can I do this for all users, and only display the 5 highest results? Thanks. Link to comment https://forums.phpfreaks.com/topic/50605-solved-how-would-i-do-this-top-5-contributors-system/ Share on other sites More sharing options...
neonfish07 Posted May 9, 2007 Author Share Posted May 9, 2007 Pardon the syntax error, I forgot the quote at the end of the query. Link to comment https://forums.phpfreaks.com/topic/50605-solved-how-would-i-do-this-top-5-contributors-system/#findComment-248742 Share on other sites More sharing options...
neonfish07 Posted May 9, 2007 Author Share Posted May 9, 2007 Perhaps a function that ranks the 5 most common results for the "submitter" column in the recipes table? Any ideas? :-\ Link to comment https://forums.phpfreaks.com/topic/50605-solved-how-would-i-do-this-top-5-contributors-system/#findComment-248777 Share on other sites More sharing options...
only one Posted May 9, 2007 Share Posted May 9, 2007 when they post add another post too a posts feild in the users table... $query = mysql_query("SELECT * FROM recipes ORDER BY posts DESC LIMIT 5)); while ($row = mysql_fetch_array($query)){ echo "$row[the filed]"; } edit bold bits.. Link to comment https://forums.phpfreaks.com/topic/50605-solved-how-would-i-do-this-top-5-contributors-system/#findComment-248845 Share on other sites More sharing options...
neonfish07 Posted May 9, 2007 Author Share Posted May 9, 2007 okay, I got it work. An excerpt of the mechanism to register # of submissions: <?php // This occurs once a recipe is approved by the moderators, inside protected admin.php file $hh = sprintf("SELECT * FROM users WHERE username = '".$u."'"); // $u is name of the user $jj = mysql_query($hh) or die(mysql_error()); // sets formatted query to a variable $users = mysql_fetch_assoc($jj); $posts = $row['posts']; $updatedpost = $row['posts']+1; $update = "UPDATE `users` SET `posts` = '".$updatedpost."' WHERE `username` = '".$u."'"; // formats the query mysql_query($update) or die(mysql_error()); ?> Then its displayed by using simple query as described by only one: <?php //top5module.php (displayed on the menu) $query = mysql_query("SELECT * FROM users ORDER BY posts DESC LIMIT 5"); while ($row = mysql_fetch_array($query)){ echo "$row[username]: $row[posts]"; } ?> And it works I'm VERY new to PHP so are there any security concerns with this I should be aware of? Any way to clean up this code? Link to comment https://forums.phpfreaks.com/topic/50605-solved-how-would-i-do-this-top-5-contributors-system/#findComment-249408 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.