Search +Paginate results
Posted: Mon Feb 28, 2005 2:39 pm
Hi,
Im trying to search a table and bring back paginated results .. Here is the code I have been playing with .. but have had no luck in gettin it to work ..
What am I missing out?
Thanks
Im trying to search a table and bring back paginated results .. Here is the code I have been playing with .. but have had no luck in gettin it to work ..
Code: Select all
<form name="form" method="post" action="search.php">
<input type="text" name="textfield">
<input type="submit" name="Submit" value="Search">
</form>
<?php
// Database Connection
require 'db.php';
// If current page number, use it
// if not, set one!
if(!isset($_GETї'page'])){
$page = 1;
} else {
$page = $_GETї'page'];
}
// Define the number of results per page
$max_results = 1;
// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);
// Perform MySQL query on only the current page number's results
$sql = mysql_query("SELECT * FROM users WHERE (user_name LIKE('$search%') OR first_name LIKE('%$search%')) ORDER BY first_name LIMIT $from, $max_results");
while($row = mysql_fetch_array($sql)){
$user_name = $rowї"user_name"];
$first_name = $rowї"first_name"];
echo "<table width="100%" height="" border="1" align="center" cellpadding="5" cellspacing="1" bordercolor="#000000">
<tr>
<td width="164" rowspan="4"> <div align="center"><font size="1" face="verdana">$picture</font></div></td>
</tr>
<tr>
<td width="189" height="20" div align="center">
<div align="left"><font size="1" face="verdana">$user_name</font></div></td>
<td height="20" align="center" div>
<div align="left"><font color="#FF0000" size="1" face="verdana">$first_name $last_name </font></div></td>
</tr>
</table>
<br>";
}
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM users"),0);
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);
/* Build Page Number Hyperlinks
Build Previous Link */
if($page > 1){
$prev = ($page - 1);
echo "<a href="".$_SERVERї'PHP_SELF']."?page=$prev"><<Previous</a> ";
}
for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "$i ";
} else {
echo "<a href="".$_SERVERї'PHP_SELF']."?page=$i">$i</a> ";
}
}
// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
echo "<a href="".$_SERVERї'PHP_SELF']."?page=$next">Next>></a>";
}
echo "</center>";
?>Thanks