Jump to content

Page navigation from search results - If you can work it out your a genious!!


Recommended Posts

Hi,

 

Was Hoping for a bit of help....Bit of a tricky one but here goes!

 

I have created a search form that searchs for results from my database matching the criteria to what ever the user inputs. I have done this using 2 php pages the first page points to the second, where the data is sanitised and cleaned accordingly. The results are displayed on the 2nd page.

 

Heres a copy of the code for the form:

 

<form name="search" method="get" action="search_laptop_details.php"
		onsubmit="return validate_form(this)">
              <input type="text" name="finda"/> 
              <span class="style10">in</span>
              <Select NAME="field">
		  
             	<Option VALUE="laptopname">Laptop Name</option>
                <Option VALUE="statusID">Status</option>		


              </Select>
              <input type="hidden" name="searching" value="yes" />
              <input type="submit" name="search" value="Search" />
		  </form>	

 

the second page is:

 


<?php



    $limit          = 10;               
    $query_count    = "SELECT count(*) FROM laptopname";    
    $result_count   = mysql_query($query_count);    
    $totalrows      = mysql_num_rows($result_count); 

    if(empty($page)){
        $page = 1;
    }
        

    $limitvalue = $page * $limit - ($limit); 
    $query  = "SELECT * FROM laptopname WHERE " . $_GET['field'] . " LIKE '%" . $_GET['finda'] . "%' LIMIT $limitvalue, $limit";     
//	echo $query;  
    $result = mysql_query($query) or die("Error: " . mysql_error()); 

    if(mysql_num_rows($result) == 0){
        echo("Nothing to Display!");
    }

    

      
    



?>

<?php

//check if the user entered data in the form
if (isset($_GET['finda'])) {
//data has been entered so lets search the mofo

echo "<h2>Results</h2><p>";
	// sanitise the data
	$find = strtoupper($_GET['finda']);
	$find = strip_tags($_GET['finda']);
	$find = trim ($_GET['finda']);
	$data = mysql_query($query);




       	//And we display the results
	while($result = mysql_fetch_array( $data ))
	{

   

	echo"<table width=\"200\" border=\"0\" class=\"border_bottom\"> \n";
	echo "<tr> \n";
  
  echo "<td> </td> \n";
  echo "</td> \n";
  echo "</tr> \n";
  echo "<tr> \n";
  
  
    echo "<td>";	echo "<strong>Laptop Name</strong>"; echo "</td>"; 
    echo "<td width=\"75\" style =\"text-align: left\""; ?> <em><a href="search_laptop_details.php?recordID=<?php echo $result['LaptopName']; ?>"><?php echo $result['LaptopName']; ?> </a></em> <?php echo " </td> \n"; 
  echo "</tr> \n";
  echo "<tr> \n";
  
  		echo "<td>";	echo "<strong>Status</strong>"; echo "</td>";
       echo "<td>";	echo "<em>"; 	echo $result['StatusID']; echo "</em>"; echo "</td>";
echo "</tr> \n";
  	echo "<tr> \n";



    
  echo "</tr> \n";

  echo "<tr> \n";
  echo "<td> </td> \n";
  echo "</td> \n";
  echo "</tr> \n";
echo "</table> \n";
echo"<br>";
   
    }
	}

	//This counts the number or results - and if there wasn't any it gives them a little message explaining that
	$anymatches=mysql_num_rows($data);
	if ($anymatches == 0)
	{
	echo "Sorry, but we can not finda an entry to match your query<br><br>";
	}


?>

 

So far this all works smoothly!!! No problem at all. The problem is when i try to add in the page navigation for it......Here is copy of page navigation bit:

 


<?php



    $limit          = 2;               
    $query_count    = "SELECT count(*) FROM teachersname";    
    $result_count   = mysql_query($query_count);    
    $totalrows      = mysql_num_rows($result_count); 

    if(empty($page)){
        $page = 1;
    }
        

    $limitvalue = $page * $limit - ($limit); 
    $query  = "SELECT * FROM teachersname WHERE " . $_GET['field'] . " LIKE '%" . $_GET['finda'] . "%' LIMIT $limitvalue, $limit";     
//	echo $query;  
    $result = mysql_query($query) or die("Error: " . mysql_error()); 

    if(mysql_num_rows($result) == 0){
        echo("Nothing to Display!");
    }

    

      
    



