supermerc Posted April 28, 2007 Share Posted April 28, 2007 Hey, Im trying to make a little code that will display the top 5 art of the selected user with the greatest rating. I have an idea of how to do it but not completely so i need help on how i need to do it. For of all my rating table is set up like this, ID, total_votes, total_value, which_id, which_table, used_ips, tpath. So I think you would have to do display 5 where total value divided by total votes or something but Im new to php and dont really know how to put it in php so if someone could help me it would be greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/49035-top-five-script/ Share on other sites More sharing options...
snowdog Posted April 28, 2007 Share Posted April 28, 2007 You want to do a search of the table. If it is # of votes you want then the code would look something like this. This is how I do it to echo out say the top 5 hockey scorers in my hockey league of 120 players. Snowdog $query = "select * from mytable ORDER BY total_votes DESC"; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); while($show = mysql_fetch_object($result)) { $id = $show->id; $total_votes = $show->total_votes; $which_id = $show->which_id; $Which_table = $show->which_table; $used_ips = $show->used_ips; $tpath = $show->tpath; if($top5_count < 5) { echo("<h3 align='center'><font color='yellow'> $id ,$total_votes , etc or WHATEVER YOU WANT ON THE SCREEN </font></h3>"); } $top5_count++; } Quote Link to comment https://forums.phpfreaks.com/topic/49035-top-five-script/#findComment-240220 Share on other sites More sharing options...
supermerc Posted April 28, 2007 Author Share Posted April 28, 2007 But I dont just want the one with the most vote, I want to ones with the higher average so it has to be divided by the number of votes Quote Link to comment https://forums.phpfreaks.com/topic/49035-top-five-script/#findComment-240226 Share on other sites More sharing options...
supermerc Posted April 28, 2007 Author Share Posted April 28, 2007 bump Quote Link to comment https://forums.phpfreaks.com/topic/49035-top-five-script/#findComment-240386 Share on other sites More sharing options...
MadTechie Posted April 28, 2007 Share Posted April 28, 2007 what code do you have so far ? Quote Link to comment https://forums.phpfreaks.com/topic/49035-top-five-script/#findComment-240388 Share on other sites More sharing options...
supermerc Posted April 28, 2007 Author Share Posted April 28, 2007 The code I have so far displays ALL the art for that person, so lets say they have 100 art it will all show, I want to reduce it to 5 and that those 5 are the top ones, here is what I got <?php $rating = mysql_query("SELECT * FROM ratings WHERE which_user = '{$profile_info['username']}'") or die(mysql_error()); if(mysql_num_rows($rating) > 0) //create a loop, because there are rows in the DB // How many images to span across the page before they go on to the next $maxItemsPerRow = 5; $i = 1; ?> <table border="0" cellpadding="1" cellspacing="0"> <tr> <?php while($row = mysql_fetch_assoc($rating)) { echo "<td style=\"font-size: 7pt; font-family:Verdana, Arial, Helvetica, sans-serif; \"> <a href=\"javascript:poptastic('view_art.php?member_id=$row[which_id]');\"><img src=\"$row[tpath]\" /></a> </td>"; if($i % $maxItemsPerRow == 0) { // Insert row break echo "</tr><tr>"; } $i++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/49035-top-five-script/#findComment-240394 Share on other sites More sharing options...
MadTechie Posted April 28, 2007 Share Posted April 28, 2007 so something like this <?php // How many images to span across the page before they go on to the next $maxItemsPerRow = 5; $StartRow = 0; $rating = mysql_query("SELECT * FROM ratings WHERE which_user = '{$profile_info['username']}' ORDER BY total_votes DESC LIMIT $StartRow,$maxItemsPerRow") or die(mysql_error()); if(mysql_num_rows($rating) > 0) //create a loop, because there are rows in the DB ?> <table border="0" cellpadding="1" cellspacing="0"> <tr> // Insert row break </tr><tr> <?php while($row = mysql_fetch_assoc($rating)) { echo "<td style=\"font-size: 7pt; font-family:Verdana, Arial, Helvetica, sans-serif; \"> <a href=\"javascript:poptastic('view_art.php?member_id=$row[which_id]');\"><img src=\"$row[tpath]\" /></a> </td>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/49035-top-five-script/#findComment-240395 Share on other sites More sharing options...
supermerc Posted April 28, 2007 Author Share Posted April 28, 2007 But it has to be like total_value divided by total vote because for example if theres a really old art thats been rated only 1 star 200 times, the total value will be 200, and a new art could have been rated 5 10 times so the total value is only 50 but its rating is greater, do you understand? Quote Link to comment https://forums.phpfreaks.com/topic/49035-top-five-script/#findComment-240398 Share on other sites More sharing options...
taith Posted April 28, 2007 Share Posted April 28, 2007 might require some improvisation... but i'm sure we can get it :-) <?php $query=mysql_query("SELECT * FROM ratings WHERE which_user = '{$profile_info['username']}'") or die(mysql_error()); while($row=mysql_fetch_assoc($query)){ $array[$row[id]]=$row[won]/$row[total]*100; } sort($array); ?> now you have an $array of all the user id's and their averages... you just need to sort them out... as such... <? $i=0; foreach($array as $k=>$v){ echo $k.'-->'.$v; $i++; if($i==5) break; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/49035-top-five-script/#findComment-240400 Share on other sites More sharing options...
supermerc Posted April 28, 2007 Author Share Posted April 28, 2007 I tried it and it gives me Warning: Division by zero in /home/randomy/public_html/view_profile3.php on line 133 <?php $query=mysql_query("SELECT * FROM ratings WHERE which_user = '{$profile_info['username']}'") or die(mysql_error()); while($row=mysql_fetch_assoc($query)){ $array[$row[id]]=$row[won]/$row[total]*100; } sort($array); //create a loop, because there are rows in the DB // How many images to span across the page before they go on to the next $maxItemsPerRow = 5; $i=0; foreach($array as $k=>$v){ echo $k.'-->'.$v; $i++; if($i==5) break; } ?> <table border="0" cellpadding="1" cellspacing="0"> <tr> <?php while($row = mysql_fetch_assoc($query)) { echo "<td style=\"font-size: 7pt; font-family:Verdana, Arial, Helvetica, sans-serif; \"> <a href=\"javascript:poptastic('view_art.php?member_id=$row[which_id]');\"><img src=\"$row[tpath]\" /></a> </td>"; if($i % $maxItemsPerRow == 0) { // Insert row break echo "</tr><tr>"; } $i++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/49035-top-five-script/#findComment-240406 Share on other sites More sharing options...
taith Posted April 28, 2007 Share Posted April 28, 2007 you need to configure "$array[$row[id]]=$row[won]/$row[total]*100;" to your [votes field] and [total field] Quote Link to comment https://forums.phpfreaks.com/topic/49035-top-five-script/#findComment-240414 Share on other sites More sharing options...
supermerc Posted April 28, 2007 Author Share Posted April 28, 2007 I did and now it shows values... 0-->1001-->2002-->2003-->3004-->400 thats what is shown Quote Link to comment https://forums.phpfreaks.com/topic/49035-top-five-script/#findComment-240415 Share on other sites More sharing options...
supermerc Posted April 28, 2007 Author Share Posted April 28, 2007 bump... its not displaying images! Quote Link to comment https://forums.phpfreaks.com/topic/49035-top-five-script/#findComment-240448 Share on other sites More sharing options...
supermerc Posted April 28, 2007 Author Share Posted April 28, 2007 bump... Quote Link to comment https://forums.phpfreaks.com/topic/49035-top-five-script/#findComment-240628 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.