Jump to content

[SOLVED] How would I do this? ('top 5 contributors' system)


neonfish07

Recommended Posts

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.

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

okay, I got it work.  ;D

 

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  ;D

 

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?

 

 

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.