• I’m trying to create a function to display the top voted images, but it’s pushing me beyond my php skills. The code below is in my functions.php. It is not returning any errors, but it also isn’t returning the images. I put it together from:

    • The db query found in this forum post
    • The html to display the images found in the nextgen gallery plugin. (/view/gallery.php lines 38 and following)

    My hunch is that I’m close, but like I said, I don’t know php quite well enough to figure out what’s not working right. Appreciate any help getting it right:

    function top_images() {
    global $wpdb;
    
    $images = $wpdb->get_results("SELECT pid, SUM(vote) AS totalVote;
    FROM wp_nggv_votes;
    GROUP BY pid;
    ORDER BY totalVote DESC;
    LIMIT 10;");
    
    foreach($images as $image) {
    global $wp_query;
    $wp_query->get_queried_object();
    echo "<a href='";
    echo $image->imageURL;
    echo "' title='";
    echo $image->description;
    echo "'";
    echo $image->thumbcode;
    echo "'><img title='";
    echo $image->alttext;
    echo "' alt='";
    echo $image->alttext;
    echo "' src='";
    echo $image->thumbnailURL;
    echo "'";
    echo $image->size;
    echo " /></a>";
    }
    }
Viewing 15 replies - 1 through 15 (of 47 total)
  • Plugin Author shauno

    (@shauno)

    Hey John

    There’s a couple syntax errors with your query. You don’t need semi-colons after each line in the query, or even at the end of the query.

    Then, once the query does return a result, all it is returning is pretty much the pid of the image (the pictures unique identifier in the data base). You need one more step to get all the pictures info.

    I have hacked together a quick bit of code that should do what you need. I have added a note to my TODO list to add a way for users to get top lists

    Good luck!

    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);
    
    		/* Uncomment this to see all the fields you have for each image
    		print("<pre style='font-family:verdana;font-size:13;color:#000;z-index:99999;background:#ccc;'>");
    		print_r($detail);
    		print("</pre>");
    		*/
    
    		echo "<a href='";
    		echo $detail->imageURL;
    		echo "' title='";
    		echo $detail->description;
    		echo "'";
    		echo $detail->thumbcode;
    		echo "'><img title='";
    		echo $detail->alttext;
    		echo "' alt='";
    		echo $detail->alttext;
    		echo "' src='";
    		echo $detail->thumbURL;
    		echo "'";
    		echo " /></a>";
    	}
    }
    Thread Starter John Chandler

    (@euangel)

    Working perfectly. Thanks for the prompt response!

    Thread Starter John Chandler

    (@euangel)

    Just for others looking to do this. Here’s my final function. I removed the hyperlink wrapped around each image, and then I added the voting results for each image:

    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 .= '<div class="like-results">';
    		$out .= $results['likes'].' ';
    		$out .= $results['likes'] == 1 ? 'Vote' : 'Votes';
    		$out .= '</div>';
    
    		/*Uncomment this to see all the fields you have for each image
    		print("<pre style='font-family:verdana;font-size:13;color:#000;z-index:99999;background:#ccc;'>");
    		print_r($detail);
    		print("</pre>");
    		*/
    		echo "<img title='";
    		echo $detail->alttext;
    		echo "' alt='";
    		echo $detail->alttext;
    		echo "' src='";
    		echo $detail->thumbURL;
    		echo "' />";
    		echo $out;
    	}
    }

    Hey guys,

    Thank you so much for this piece of code. Displaying the most rated galleries is just what I need nowadays, but unfortunately the code John has sent in the last post is only displaying one gallery, without it’s thumbnail and the total votes of all galleries existing.

    I have worked out how to display the thumbnail(I am using a custom field for the post thumbnails and I’ve managed to get it working). But unfortunately I couldn’t work out how to display all of the posts with each having their own vote count.

    Thank you in advance

    John where can I insert this function to order my gallery by votes. In which file and where exactly?
    Thanks in advance 🙂

    Hey guys,

    Had the same task. Done it with a plugin which provides php execution in pages and posts.

    See this article on my website with the code. It’s in german though, but google should be able to translate it good enough.

    Whats really interesting for you is the Code box in chapter 1.1.
    You can copy that and give it a go if you want.
    Just make sure you have the plugin called Allow PHP in Posts and Pages installed and paste the code between the tags [php] and [/php].

    Thats it.

    Result is here: http://www.rustystrings.eu

    Good luck. Rob

    Reshovski

    (@reshovski)

    I try it but nothing shows. Blank screen.

    Reshovski

    (@reshovski)

    I catch it. Change into your code ““ to “” !!!

    Plugin Author shauno

    (@shauno)

    Check out v1.8 🙂
    Cool new filter to list top voted images.

    http://wordpress.org/extend/plugins/nextgen-gallery-voting/

    hi!

    i’ve got a problem.. i put john’s code in function.php, but how can i recall it in a post/page?

    thank you very much, you guys are fantastic!

    Jacopod,

    Once you define the function, you should be able to call it with php in the page template:
    <?php top_images(); ?>

    thank you for your help, it works.

    i’d like also to get for every image this value:

    score=(avg*numer_votes)/100

    i think this can be a good score method in a stars-based voting system. I’d like also to get top 10 images with that score.

    How can i do?

    i’d like also to display the score value. at the moment i’m using John’s code, wich diasplay likes.

    foreach($images as $image) {
    		$results = nggv_getImageVotingResults($image->pid, array("likes"=>true));
    		unset($out);
    
    		$out .= $results['likes'].' ';
    		$out .= $results['likes'] == 1 ? 'Vote' : 'Votes';

    how can i display my score system?

    thank in advance guys!

    edit: if we define avg=sum(votes)/numer_votes the score appears to be sum(votes)/100

    simpler problem:

    how can i extract from database avarage vote for every image, number of votes for every image, total numer of votes and total avarage vote

    thank you for helping!

    I’m having problems as to where to insert this code and if I did insert correctly then I’m having trouble calling top images with

    <?php top_images(); ?>

    Could someone post up a how to… I just can’t figure out where I have to insert the code and where I insert the code to call top rated images.

    Thanks in advance

Viewing 15 replies - 1 through 15 (of 47 total)
  • The topic ‘[Plugin: NextGEN Gallery Voting] Need help displaying the top voted images’ is closed to new replies.