Jump to content

Adding pagination to results with search function


angryjohnny

Recommended Posts

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 '  <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

Link to comment
Share on other sites

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.