?>

<?php

//check if the user entered data in the form
if (isset($_GET['finda'])) {
//data has been entered so lets search the mofo

echo "<h2>Results</h2><p>";
	// sanitise the data
	$find = strtoupper($_GET['finda']);
	$find = strip_tags($_GET['finda']);
	$find = trim ($_GET['finda']);
	//connect to the db
	//mysql_connect("localhost", "root", "winn3rs") or die(mysql_error());
	//mysql_select_db("laptop_loan_database") or die(mysql_error());
	//Now we search for our search term, in the field the user specified
//	$query = "SELECT * FROM teachersname WHERE " . $_GET['field'] . " LIKE '%$finda%'"  ;
	$data = mysql_query($query);




       	//And we display the results
	while($result = mysql_fetch_array( $data ))
	{

   

	echo"<table width=\"200\" border=\"0\" class=\"border_bottom\"> \n";
	echo "<tr> \n";
  
  echo "<td> </td> \n";
  echo "</td> \n";
  echo "</tr> \n";
  echo "<tr> \n";
    echo "<td>";	echo "<strong>Client Name</strong>"; echo "</td>"; 
    echo "<td width=\"75\" style =\"text-align: left\""; ?> <em><a href="search_client_details.php?recordID=<?php echo $result['Client']; ?>"><?php echo $result['Client']; ?> </a></em> <?php echo " </td> \n"; 
  echo "</tr> \n";
  echo "<tr> \n";
  
  		echo "<td>";	echo "<strong>Department Code</strong>"; echo "</td>";
       echo "<td>";	echo "<em>"; 	echo $result['DepartmentCode']; echo "</em>"; echo "</td>";
    
  echo "</tr> \n";

  echo "<tr> \n";
  echo "<td> </td> \n";
  echo "</td> \n";
  echo "</tr> \n";
echo "</table> \n";
echo"<br>";
   
    }
	}

	//This counts the number or results - and if there wasn't any it gives them a little message explaining that
	$anymatches=mysql_num_rows($data);
	if ($anymatches == 0)
	{
	echo "Sorry, but we can not finda an entry to match your query<br><br>";
	}


?>

<?php

    if($page != 1){ 
        $pageprev = $page--;
         


        echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV" .$limit."</a> "); 

    }else{
        echo("PREV" . $limit." ");
    }

    $numofpages = $totalrows / $limit; 
    

    for($i = 1; $i <= $numofpages; $i++){
        if($i == $page){
            echo($i." ");
        }else{
            echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");

       }
    }


    if(($totalrows % $limit) != 0){
        if($i == $page){
            echo($i." ");
        }else{
           											echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");

        }
    }

   if(($totalrows - ($limit * $page)) > 0){
        $pagenext = $page++;
         
       												 echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT" .$limit."</a>"); 
    }else{
       												 echo("NEXT" . $limit); 
	$pagenext = ($page++)+1;
	echo("<a href=\"$PHP_SELF?page=$pagenext&field=" . $_GET['field'] . "&finda=" . $_GET['finda'] . "\">NEXT" .$limit."</a>"); 

    } 
?>	

 

Please note the page navigation code page is for a section of the database called clients, other than that it is identical.

 

It seems that when it displays the desired number of records and you can see that there are more records on the following page and the "Next page" is hyperlinked to the next page....That when clicked it doesnt do anything??? It just loads the same page with the same results.

 

When looking at what the browser is pointing at (hovering mouse pointer over the "Next Page" hyperlink it says that i is looking for whatever records = whatever was entered into the search field at the beginning. NOT page 2??? 

 

How can i get this to working properly as the nvigation doesnt seem to work at all. It will not link to 2nd page of results BUT it WILL show that there are more records avaliable on next page as the "Next Page" hyperlinnk is infact hyperlinked, otherwise it would NOT bwe hyperlinked.

 

Please help me im becomming very desperate now and getting close to my deadline. Ill be so greatful if you can help me out here. If you want to ask anymore questions regarding it or what the code itself, please just let me know.

 

Many thanks

 

Tom :)

First of all, you aren't checking if $page is passed to the script.

 

Add this to the top somewhere

