Return to Snippet

Revision: 59745
at September 27, 2012 09:33 by noobcoder


Initial Code
$firstQuizSql = $wpdb->get_results("SELECT id, datetaken, person FROM aw_wpsqt_all_results WHERE person_name='$user_info->user_login' ORDER BY id");
	$i = 1;
	foreach ($firstQuizSql as $firstQuizResult) {
		//first quiz result
		if ($i == 1) {
			echo $firstQuizResult->id . " ";
		}
		
		//last quiz result
		if ($i == count($firstQuizSql)) {
			echo $firstQuizResult->id . " ";
		}
		
		$i++;
	}

Initial URL

                                

Initial Description
I needed to know the first time a user took a quiz and the last quiz they took. I utilized php count() and as I iterated through the array, I incremented a variable ($i) which before the loop, I already set it to 1. If $i was equal to 1, I printed it out. If I was equal to the total count, I printed it out. Everything else got ignored.

Initial Title
Wordpress - First and last result of wp_query

Initial Tags

                                

Initial Language
PHP