PHP Pagination


#1

Hi Guys

I’m in a bit of a flat spin here. I’m putting a gallery together. I’ve implemented this script for pagination. I also have three filters running on the page to sort image, by drop downs. It works pretty good except when i do filter the images via a drop down the pagination does not display the correct amount of pages or based on the new query.

I hope someone can help out here.

Thanks in advance

[code]

<?php include(''); ?> Please Select Landscape Portrait Panoramic Please Select Not Free Free Please Select <?php
	  $result = mysql_query("SELECT * FROM ''");
		for ($i=0; $i < (mysql_num_rows($result)); $i++)
		{
			$thisrow = mysql_fetch_row($result);  

			print	"<option value='$thisrow[0]'>$thisrow[1]</option>";

		  
	  } ?>
        </select>
      <input type="submit" name="Submit" value="Filter" />
  </div>
</form></td>
<?
	//**EDIT TO YOUR TABLE NAME, ECT.
	
	$t = mysql_query("SELECT * FROM ''");
	  if(!$t) die(mysql_error());
	   
	$a                = mysql_fetch_object($t);
	$total_items      = mysql_num_rows($t);
	$limit            = $_GET['limit'];
	$type             = $_GET['type'];
	$page             = $_GET['page'];
	

	$setup            = getRequestVariable("setup");
	$type             = getRequestVariable("type");
	$category         = getRequestVariable("category");
	
	//set default if: $limit is empty, non numerical, less than 10, greater than 50
	if((!$limit)  || (is_numeric($limit) == false) || ($limit < 10) || ($limit > 50)) {
		 $limit = 4; //default
	}
	//set default if: $page is empty, non numerical, less than zero, greater than total available
	if((!$page) || (is_numeric($page) == false) || ($page < 0) || ($page > $total_items)) {
		  $page = 1; //default
	}
	
	//calcuate total pages
	$total_pages        = ceil($total_items / $limit);
	$set_limit          = $page * $limit - ($limit);
	
	//query: **EDIT TO YOUR TABLE NAME, ECT.
	
	$isFirst = true;

	$result = "";
	$selectbase = "SELECT * FROM '' "; 

						   if ( $setup==""){															
							
							
							}else{
							
								if ($isFirst){
									$result = $result . " WHERE ";
									$isFirst = false;
								}else{
									$result = $result . " AND ";
								}
								$result  = "" . $result . " setup = '$setup' ";
							}

						/* ======================================================================================================= */
							
							if ( $type==""){
							
							
							}else{
							
								if ($isFirst){
									$result = $result . " WHERE ";
									$isFirst = false;
								}else{
									$result = $result . " AND ";
								}
							
								$result = "" . $result . " photo_type  = '$type' ";
							}
							
						/* ======================================================================================================= */
							
							if ( $category==""){
							

							}else{
							
								if ($isFirst){
									$result = $result . " WHERE ";
									$isFirst = false;
								}else{
									$result = $result . " AND ";
								}
							
								$result = "" .  $result . " category  = '$category' ";
							}						

						/* ======================================================================================================= */
											
						
							$tail = "  LIMIT $set_limit, $limit ";
							

	 $fullquery = $selectbase . $result . $tail;
	 
	
	$q = mysql_query("$fullquery");
	  if(!$q) die(mysql_error());
		 $err = mysql_num_rows($q);
		   if($err == 0) die("<span class='no_result'><br><br><br><div align='center'>No matches met your criteria.<br>Please try another combination</div></span>");
	

	print "<table width='545' border='0' cellspacing='0' cellpadding='0'>";
	
	//show data matching query:
	while($code = mysql_fetch_object($q)) {
		 echo("".$code->title."");
		 
		 
			print "  <tr>";
			print "    <td width='273' align='center'><img src=viewThumb.php?id=".$code->id." border='1'><br><br></td>";
			print "   <td width='272'  align='center'>";
			
				if($thisrow = mysql_fetch_row($q))
				{
					$code->id++;
					
						print "<img src=viewThumb.php?id=".$code->id." border='1'><br><br>";
				}
			
			print "</td>";
			print "  </tr>";
 
	}
	

	print "</table>";


	
	
	
	//all done
	?></td>
<?php
		$cat = urlencode($cat); //makes browser friendly
	
	//prev. page: **EDIT LINK PATH**
	
	$prev_page = $page - 1;


	
	if($prev_page >= 1) {
	  echo("<span class='next'><b>&lt;&lt;</b> <a href=gallery.php?cat=$cat&amp;limit=$limit&amp;page=$prev_page><b>Prev.</b></a></span>");
	}
	
	//Display middle pages: **EDIT LINK PATH**
	
	for($a = 1; $a <= $total_pages; $a++)
	{
	   if($a == $page) {
		  echo("<span class='next'><b> $a</b> | </span>"); //no link
		 } else {
	  echo("  <span class='next'><a href=gallery.php?cat=$cat&amp;limit=$limit&amp;page=$a> $a </a> | </span>");
		 }
	}
	
	//next page: **EDIT THIS LINK PATH**
	
	$next_page = $page + 1;
	if($next_page <= $total_pages) {
	   echo("<span class='next'><a href=gallery.php?cat=$cat&amp;limit=$limit&amp;page=$next_page><b>Next</b></a> &gt; &gt;</span>");
	}


?>	
 </div></td>

[/code][/quote]


#2

Have you tried Debugging?