$page = trim(addslashes(strip_tags($_GET['page'])));

if($page == "" OR !is_numeric($page))
unset($page);

 

Secondly.... change all the ++ and -- that are affecting $page.

IE:

$pageprev = $page--;

 

This first evaluates and assigns a new value to $page, then places that value into $pageprev. This will mess up the page number. Instead, use "+ 1" in place of "++" and "- 1" in place of "--".

 

There are more typos in the script, but hopefully this should get you started.

 

Hope it helps,

-Kalivos

Hi there and thanks for the reply.

 

With regards to the second bit you suggested:

 

"Secondly.... change all the ++ and -- that are affecting $page.

IE:

Code:

$pageprev = $page--;"

 

Im a little confused because looking at my code that is what it says it is already??? Could you elaborate a bit more please??

 

Also i tried changing to +1 NOT ++ and same for -- but when doing so i notice that the browser (when hovered over "Next Page" hyperlink) points to 1!!??

 

 

 

thank you

Your code uses the "++" syntax. You need to step away from using that in this script.

 

An example of what to change:

$pagenext = ($page++)+1;

 

to this...

 

$pagenext = $page+2;

 

Post your new code so we can look over the changes you have made.

 

-Kalivos

Hi,

 

Ive made the changes to the code to give me the following:

 


<?php



    $limit          = 2;               
    $query_count    = "SELECT count(*) FROM teachersname";    
    $result_count   = mysql_query($query_count);    
    $totalrows      = mysql_num_rows($result_count); 

    if(empty($page)){
        $page = 1;
    }
        

    $limitvalue = $page * $limit - ($limit); 
    $query  = "SELECT * FROM teachersname WHERE " . $_GET['field'] . " LIKE '%" . $_GET['finda'] . "%' LIMIT $limitvalue, $limit";     
//	echo $query;  
    $result = mysql_query($query) or die("Error: " . mysql_error()); 

    if(mysql_num_rows($result) == 0){
        echo("Nothing to Display!");
    }

    

      
    



?>

<?php

//check if the user entered data in the form
if (isset($_GET['finda'])) {
//data has been entered so lets search the mofo

$page = trim(addslashes(strip_tags($_GET['page'])));

if($page == "" OR !is_numeric($page))
unset($page);

echo "<h2>Results</h2><p>";
	// sanitise the data
	$find = strtoupper($_GET['finda']);
	$find = strip_tags($_GET['finda']);
	$find = trim ($_GET['finda']);
	//connect to the db
	//mysql_connect("localhost", "root", "winn3rs") or die(mysql_error());
	//mysql_select_db("laptop_loan_database") or die(mysql_error());
	//Now we search for our search term, in the field the user specified
//	$query = "SELECT * FROM teachersname WHERE " . $_GET['field'] . " LIKE '%$finda%'"  ;
	$data = mysql_query($query);




       	//And we display the results
	while($result = mysql_fetch_array( $data ))
	{

   

	echo"<table width=\"200\" border=\"0\" class=\"border_bottom\"> \n";
	echo "<tr> \n";
  
  echo "<td> </td> \n";
  echo "</td> \n";
  echo "</tr> \n";
  echo "<tr> \n";
    echo "<td>";	echo "<strong>Client Name</strong>"; echo "</td>"; 
    echo "<td width=\"75\" style =\"text-align: left\""; ?> <em><a href="search_client_details.php?recordID=<?php echo $result['Client']; ?>"><?php echo $result['Client']; ?> </a></em> <?php echo " </td> \n"; 
  echo "</tr> \n";
  echo "<tr> \n";
  
  		echo "<td>";	echo "<strong>Department Code</strong>"; echo "</td>";
       echo "<td>";	echo "<em>"; 	echo $result['DepartmentCode']; echo "</em>"; echo "</td>";
    
  echo "</tr> \n";

  echo "<tr> \n";
  echo "<td> </td> \n";
  echo "</td> \n";
  echo "</tr> \n";
echo "</table> \n";
echo"<br>";
   
    }
	}

	//This counts the number or results - and if there wasn't any it gives them a little message explaining that
	$anymatches=mysql_num_rows($data);
	if ($anymatches == 0)
	{
	echo "Sorry, but we can not finda an entry to match your query<br><br>";
	}


?>

