Next Page Script is not working

here is the code for the display page and the code for the applications page…

What is going on is the first search with no category filter is working fine showing all results and stopping at the last page, but for the second search with the category it shows the proper results but does not stop, it keeps on saying next page even when the results are finished?? WHY???

Aplication page:


//-- Globals & Contants ------------------------------------------------------//

	global $__PREFS, $__KEYWORDS, $__REQUEST, $__PAGE;

//-- Application Class -------------------------------------------------------//

	/**
	 * 
	 */
	class CaseStudies extends Gallery
	{
		function generate_listing() // array
		{
			// Categories
			$q_categories = "
			select 
				pc.project_id, 
				c.category, 
				c.category_url_safe, 
				c.type 
			from 
				".$this->get_table_name('c')." as c, 
				".$this->get_table_name('pc')." as pc
			where 
				pc.category_id = c.id";

			$q_categories = mysql_query($q_categories, $this->connection);

			$gallery_categories = array();
			$case_study_categories = array();

			$category_found = false;
			$category_empty = empty($this->category);

			while ($r_categories = mysql_fetch_assoc($q_categories)) {
				if ($r_categories['type'] == 'gallery') {
					$gallery_categories[$r_categories['project_id']][] = $this->stripslashes($this->htmlout($r_categories['category']));
				} else if ($r_categories['type'] == 'case studies') {
					$case_study_categories[$r_categories['project_id']][] = $this->stripslashes($this->htmlout($r_categories['category']));
				}

				if ($category_empty === false && $this->category == $r_categories['category_url_safe']) {
					$category_found = true;
				}
			}

			// Do we have a category
			if ($category_empty === false && $category_found === false) {
				return array('error_code' => 404);
			} else if ($category_empty === false) {
			
	$limit=8; 

				$q = "select 
					distinct p.id, 
					p.name, 
					p.description, 
					p.thumbnail2, 
					c.category 
				from 
					".$this->get_table_name('p')." as p, 
					".$this->get_table_name('c')." as c, 
					".$this->get_table_name('pc')." as pc 
				where 
					c.category_url_safe = '%s' and 
					c.`type` = 'case studies' and
					c.id = pc.category_id and 
					pc.project_id = p.id 
				order by 
					p.promote DESC, p.num, p.name";
				/* LIMIT 0, 15;*/ // categoized page limit 15 
				
	$numresults=mysql_query($q);
	$numrows=mysql_num_rows($numresults);

	$s = 0; 
	if(isset($_GET['s']) && !empty($_GET['s'])){ 
 	$s = $_GET['s']; 
	}

	$q .= " limit $s,$limit";
	$result = mysql_query($q) or die("Couldn't execute query");
	$count = 1 + $s ;
				
				$q = sprintf($q, mysql_real_escape_string($this->category, $this->connection));	
			} else {
				$q = "select min(seq_id) as min, max(seq_id) as max from ".$this->get_table_name('p');
				$q = mysql_query($q, $this->connection);
				$r = mysql_fetch_assoc($q);
				$min = $r['min'];
				$max = $r['max'];

				$set = array();
				while (count($set) < 5) {
					$rand = mt_rand($min, $max);
					if (!in_array($rand, $set)) {
						$set[] = $rand;
					}
				}
	
			$limit=8;
									 
				$q = "select 
					distinct p.id, 
					p.name, 
					p.description, 
					p.thumbnail2 
				from 
					".$this->get_table_name('p')." as p, 
					".$this->get_table_name('c')." as c, 
					".$this->get_table_name('pc')." as pc 
				where 
					c.type = 'case studies' and 
					c.id = pc.category_id and 
					pc.project_id = p.ID
				order by 
					p.promote DESC, p.num, p.name";
				/* LIMIT 0, 8;*/ // categoized page limit 8 
				
	$numresults=mysql_query($q);
	$numrows=mysql_num_rows($numresults);

	$s = 0; 
	if(isset($_GET['s']) && !empty($_GET['s'])){ 
 	$s = $_GET['s']; 
	}

	$q .= " limit $s,$limit";
	$result = mysql_query($q) or die("Couldn't execute query");
	$count = 1 + $s ;
					
			}
			
//exit(var_dump($q));
			$q = mysql_query($q, $this->connection);

			$title = '';
			$projects = array();
			while ($r = mysql_fetch_assoc($q)) {
				$r['gallery_category'] = implode(', ', $gallery_categories[$r['id']]);
				$r['case_studies_category'] = implode(', ', $case_study_categories[$r['id']]);

				$r['name'] = $this->stripslashes($this->htmlout($r['name']));
				$r['description'] = $this->stripslashes($this->htmlout($r['description']));
				$r['body1'] = $this->stripslashes($this->htmlout($r['body1']));
				$projects[] = $r;

				if (!$title && isset($r['category'])) {
					$title = 'Case Studies: '.$this->stripslashes($this->htmlout($r['category']));
				}
			}

			if (!$title) {
				$title = 'Case Studies';
			}

			$current_category = $this->category;

			ob_start();
			include('template.case_study_listing.php');
			$content = ob_get_clean();

			return array('content' => $content, 'name' => $title);
		}
	}



the display page:



<div class="right_column">
 
<?php if (!empty($projects)) { ?>
	<?php foreach ($projects as $project) { ?>
<div class="gallery_block">
	<div class="photo_thumb"><a href="/case_studies/<?php echo $project['id']; ?>"><img src="/images/test/thumbnail2/<?php print $project['thumbnail2']; ?>" border="0" alt="" /></a></div>

	<h1><?php echo $project['name']; ?></h1>

	<p class="details"><?php echo $project['description']; ?><br />
	(<?php echo $project['gallery_category']; ?>)</p>

	<p><a href="/case_studies/<?php echo $project['id']; ?>">learn more</a></p>
</div>

	<?php } ?>
<?php } ?>

<?php 

 $count++ ;

echo "<br />";

$currPage = (($s/$limit) + 1);

//break before paging

 echo "<br /><center>";

  // next we need to do the links to other results

  if ($s>=1) { // bypass PREV link if s is 0

  $prevs=($s-$limit);

  $exx = 0;

  print " <a href=\\"$PHP_SELF?s=$prevs\\"><< 

  Prev 8</a>&nbsp ";

  }

// calculate number of pages needing links

  $pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

  if ($numrows%$limit) {

  // has remainder so add one page

  $pages++;

  }

// check to see if last page

  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

  // not last page so give NEXT link

  $news=($s+$limit);

  $exx = 0;  //Sets the incoming variable counter to exit to zero

  echo " <a href=\\"$PHP_SELF?s=$news\\">Next 8 >></a>";

  }

?>


else if ($category_empty === false)

this is the query that does not work if the category is empty and it shows all the next script works fine… That query is on the bottom of the page…