Jump to content

Paged results


graham23s

Recommended Posts

Hi Guys,

 

i'm so close to cracking this pagination code i have re-written some of it.

 

i have re-written some of the pagination code, the problem i have now is its bringing me back ALL results from the db

 

in the db there is 4 members, when i search a citeria it brings me back 2 members' profiles (which is correct but it shows me 4 links 1 for each member!)

 

new code:

 

<?php
  ## deal with the submission ########################################################### 
  if(isset($_GET['search'])) {  
   
  ## address bar fix 
  $addressbar = NULL; 
   
  ## vars we'll be using 
   $gender = mysql_real_escape_string($_GET['gender']); 
  $herefor = mysql_real_escape_string($_GET['herefor']); 
  $country = mysql_real_escape_string($_GET['country']); 
  $fromage = mysql_real_escape_string($_GET['fromage']); 
    $toage = mysql_real_escape_string($_GET['toage']);  
$ethnicity = mysql_real_escape_string($_GET['ethnicity']); 
     $hair = mysql_real_escape_string($_GET['hair']); 
    $build = mysql_real_escape_string($_GET['build']); 
     $eyes = mysql_real_escape_string($_GET['eyes']); 
     
  ## Pagination start ############################################################################# 
  if(!isset($_GET['page'])){  
     $page = 1;  
         } else {  
     $page = $_GET['page'];  
  }  
     
  $max = 1;  
  $num = $page * $max - $max;   
  ## Pagination start #############################################################################         
  //======================== COMPILE QUERY ========================// 
  $searchquery  = "SELECT * FROM `users` WHERE 1=1 "; 
   
  ## GENDER ######################################################### 
  if(isset($gender) && ($gender != '0')) {  
   
  $searchquery .= "AND `gender`='$gender' ";  
  $addressbar .= "&gender=$gender"; 
   
  } 
  ## HERE FOR ####################################################### 
  if(isset($herefor) && ($herefor != '0')) {  
   
  $searchquery .= "AND `herefor`='$herefor' ";  
  $addressbar .= "&herefor=$herefor"; 
   
  }   
  ## COUNTRY ######################################################## 
  if(isset($country) && ($country != '0')) {  
   
  $searchquery .= "AND `country`='$country' ";  
  $addressbar .= "&country=$country"; 
   
  }   
  ## ETHNICITY ###################################################### 
  if(isset($ethnicity) && ($ethnicity != '0')) {  
   
  $searchquery .= "AND `ethnic`='$ethnicity' ";  
  $addressbar .= "&ethnicity=$ethnicity"; 
   
  }  
  ## HAIR ########################################################### 
  if(isset($hair) && ($hair != '0')) {  
   
  $searchquery .= "AND `hair`='$hair' ";  
  $addressbar .= "&hair=$hair"; 
   
  }   
  ## BUILD ########################################################## 
  if(isset($build) && ($build != '0')) {  
   
  $searchquery .= "AND `build`='$build' ";  
  $addressbar .= "&build=$build"; 
   
  }     
  ## EYE_C ########################################################## 
  if(isset($eyes) && ($eyes != '0')) {  
   
  $searchquery .= "AND `eye_c`='$eyes' ";  
  $addressbar .= "&eyes=$eyes"; 
   
  }      
  ## AGES ########################################################### 
  $searchquery .= "AND `year` BETWEEN YEAR(CURDATE() - INTERVAL $toage YEAR) AND YEAR(CURDATE() - INTERVAL $fromage YEAR) LIMIT $num, $max"; 
  //======================== COMPILE QUERY ========================// 
  echo $searchquery; 
  ## run query 
  $resultquery = mysql_query($searchquery); 
   
  ## num results 
  $numresults = mysql_num_rows($resultquery); 
   
  ## no results 
  if($numresults == 0) { 

  echo ('<br /><span class="gallery_header">Sorry, no results were found!</span><br />'); 
  include("includes/footer.php"); 
  exit; 
     
  } else { 
    
  echo ('<br /><span class="gallery_header">We have found you <b>'.$numresults.'</b> matches!</span><br /><br />'); 
   
  while($row = mysql_fetch_array($resultquery)) { 
   
  ## details 
  $searchid = $row['id']; 
  $searchusername = $row['username']; 
  $searchthumb = $row['thumbnail']; 
   
  $nopic = (!empty($searchthumb)) ? "<img src=\"thumbs/$searchthumb\">" : "<img src=\"images/noimgup.jpg\""; 
    
  ## some results 
  echo ("<table class=\"sub_table\" align=\"center\" width=\"300\" border=\"0\" cellpadding=\"5\" cellspacing=\"0\">"); 
  echo ("<tr>"); 
  echo ("<td class=\"sub_table\" align=\"center\">$nopic</td>"); 
  echo ("</tr>"); 
  echo ("</table>");  
   
  } // end while all the results should be displayed 
   
  ## Pagination Links ############################################################################# 
  $totalresults = mysql_result(mysql_query("SELECT COUNT(*) FROM `users`"),0);  
     
  $totalpage = ceil($totalresults/$max) + 1;  
   
  ## previous link 
  if ($page > 1) {  
           echo "<a href=\"?page=".($page-1)."\">[Prev]</a> \n";  
  }   
  ## no link this is the page your on    
  for($i=1; $i<$totalpage; $i++) {  
     
        if ($i == $page) {  
            echo "<b>[$i] </b>\n";  
        } else {  
            echo "<a href=\"?page=".$i."\">[$i]</a> \n";  
        }  
  }  
  ## next link     
  if ($page < $totalpage-1) {  
           echo "<a href=\"?page=".($page+1)."\">[Next]</a>\n";  
  }  
  ## Pagination Links #############################################################################   
   
  } 
   
  } // end if 
?>

 

it's hopefully something small i have overlooked lol thanks for the help guys

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/80026-paged-results/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.