<?php

    if($page != 1){ 
        $pageprev = $page-1;
         


        echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV" .$limit."</a> "); 

    }else{
        echo("PREV" . $limit." ");
    }

    $numofpages = $totalrows / $limit; 
    

    for($i = 1; $i <= $numofpages; $i+1){
        if($i == $page){
            echo($i." ");
        }else{
            echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");

       }
    }


    if(($totalrows % $limit) != 0){
        if($i == $page){
            echo($i." ");
        }else{
           											echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");

        }
    }

   if(($totalrows - ($limit * $page)) > 0){
        $pagenext = $page+1;
         
       												 echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT" .$limit."</a>"); 
    }else{
       												 echo("NEXT" . $limit); 
	$pagenext = $page+2;
	echo("<a href=\"$PHP_SELF?page=$pagenext&field=" . $_GET['field'] . "&finda=" . $_GET['finda'] . "\">NEXT" .$limit."</a>"); 

    } 
?>		

Browser points to number 1 still, and doesnt return any results even though i know there are more records! 

Any help is so greatly appreciated

Thank You 

Tom 

Try this...

 

<?php



    $limit          = 2;               
    $query_count    = "SELECT count(*) FROM teachersname";    
    $result_count   = mysql_query($query_count);    
    $totalrows      = mysql_num_rows($result_count); 

$page = trim(addslashes(strip_tags($_GET['page'])));

if($page == "" OR !is_numeric($page))
unset($page);

    if(empty($page)){
        $page = 1;
    }
        

    $limitvalue = $page * $limit - $limit; 
    $query  = "SELECT * FROM teachersname WHERE " . $_GET['field'] . " LIKE '%" . $_GET['finda'] . "%' LIMIT $limitvalue, $limit";     
//	echo $query;  
    $result = mysql_query($query) or die("Error: " . mysql_error()); 

    if(mysql_num_rows($result) == 0){
        echo("Nothing to Display!");
    }

//check if the user entered data in the form
if (isset($_GET['finda'])) {
//data has been entered so lets search the mofo

echo "<h2>Results</h2><p>";
	// sanitise the data
	$find = trim(strip_tags(strtoupper($_GET['finda'])));
	//connect to the db
	//mysql_connect("localhost", "root", "winn3rs") or die(mysql_error());
	//mysql_select_db("laptop_loan_database") or die(mysql_error());
	//Now we search for our search term, in the field the user specified
//	$query = "SELECT * FROM teachersname WHERE " . $_GET['field'] . " LIKE '%$find%'"  ;
	$data = mysql_query($query);	

       	//And we display the results
	while($result = mysql_fetch_array( $data ))
	{

	echo"<table width=\"200\" border=\"0\" class=\"border_bottom\"> \n";
	echo "<tr> \n";
  
  echo "<td> </td> \n";
  echo "</td> \n";
  echo "</tr> \n";
  echo "<tr> \n";
    echo "<td>";	echo "<strong>Client Name</strong>"; echo "</td>"; 
    echo "<td width=\"75\" style =\"text-align: left\""; ?> <em><a href="search_client_details.php?recordID=<?php echo $result['Client']; ?>"><?php echo $result['Client']; ?> </a></em> <?php echo " </td> \n"; 
  echo "</tr> \n";
  echo "<tr> \n";
  
  		echo "<td>";	echo "<strong>Department Code</strong>"; echo "</td>";
       echo "<td>";	echo "<em>"; 	echo $result['DepartmentCode']; echo "</em>"; echo "</td>";
    
  echo "</tr> \n";

  echo "<tr> \n";
  echo "<td> </td> \n";
  echo "</td> \n";
  echo "</tr> \n";
echo "</table> \n";
echo"<br>";
   
    }
	}

	//This counts the number or results - and if there wasn't any it gives them a little message explaining that
	$anymatches=mysql_num_rows($data);

	if ($anymatches == 0)
		echo "Sorry, but we can not finda an entry to match your query<br><br>";

    if($page > 1){ 
        $pageprev = $page-1;

        echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV" .$limit."</a> "); 

    }else{
        echo("PREV" . $limit." ");
    }

    $numofpages = $totalrows / $limit; 
    

    for($i = 1; $i <= $numofpages; $i++){
        if($i == $page){
            echo($i." ");
        }else{
            echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");

       }
    }


