Jump to content

Please help with PHP/Mysql multi page query


newjaguar

Recommended Posts

I am very very new to php and mysql, but some how I manage to piece together this:

I have simple phone db. I can run the search with last name and results are fine.

The PROBLEM I have is with multi page, when I go to next page, it includes all the db, and it should be just that last name.

Please see for yourself: www.monkeywindow.com/try/search.html

You can use last names: adam, baker, carter for testing.

 

Please help me fix this.

Thanks for your help in advance

 

Here is my code:

<?php
//

$search=$_POST["Last_Name"];
$search1=$_POST["First_Name"];
//
// how many rows to show per page
$rowsPerPage = 5;

// by default we show first page
$pageNum = 1;

// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}

// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;

// 

//

// Retrieve all the data from the "example" table

$result = mysql_query("SELECT * FROM pbook WHERE last LIKE '%$search%' AND first LIKE '%$search1%' LIMIT $offset, $rowsPerPage ")or die(mysql_error());
//

//echo "<table border='1'>";
//echo "<tr> <th>Last</th> <th>First</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>"; 
echo $row['last'];
echo "</td><td>"; 
echo $row['first'];
echo "</td><td>"; 
echo $row['phone'];
echo "</td></tr>"; 
}
//
// how many rows we have in database
$numresults = mysql_query("SELECT * FROM pbook WHERE last LIKE '%$search%' AND first LIKE '%$search1%'")or die(mysql_error());
$numrows = mysql_num_rows($numresults); // Number of rows returned from above query.
//$query   = "SELECT * FROM pbook WHERE last LIKE '%$search%' AND first LIKE '%$search1%' AS numrows FROM pbook";
//$result  = mysql_query($query) or die('Error, query failed');
//$row     = mysql_fetch_array($result, MYSQL_ASSOC);
//$numrows = $row['numrows'];

// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);

$self = $_SERVER['PHP_SELF'];

// creating 'previous' and 'next' link
// plus 'first page' and 'last page' link

// print 'previous' link only if we're not
// on page one
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";

$first = " <a href=\"$self?page=1\">[First Page]</a> ";
} 
else
{
$prev  = ' [Prev] ';       // we're on page one, don't enable 'previous' link
$first = ' [First Page] '; // nor 'first page' link
}

// print 'next' link only if we're not
// on the last page
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"$self?page=$page\">[Next]</a> ";

$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
} 
else
{
$next = ' [Next] ';      // we're on the last page, don't enable 'next' link
$last = ' [Last Page] '; // nor 'last page' link
}

// print the page navigation link
echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> 

pages " . $next . $last;
//

//
?> 

I think the problem is that when you click "Next" it is not passing the search variable though e.g. Baker.

 

What you should try:

 

Change this:

	$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";

$first = " <a href=\"$self?page=1\">[First Page]</a> ";

 

to this:

	$prev = " <a href=\"$self?page=$page&fname=$search&lname=$search1\">[Prev]</a> ";

$first = " <a href=\"$self?page=1&fname=$search&lname=$search1\">[First Page]</a> ";

 

That will pass the variables to the next page.

Then you need to pick them up.

 

So try changing this:

$search=$_POST["Last_Name"];
$search1=$_POST["First_Name"];

 

to this:

$search=stripslashes($_POST["Last_Name"]);
$search1=stripslashes($_POST["First_Name"]);

if ($search == '' && $search1 == '') {

$search=stripslashes($_GET["fname"]);
$search1=stripslashes($_GET["lname"]);

}

Remember to stripslashes to avoid security breaches :-)

 

Hopefully this will send the variables to the next page when you click next/prev and the script will pick them up if the original variables are not set :-)

 

 

Thank you very much page links work :-)

 

BUT I still get ALL the results back. i.e if I put carter in last name search it shows baker and adam as well. e.g carter search "Showing page 1 of 4 pages" BUT when hit next "Showing page 2 of 13 pages".

 

It should be ONLY that last name and if first name was entered ONLY that last and first.

 

Please have a look again and please please fix any other errors or security things as well.

 

I really appreciate your help. Thanks :-)

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.