Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Ciao,
- 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:
- function top_images() {
- global $wpdb;
- $images = $wpdb->get_results("SELECT pid, SUM(vote) AS totalVote, COUNT(id) AS numVotes
- FROM ".$wpdb->prefix."nggv_votes
- GROUP BY pid
- ORDER BY totalVote DESC
- LIMIT 10");
- foreach($images as $image) {
- $detail = nggdb::find_image($image->pid);
- $results = nggv_getImageVotingResults($image->pid, array("likes"=>true));
- unset($out);
- $out .= $results['likes'].' ';
- $out .= $results['likes'] == 1 ? 'Vote' : 'Votes';
- /* Stampa il punteggio */
- echo $out;
- }
- }
- se può essere utile, di seguito riporto come il plugin estrae i punteggi dal database:
- /**
- * Get the voting results of an image
- * @param int $pid The image ID
- * @param array $type The type of results to return (can limit number of queries if you only need the avg for example)
- * bool type[avg] : Get average vote
- * bool type[list] : Get all the votes for the gallery
- * bool type[number] : Get the number of votes for the gallery
- * @author Shaun <[email protected]>
- * @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)
- */
- function nggv_getImageVotingResults($pid, $type=array("avg"=>true, "list"=>true, "number"=>true, "likes"=>true, "dislikes"=>true)) {
- if(is_numeric($pid)) {
- global $wpdb;
- if($type["avg"]) {
- $avg = $wpdb->get_row("SELECT SUM(vote) / COUNT(vote) AS avg FROM ".$wpdb->prefix."nggv_votes WHERE pid = '".$wpdb->escape($pid)."' GROUP BY pid");
- }
- if($type["list"]) {
- $list = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."nggv_votes WHERE pid = '".$wpdb->escape($pid)."' ORDER BY dateadded DESC");
- }
- if($type["num"]) {
- $num = $wpdb->get_row("SELECT COUNT(vote) AS num FROM ".$wpdb->prefix."nggv_votes WHERE pid = '".$wpdb->escape($pid)."' GROUP BY pid");
- }
- if($type["likes"]) {
- $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");
- }
- if($type["dislikes"]) {
- $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");
- }
- return array("avg"=>$avg->avg, "list"=>$list, "number"=>$num->num, "likes"=>($likes->num ? $likes->num : 0), "dislikes"=>($dislikes->num ? $dislikes->num : 0));
- }else{
- return array();
- }
- }
- ==================================================================================
- Quello che vorrei fare io è implementare un punteggio bayesano. In pratica, adattare questo script di seguito al mio caso:
- // Bayesian Rating Calc
- $theItem = $_GET[’id’];
- if($theItem) {
- // all items votes and ratings
- $result = mysql_query(”SELECT AVG(item),AVG(vote) FROM itemvotes WHERE vote>’0′ GROUP BY item”) or die(mysql_error());
- $row = mysql_fetch_row($result);
- $avg_num_votes = $row[0];
- $avg_rating = $row[1];
- // this item votes and ratings
- $result = mysql_query(”SELECT COUNT(item),AVG(vote) FROM itemvotes WHERE item=’$theItem’ AND vote>’0′”) or die(mysql_error());
- $row2 = mysql_fetch_row($result);
- $this_num_votes = $row2[0];
- $this_rating = $row2[1];
- if(!$row OR !$row2)
- $br = “_”;
- else
- $br = number_format( ((($avg_num_votes * $avg_rating) + ($this_num_votes * $this_rating))/($avg_num_votes + $this_num_votes)), 1, ‘.’ );
- } // end of if item selected
Advertisement
Add Comment
Please, Sign In to add comment