Jump to content

Top five script


supermerc

Recommended Posts

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!

Link to comment
Share on other sites

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++;
}

Link to comment
Share on other sites

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++;
    }
?>

Link to comment
Share on other sites

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>";
    }
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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;
}
?>

Link to comment
Share on other sites

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++;
    }
?>

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.