/*
I'm not sure what this is for....

    if(($totalrows % $limit) != 0){
        if($i == $page){
            echo($i." ");
        }else{
           echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");	
        }
    }
*/

   if(($totalrows - $limit * $page) > 0){
        $pagenext = $page+1;
         
       	echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT" .$limit."</a>"); 
    }else{
       												 echo("NEXT" . $limit); 
	$pagenext = $page+2;
	echo("<a href=\"$PHP_SELF?page=$pagenext&field=" . $_GET['field'] . "&finda=" . $_GET['finda'] . "\">NEXT" .$limit."</a>"); 

    } 
?>

 

I also want to note that using the $i++ in the for loop is ok ;-)

try

<?php
//conect to db
$limit = 2;
$fields = array("laptopname", "statusID");
if (isset($_GET['search']) and $_GET['search'] == 'Search'){
$page = trim(addslashes(strip_tags($_GET['page'])));
if($page == "" OR !is_numeric($page)) $page = 1;
$find = strtoupper($_GET['finda']);
$find = strip_tags($_GET['finda']);
$find = trim ($_GET['finda']);
$find = mysql_real_escape_string($find);
if (in_array($_GET['field'], $fields)) $field = $_GET['field']; else die('wrong field name');
if ($find) $where = ' WHERE '.$field." LIKE '%".$find."%'"; else $where = ' WHERE 1';
$lm = ' LIMIT '.(($page - 1) * $limit).' '.$limit;
$qve_tot_row = "SELECT count(*) FROM teachersname".$where;
$qve_data = "SELECT count(*) FROM teachersname".$where.$lm;
$res = mysql_query($qve_tot_row);
$tot_row = mysql_result($res, 0, 0);
$tot_pages = ceil($tot_row / $limit);
$res =mysql_query($qve_data);
if (mysql_num_rows($res) > 0) {
	while($result = mysql_fetch_array($res)) {
		echo"<table width=\"200\" border=\"0\" class=\"border_bottom\"> \n";
		echo "<tr> \n";
		echo "<td> </td> \n";
		echo "</td> \n";
		echo "</tr> \n";
		echo "<tr> \n";
		echo "<td>";
		echo "<strong>Client Name</strong>"; 
		echo "</td>";
		echo "<td width=\"75\" style =\"text-align: left\""; ?> <em><a href="search_client_details.php?recordID=<?php echo $result['Client']; ?>"><?php 
		echo $result['Client']; ?> </a></em> <?php 
		echo " </td> \n";
		echo "</tr> \n";
		echo "<tr> \n";
		echo "<td>";
		echo "<strong>Department Code</strong>";
		echo "</td>";
		echo "<td>";
		echo "<em>";
		echo $result['DepartmentCode'];
		echo "</em>";
		echo "</td>";
		echo "</tr> \n";
		echo "<tr> \n";
		echo "<td> </td> \n";
		echo "</td> \n";
		echo "</tr> \n";
		echo "</table> \n";
		echo"<br>";
	}
} else echo 'no result';
if ($page > 1) echo '<a href="?finda='.$find.'&field='.$field.'&page='.($page - 1).'"> PREV </a>';
else echo ' PREV ';
for ($i =1; $i <= $tot_pages; $i++) {
	if ($i != $page) echo '<a href="?finda='.$find.'&field='.$field.'&page='.$i.'"> '.$i.' </a>';
	else echo " $i ";
}
if ($page < $tot_pages) echo '<a href="?finda='.$find.'&field='.$field.'&page='.($page + 1).'"> NEXT </a>';
else echo ' NEXT ';
} else die('error');
?>

Hi,

 

Firstly, thanks ever so much for your help, your really helping me out of a rather deep hole.

 

I hasve used the first bit of code you said to try and it works perfectly when navigating to "Next Page". I notice that the browser is still pointing at whatever was entered into search field.

 

The only problem i have now is that the "previous Page" hyperlink doesnt work and looking at what the browser is pointing at is number 2...NOT the search details that the user entered. When clicked i get a page cannot be displayed??

 

Whats the deal with the "Previous Page" bit? We are so close now dont give up on me :)

 

Thank You kindly

 

Tom

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.