Forums

NextGEN Gallery Voting
Need help displaying the top voted images (48 posts)

  1. John Chandler
    Member
    Posted 1 year ago #

    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>";
    }
    }
  2. shauno
    Member
    Posted 1 year ago #

    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>";
    	}
    }
  3. John Chandler
    Member
    Posted 1 year ago #

    Working perfectly. Thanks for the prompt response!

  4. John Chandler
    Member
    Posted 1 year ago #

    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;
    	}
    }
  5. stndomains
    Member
    Posted 1 year ago #

    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

  6. Reshovski
    Member
    Posted 1 year ago #

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

  7. rustyrob
    Member
    Posted 1 year ago #

    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

  8. Reshovski
    Member
    Posted 1 year ago #

    I try it but nothing shows. Blank screen.

  9. Reshovski
    Member
    Posted 1 year ago #

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

  10. shauno
    Member
    Posted 1 year ago #

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

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

  11. jacopod
    Member
    Posted 11 months ago #

    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!

  12. johndchandler
    Member
    Posted 11 months ago #

    Jacopod,

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

  13. jacopod
    Member
    Posted 11 months ago #

    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

  14. jacopod
    Member
    Posted 11 months ago #

    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!

  15. SlickVicENT
    Member
    Posted 11 months ago #

    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(); ?>

  16. SlickVicENT
    Member
    Posted 11 months ago #

    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

  17. shauno
    Member
    Posted 11 months ago #

    @SlickVicENT: Dude, there's a button for that in the backend.

    @jacopod: Dude, there's a button for that in the backend.

    Did you guys even read the thread? Specifically the part where I said

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

    @Everyone-who-asks-to-see-top-or-average-image-results-somewhere-ever-again: Dude, there's a button for that in the backend.

  18. SlickVicENT
    Member
    Posted 11 months ago #

    I am totally laughing at your reply... I just got a notification to update to 1.8 I thought I was updated all along. Thanks for the laugh @Shauno

  19. shauno
    Member
    Posted 11 months ago #

    Lol, glad you took that the right way. Psst, it's been in since 1.8 :)

  20. SlickVicENT
    Member
    Posted 11 months ago #

    Lol...

    Hey Shauno... I'm still looking for the back end BUTTON to display top voted images and I can't find it...

    I guess I am terrible at this

  21. shauno
    Member
    Posted 11 months ago #

    It's under the 'NGG Voting Defaults' toolbar on the left side. The option labeled 'Top Rated Images'.

  22. SlickVicENT
    Member
    Posted 11 months ago #

    Ok I saw that but how do I get images to display on my website anywhere I want it to show. I really want to display top rated pics on my home page.... not the side bar

    Thanks for all the help

  23. shauno
    Member
    Posted 11 months ago #

    Um, you can't...
    That's purely for the admin to have a look at what's-what with their images.

    If I had to make a front end thing I would need to make it templatable, and be able to handle arguments like what gallery, how many images, voting thresholds etc.

    Maybe in the future. But for now, it's bed time for bitter programmers in South Africa :)

    PS, have a look at the function nggv_admin_top_rated_images() on line 803 if you want to try hack it together yourself...

  24. SlickVicENT
    Member
    Posted 11 months ago #

    Ok thanks for everything then...

    Would of loved to display for visitors on the home page. I wish I could program but I can't. Well maybe in the future. I know someone on this thread has top ten images posting on the website but his function and website is german.

    Well thanks again.... night! night!

  25. shauno
    Member
    Posted 11 months ago #

    Copy and paste this into your functions file:

    function shaunos_awsome_top_images_i_should_really_consider_donating($limit=10) {
    	global $wpdb, $nggdb;
    	$qry = 'SELECT pid, SUM(vote) AS total, AVG(vote) AS avg, MIN(vote) AS min, MAX(vote) AS max, COUNT(vote) AS num'; //yes, no joins for now. performance isnt an issue (i hope...)
    	$qry .= ' FROM '.$wpdb->prefix.'nggv_votes';
    	$qry .= ' WHERE';
    	$qry .= ' pid > 0';
    	$qry .= ' GROUP BY pid';
    	$qry .= ' ORDER BY avg DESC';
    	$qry .= ' LIMIT 0, '.$limit;
    
    	$list = $wpdb->get_results($qry);
    
    	foreach ($list as $key=>$val) {
    		$val->image = nggdb::find_image($val->pid);
    
    		/*
    		//Uncomment this amazing piece of code to see all the magical attributes provided, free of charge, by NextGEN. Radical bro.
    		print("<pre style='font-family:verdana;font-size:13;color:#000;z-index:99999;background:#ccc;'>");
    		print_r($val);
    		print("</pre>");
    		*/
    
    		echo "<a href='";
    		echo $val->image->imageURL;
    		echo "' title='";
    		echo $val->image->description;
    		echo "'";
    		echo $val->image->thumbcode;
    		echo "'><img title='";
    		echo $val->image->alttext;
    		echo "' alt='";
    		echo $val->image->alttext;
    		echo "' src='";
    		echo $val->image->thumbURL;
    		echo "'";
    		echo $val->image->size;
    		echo " /></a>";
    	}
    }

    Then paste this code anywhere you want to show your top images:
    <?php shaunos_awsome_top_images_i_should_really_consider_donating(10); ?>
    You can change the 10 to the number of images you want to show.

  26. SlickVicENT
    Member
    Posted 11 months ago #

    Hey Sauno you are awesome even though I haven't tried the program you just wrote.

    But when you say copy and paste into your functions file do you mean functions in the theme back end area??? Sorry don't know where exactly I would copy and paste.

    I will be sending donation as soon as I can. Thanks for doing this for me and obviously everyone else that would like to use.

  27. SlickVicENT
    Member
    Posted 11 months ago #

    I posted on Photocrati Theme: Theme Functions (functions.php) below the line to insert custom functions

    not sure if that's where I should put your program.

  28. SlickVicENT
    Member
    Posted 11 months ago #

    installed Exec-PHP plugin and now i get this...

    Fatal error: Call to undefined function shaunos_awsome_top_images_i_should_really_consider_donating() in /home/content/99/7381199/html/wp-content/plugins/exec-php/includes/runtime.php(42) : eval()'d code on line 1

    I think I'm on to something....

  29. shauno
    Member
    Posted 11 months ago #

    You do put it in your theme's function file. That should make it available anywhere in your site.

    You then should just need to paste the call to the function anywhere in your theme you want to show the pictures (I tested in my index.php in the default twentyten theme). You don't need Exec-PHP or anything else.

  30. SlickVicENT
    Member
    Posted 11 months ago #

    posted in the following areas

    functions.php ( My Theme Functions )
    single.php ( My Single Post )
    and
    index.php ( My Main Index Template )

    I posted program at the very bottom and when I use the call to the function on my home page and I wasnt able to call my top rated images

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic