Help...adding pagination to results with search function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ThomasWillow
Forum Newbie
Posts: 2
Joined: Tue Mar 10, 2009 2:34 pm

Help...adding pagination to results with search function

Post by ThomasWillow »

Hello, I have made a page that pulls results from a database and added a simple search function to it which works fine. Although I was able to code that myself I wanted to add pagination to the page which was beyond my knowledge.
I went online and looked through a bunch of tutorials and manage to get one working, although I can't seem to get it working with the search function properly. When I do a search the first page comes out fine but when I click the next button the next pages just seem to grab everything from the database and not just what I was searching for. I have been working with this script for a few days now, any help with this script would be much appreciated. Or if you think I am going about it all wrong please let me know. Here is what I have.


<?php
// Connect to the database server
$dbcnx = @mysql_connect('loc', 'user', 'password');

if (!$dbcnx) {
exit('<p>Search for Clients.</p>');
}
// Select the clients database

if (!@mysql_select_db('mydatabase')) {
exit('<p>Unable to locate the Client list ' .
'database at this time.</p>');
}

//select all the clients records
$req_limit = mysql_query("SELECT id,age,name,height,weight,hair,eyes,stats,location,travel,datejoined,ethnic,services FROM Mable WHERE location LIKE '%$location%' and services LIKE '%$services%'");

$result = mysql_numrows($req_limit);
$page_limit = '2';
$page_number = $result / $page_limit;
$total_number = ceil($page_number);
$number = $total_number - 1;
if(isset($_GET['page_number']) || $_GET['page_number'] != '0' )

{
$mysql_limit = $page_limit * $_GET['page_number'];
}
else{

$mysql_limit = '0';
}

?>

</head>

<body>
<div id='wrapper'>
<div id='containersubmenu'><div class='container'><img src='images/header/common/banner02a.jpg' name='escorts'/></div>
<div id='submenu'>
<div class='submenuheader'>SEARCH FUNCTION</div>

<form method='post' action="temp.php">
<span class='submenuoptionheader'>Choose a City</span>
<select name="location" id="city" size="0">
<option value="">All Cities </option>
<option value="toronto">Toronto </option>
<option value="vancover" >Vancover </option>
</select>
<span class='submenuoptionheader'>Services</span>
<select name="services" id="city" size="0">
<option value="">All Services </option>
<option value="management">management </option>
<option value="generalhelp" >help</option>
<option value="parttime" >part time </option>
</select>

<br /><br />

<input type="Submit" id="searchbutton" name="Submit" value="Search" />
</form>


<?php
// next and pre links
if( $number != '0' && empty($_GET['page_number']))
{
print '<a href="browsetemp.php?page_number=1">Next page</a>';
}
elseif($number !='0' && isset($_GET['page_number']) && $_GET['page_number'] < $number)
{
$next = $_GET['page_number'] + 1;
print '<a href="browsetemp.php?page_number='.$next.'">next page</a>';
print '&nbsp;&nbsp;<a href="javascript: history.back();">Previous page</a>';
}

elseif( $number !='0' && isset($_GET['page_number']) && $_GET['page_number'] >= $number )
{
print '<a href="javascript: history.back();">Previous page</a>';
}

?>

</div>
<div class='containerfeatureboxmain'>
<!--this is the start or the feature escorts -->
<?php

$result = mysql_query("SELECT id,age,name,height,weight,hair,eyes,stats,location,travel,datejoined,ethnic,services FROM mytable WHERE location LIKE '%$location%' and services LIKE '%$services%' ORDER by name ASC Limit $mysql_limit , $page_limit") or die ("erreur requte");

$display = 1;
$cols = 0;
echo "<table border='0' cellpadding='0' cellspacing='0' bordercolor='red'>";

while($info=mysql_fetch_array($result))
{

$id = $info['id'];
$name = $info['name'];
$age = $info['age'];
$height = $info['height'];
$weight = $info['weight'];
$hair = $info['hair'];
$eyes = $info['eyes'];
$stats = $info['stats'];
$ethnic = $info['ethnic'];
$location = $info['location'];
$travel = $info['travel'];
$services = $info['services'];

// Display the employees

if($cols == 0){
echo "<tr>\n";
}

// put what you would like to display within each cell here
echo "<td valign='top'>

My results Go here


</td>\n";
$cols++;
if($cols == $display){
echo "</tr>\n";
$cols = 0;
}
}
// added the following so it would display the correct html
if($cols != $display && $cols != 0){
$neededtds = $display - $cols;
for($i=0;$i<$neededtds;$i++){
echo "<td></td>\n";
}
echo "</tr></table>";
} else {
echo "</table>";


}


?>


Thanks
John
ThomasWillow
Forum Newbie
Posts: 2
Joined: Tue Mar 10, 2009 2:34 pm

Re: Help...adding pagination to results with search function

Post by ThomasWillow »

ahh the problem seems to lie in the links them self

<a href="browsetemp.php?page_number=1">Next page</a>

I believe I just have to pass the search var in the links.
I think I can get this to work now

yup, seems all I had to do was:
print '<a href="browsetemp.php?page_number=1&location='.$location.'&services='.$services.'">Next page</a>'

now can anyone help me add in number links for the result pages :)
Post Reply