| | |
PHP Search pagination problem
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Aug 2005
Posts: 17
Reputation:
Solved Threads: 0
Hi I've been trying to make pagination for my search results and I have this problem: The first page displays ok but when the max result per page is achieved like $max_results = 2 and it reached 2 the links for the other pages shows but when clicked on it returns nothing and when I go back to page one nothing shows up either can someone help me out? My code is as follow:
[PHP]
<?php
// Database Connection
include 'ccm_chat/database/database.inc';
// 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 = 10;
// 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 poc_user_data WHERE GENDER LIKE '$gender' AND AGE>='$agemin' AND AGE<='$agemax' AND INTERESTS LIKE '%$interest%' LIMIT $from, $max_results") or die(mysql_error());
while($row = mysql_fetch_array($sql)){
// Build your formatted results here.
echo '<p><table width="480" border="1" cellpadding="0" cellspacing="5" bordercolor="3a8ab6" bgcolor="dee7ed">';
echo '<tr>';
if(!$row['PICTURE_URL']){
echo '<td width="200" rowspan="4" align="center" class="profiletext">No Picture Available </td>';
}else{
echo '<td width="200" rowspan="4" align="center" class="profiletext"><img src="'.$row['PICTURE_URL'].'"></td>';
}
echo '<td width="280" class="profiletext">User name: '.$row['USER'].'</td>';
echo '</tr>';
echo '<tr>';
echo '<td class="profiletext">Name: '.$row['NAME'].'</td>';
echo '</tr>';
echo '<tr>';
echo '<td class="profiletext">Age: '.$row['AGE'].'</td>';
echo '</tr>';
echo '<tr>';
echo '<td class="profiletext">Gender: '.$row['GENDER'].'</td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="2" class="profiletext">Interests: </br>'.$row['INTERESTS'].'</p></td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="2" class="profiletext">Motto: '.$row['MOTTO'].'</td>';
echo '</tr>';
echo '</table>';
}
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM poc_user_data WHERE GENDER LIKE '$gender' AND AGE>='$agemin' AND AGE<='$agemax' AND INTERESTS LIKE '%$interest%'"),0);
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);
// Build Page Number Hyperlinks
echo '<p class="text"><center>Select a Page<br />';
// 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>";
?>
[/PHP]
[PHP]
<?php
// Database Connection
include 'ccm_chat/database/database.inc';
// 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 = 10;
// 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 poc_user_data WHERE GENDER LIKE '$gender' AND AGE>='$agemin' AND AGE<='$agemax' AND INTERESTS LIKE '%$interest%' LIMIT $from, $max_results") or die(mysql_error());
while($row = mysql_fetch_array($sql)){
// Build your formatted results here.
echo '<p><table width="480" border="1" cellpadding="0" cellspacing="5" bordercolor="3a8ab6" bgcolor="dee7ed">';
echo '<tr>';
if(!$row['PICTURE_URL']){
echo '<td width="200" rowspan="4" align="center" class="profiletext">No Picture Available </td>';
}else{
echo '<td width="200" rowspan="4" align="center" class="profiletext"><img src="'.$row['PICTURE_URL'].'"></td>';
}
echo '<td width="280" class="profiletext">User name: '.$row['USER'].'</td>';
echo '</tr>';
echo '<tr>';
echo '<td class="profiletext">Name: '.$row['NAME'].'</td>';
echo '</tr>';
echo '<tr>';
echo '<td class="profiletext">Age: '.$row['AGE'].'</td>';
echo '</tr>';
echo '<tr>';
echo '<td class="profiletext">Gender: '.$row['GENDER'].'</td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="2" class="profiletext">Interests: </br>'.$row['INTERESTS'].'</p></td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="2" class="profiletext">Motto: '.$row['MOTTO'].'</td>';
echo '</tr>';
echo '</table>';
}
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM poc_user_data WHERE GENDER LIKE '$gender' AND AGE>='$agemin' AND AGE<='$agemax' AND INTERESTS LIKE '%$interest%'"),0);
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);
// Build Page Number Hyperlinks
echo '<p class="text"><center>Select a Page<br />';
// 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>";
?>
[/PHP]
•
•
Join Date: Sep 2005
Posts: 51
Reputation:
Solved Threads: 1
Hi,
I had done pagination before, and my code has the same structure and i think the problem could be due to the pagination section ie:
instead of :
"// Build Previous Link
if($page > 1){
$prev = ($page - 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> ";
}
"
you could do this:
"// Build Previous Link
if($page > 1){
$prev = ($page - 1);
$words=$_GET['Query'];
$type=$_GET['SearchView'];
$search_type=$_GET['SearchType'];
$max_results=20;
$sort=$_GET['Sort'];
echo "<a href=search_pagin.php?page=$prev&Query=$words&SearchView=$type&$max_results&Sort=$sort&SearchType=$search_type> previous < </a> ";
} "
do note
words,$type, etc are only variables(which i extract the code from my own program of course), these variables are passsed on to the next page or link
instead of :
"for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "$i ";
} else {
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
}
}
"
you could do this:
"for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "$i ";
} else {
$words=$_GET['Query'];
$type=$_GET['SearchView'];
$search_type=$_GET['SearchType'];
$max_results=20;
$sort=$_GET['Sort'];
echo "<a href=search_pagin.php?page=$i&Query=$words&SearchView=$type&$max_results&Sort=$sort&SearchType=$search_type>$i</a> ";
}
} "
and instead of :
"// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
}
echo "</center>";
?> "
you could do this:
"// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
$words=$_GET['Query'];
$type=$_GET['SearchView'];
$search_type=$_GET['SearchType'];
$max_results=20;
$sort=$_GET['Sort'];
echo "<a href=search_pagin.php?page=$next&Query=$words&SearchView=$type&$max_results&Sort=$sort&SearchType=$search_type> > next20 </a>";
} "
it sounds lengthy, but i hope it helps!!
I had done pagination before, and my code has the same structure and i think the problem could be due to the pagination section ie:
instead of :
"// Build Previous Link
if($page > 1){
$prev = ($page - 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> ";
}
"
you could do this:
"// Build Previous Link
if($page > 1){
$prev = ($page - 1);
$words=$_GET['Query'];
$type=$_GET['SearchView'];
$search_type=$_GET['SearchType'];
$max_results=20;
$sort=$_GET['Sort'];
echo "<a href=search_pagin.php?page=$prev&Query=$words&SearchView=$type&$max_results&Sort=$sort&SearchType=$search_type> previous < </a> ";
} "
do note
words,$type, etc are only variables(which i extract the code from my own program of course), these variables are passsed on to the next page or linkinstead of :
"for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "$i ";
} else {
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
}
}
"
you could do this:
"for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "$i ";
} else {
$words=$_GET['Query'];
$type=$_GET['SearchView'];
$search_type=$_GET['SearchType'];
$max_results=20;
$sort=$_GET['Sort'];
echo "<a href=search_pagin.php?page=$i&Query=$words&SearchView=$type&$max_results&Sort=$sort&SearchType=$search_type>$i</a> ";
}
} "
and instead of :
"// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
}
echo "</center>";
?> "
you could do this:
"// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
$words=$_GET['Query'];
$type=$_GET['SearchView'];
$search_type=$_GET['SearchType'];
$max_results=20;
$sort=$_GET['Sort'];
echo "<a href=search_pagin.php?page=$next&Query=$words&SearchView=$type&$max_results&Sort=$sort&SearchType=$search_type> > next20 </a>";
} "
it sounds lengthy, but i hope it helps!!
![]() |
Similar Threads
- php drop down menu to search multiple sql tables (PHP)
- creating an advanced php search (PHP)
- Store multiple selection from pagination info into single array (PHP)
- pagination and search (PHP)
- pagination problem (PHP)
- PHP sessions/$_POST problem. Too Stressed today! (PHP)
- search and results problem (PHP)
- Using Search Engine Friendly PHP URLs (PHP)
Other Threads in the PHP Forum
- Previous Thread: need help in installing gd library with php 4.3.10
- Next Thread: PHP index question
| Thread Tools | Search this Thread |
apache api array beginner binary broken cache cakephp checkbox class cms code confirm cron curl customizableitems database date display dynamic echo email error external fcc file files folder form forms forum freelancing function functions google header headmethod howtowriteathesis href htaccess html iframe image include insert ip javascript joomla limit link login mail malfunction menu method mlm mod_rewrite multiple mysql neutrality oop pageing pagerank paypal pdf php phpmysql play problem query question radio random recursion remote root script search select server sessions sms soap source space sql support! syntax system table template tutorial update upload url validator variable video web youtube





