Guest User

Untitled

a guest
Jun 10th, 2011
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. Ciao,
  2.  
  3. Ho una funzione che ordina una serie di immagini in ordine di voto, per poi stampare queste immagini e il relativo punteggio. Di seguito riporto la funzione così com'è ora:
  4.  
  5.  
  6. function top_images() {
  7. global $wpdb;
  8.  
  9. $images = $wpdb->get_results("SELECT pid, SUM(vote) AS totalVote, COUNT(id) AS numVotes
  10. FROM ".$wpdb->prefix."nggv_votes
  11. GROUP BY pid
  12. ORDER BY totalVote DESC
  13. LIMIT 10");
  14.  
  15. foreach($images as $image) {
  16. $detail = nggdb::find_image($image->pid);
  17. $results = nggv_getImageVotingResults($image->pid, array("likes"=>true));
  18. unset($out);
  19.  
  20. $out .= $results['likes'].' ';
  21. $out .= $results['likes'] == 1 ? 'Vote' : 'Votes';
  22.  
  23. /* Stampa il punteggio */
  24.  
  25. echo $out;
  26. }
  27. }
  28.  
  29.  
  30. se può essere utile, di seguito riporto come il plugin estrae i punteggi dal database:
  31.  
  32. /**
  33. * Get the voting results of an image
  34. * @param int $pid The image ID
  35. * @param array $type The type of results to return (can limit number of queries if you only need the avg for example)
  36. * bool type[avg] : Get average vote
  37. * bool type[list] : Get all the votes for the gallery
  38. * bool type[number] : Get the number of votes for the gallery
  39. * @author Shaun <[email protected]>
  40. * @return array("avg"=>double average for image, "list"=>array of objects of all votes of the image, "number"=>integer the number of votes for the image)
  41. */
  42. function nggv_getImageVotingResults($pid, $type=array("avg"=>true, "list"=>true, "number"=>true, "likes"=>true, "dislikes"=>true)) {
  43. if(is_numeric($pid)) {
  44. global $wpdb;
  45.  
  46. if($type["avg"]) {
  47. $avg = $wpdb->get_row("SELECT SUM(vote) / COUNT(vote) AS avg FROM ".$wpdb->prefix."nggv_votes WHERE pid = '".$wpdb->escape($pid)."' GROUP BY pid");
  48. }
  49. if($type["list"]) {
  50. $list = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."nggv_votes WHERE pid = '".$wpdb->escape($pid)."' ORDER BY dateadded DESC");
  51. }
  52. if($type["num"]) {
  53. $num = $wpdb->get_row("SELECT COUNT(vote) AS num FROM ".$wpdb->prefix."nggv_votes WHERE pid = '".$wpdb->escape($pid)."' GROUP BY pid");
  54. }
  55. if($type["likes"]) {
  56. $likes = $wpdb->get_row("SELECT COUNT(vote) AS num FROM ".$wpdb->prefix."nggv_votes WHERE pid = '".$wpdb->escape($pid)."' AND vote = 100 GROUP BY pid");
  57. }
  58. if($type["dislikes"]) {
  59. $dislikes = $wpdb->get_row("SELECT COUNT(vote) AS num FROM ".$wpdb->prefix."nggv_votes WHERE pid = '".$wpdb->escape($pid)."' AND vote = 0 GROUP BY pid");
  60. }
  61.  
  62. return array("avg"=>$avg->avg, "list"=>$list, "number"=>$num->num, "likes"=>($likes->num ? $likes->num : 0), "dislikes"=>($dislikes->num ? $dislikes->num : 0));
  63. }else{
  64. return array();
  65. }
  66. }
  67.  
  68.  
  69.  
  70. ==================================================================================
  71.  
  72. Quello che vorrei fare io è implementare un punteggio bayesano. In pratica, adattare questo script di seguito al mio caso:
  73.  
  74. // Bayesian Rating Calc
  75. $theItem = $_GET[’id’];
  76. if($theItem) {
  77. // all items votes and ratings
  78. $result = mysql_query(”SELECT AVG(item),AVG(vote) FROM itemvotes WHERE vote>’0′ GROUP BY item”) or die(mysql_error());
  79. $row = mysql_fetch_row($result);
  80. $avg_num_votes = $row[0];
  81. $avg_rating = $row[1];
  82.  
  83. // this item votes and ratings
  84. $result = mysql_query(”SELECT COUNT(item),AVG(vote) FROM itemvotes WHERE item=’$theItem’ AND vote>’0′”) or die(mysql_error());
  85. $row2 = mysql_fetch_row($result);
  86. $this_num_votes = $row2[0];
  87. $this_rating = $row2[1];
  88.  
  89. if(!$row OR !$row2)
  90. $br = “_”;
  91. else
  92. $br = number_format( ((($avg_num_votes * $avg_rating) + ($this_num_votes * $this_rating))/($avg_num_votes + $this_num_votes)), 1, ‘.’ );
  93. } // end of if item selected
Advertisement
Add Comment
Please, Sign In to add comment