| | |
Pagination - not displaying results properly, please help!
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Apr 2006
Posts: 4
Reputation:
Solved Threads: 0
Hi everyone!
I am desperate to find an answer to this problem. I have read through (and tried to use) anout 6 different tutorials, but I just cannot fix this. It would mean so much to me if someone could tell me what I am doing wrong, my brain is really starting to hurt over this!!
I have a search (for realestate), that results in all the properties for the search criteria to show up (eg state, price etc etc). I wanted to limit the number of properties per page to 3, and to have links saying, "next 1, 2, 3, 4" etc etc. Now I know it has something to do with limits, but I can't get it to work.
If I put $limit = $maxRows_p it says:
'showing results 1 - 3 of 3'. But there are actually 16 results available. When I Take out the $limit I get:
the correct 'showing 1 - 3 of 16' but all the results are on the first page. (doesn't limit it to 3).
And the:
$from = (($pageNum_p * $maxRows_p) - $maxRows_p);
$sql = 'SELECT * FROM items LIMIT $from, $maxRows_p';
Doesn't seem to do anything (works the same if I take it out).
Please help! I have been trying to do this for 12+ hours. Thanks Michelle.
Code:
#### BUILD SEARCH SQL BASED ON SEARCH TYPE ####
#defauts
$maxRows_p = 3;
if(!isset($_GET['pageNum_p'])){
$pageNum_p = 1;
} else {
$pageNum_p = $_GET['pageNum_p'];
}
$startRow_p = (($pageNum_p * $maxRows_p) - $maxRows_p);
$from = (($pageNum_p * $maxRows_p) - $maxRows_p);
$sql = 'SELECT * FROM items LIMIT $from, $maxRows_p';
## Start building sql for GET varables for advanced search
//Add min square feet
if(isset($_REQUEST['sqft']) && ($_REQUEST['sqft'] != ''))
$search[] = ' sqft >= '.$_REQUEST['sqft'];
//Add Garage
if(isset($_REQUEST['garage']) && ($_REQUEST['garage'] != ''))
$search[] = ' garage = "'.$_REQUEST['garage'].'"';
//Add lot size
if(isset($_REQUEST['lot_size']) && ($_REQUEST['lot_size'] != ''))
$search[] = ' lot_size >= '.$_REQUEST['lot_size'];
//implode to search string on ' and ';
$searchStr = @implode(' and ',$search);
$sql = 'select * FROM items WHERE (expires > NOW()) and active = "Yes" and ';
$sql .= $searchStr;
### DEBUG
if($debugP) echo 'Advanced Search Sql<hr>'.$sql;
$error['Results'] = 'No results found, please search again';
### Finished Building search sql and execting #####
$sql .= $sort;
//Perform search
$searchResults = $mysql->exSql($sql);
### BUILD OUTPUT ####
if (isset($_GET['totalRows_p'])) {
$totalRows_p = $_GET['totalRows_p'];
} else {
$all_p = mysql_query($sql);
$totalRows_p = @mysql_num_rows($all_p);
}
$totalPages_p = ceil($totalRows_p/$maxRows_p)-1;
// Build Page Number Hyperlinks
echo "<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 <= $totalPages_p; $i++){
if(($pageNum_p) == $i){
echo "$i ";
} else {
echo "<a href=\"".$_SERVER['PHP_SELF']."?pageNum_p=$i\">$i</a> ";
}
}
// Build Next Link
if($page < $totalPages_p){
$next = ($pageNum_p + 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?pageNum_p=$next\".>Next>></a>";
}
echo "</center>";
?>
THANK-YOU,
Michelle.
I am desperate to find an answer to this problem. I have read through (and tried to use) anout 6 different tutorials, but I just cannot fix this. It would mean so much to me if someone could tell me what I am doing wrong, my brain is really starting to hurt over this!!
I have a search (for realestate), that results in all the properties for the search criteria to show up (eg state, price etc etc). I wanted to limit the number of properties per page to 3, and to have links saying, "next 1, 2, 3, 4" etc etc. Now I know it has something to do with limits, but I can't get it to work.
If I put $limit = $maxRows_p it says:
'showing results 1 - 3 of 3'. But there are actually 16 results available. When I Take out the $limit I get:
the correct 'showing 1 - 3 of 16' but all the results are on the first page. (doesn't limit it to 3).
And the:
$from = (($pageNum_p * $maxRows_p) - $maxRows_p);
$sql = 'SELECT * FROM items LIMIT $from, $maxRows_p';
Doesn't seem to do anything (works the same if I take it out).
Please help! I have been trying to do this for 12+ hours. Thanks Michelle.
Code:
#### BUILD SEARCH SQL BASED ON SEARCH TYPE ####
#defauts
$maxRows_p = 3;
if(!isset($_GET['pageNum_p'])){
$pageNum_p = 1;
} else {
$pageNum_p = $_GET['pageNum_p'];
}
$startRow_p = (($pageNum_p * $maxRows_p) - $maxRows_p);
$from = (($pageNum_p * $maxRows_p) - $maxRows_p);
$sql = 'SELECT * FROM items LIMIT $from, $maxRows_p';
## Start building sql for GET varables for advanced search
//Add min square feet
if(isset($_REQUEST['sqft']) && ($_REQUEST['sqft'] != ''))
$search[] = ' sqft >= '.$_REQUEST['sqft'];
//Add Garage
if(isset($_REQUEST['garage']) && ($_REQUEST['garage'] != ''))
$search[] = ' garage = "'.$_REQUEST['garage'].'"';
//Add lot size
if(isset($_REQUEST['lot_size']) && ($_REQUEST['lot_size'] != ''))
$search[] = ' lot_size >= '.$_REQUEST['lot_size'];
//implode to search string on ' and ';
$searchStr = @implode(' and ',$search);
$sql = 'select * FROM items WHERE (expires > NOW()) and active = "Yes" and ';
$sql .= $searchStr;
### DEBUG
if($debugP) echo 'Advanced Search Sql<hr>'.$sql;
$error['Results'] = 'No results found, please search again';
### Finished Building search sql and execting #####
$sql .= $sort;
//Perform search
$searchResults = $mysql->exSql($sql);
### BUILD OUTPUT ####
if (isset($_GET['totalRows_p'])) {
$totalRows_p = $_GET['totalRows_p'];
} else {
$all_p = mysql_query($sql);
$totalRows_p = @mysql_num_rows($all_p);
}
$totalPages_p = ceil($totalRows_p/$maxRows_p)-1;
// Build Page Number Hyperlinks
echo "<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 <= $totalPages_p; $i++){
if(($pageNum_p) == $i){
echo "$i ";
} else {
echo "<a href=\"".$_SERVER['PHP_SELF']."?pageNum_p=$i\">$i</a> ";
}
}
// Build Next Link
if($page < $totalPages_p){
$next = ($pageNum_p + 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?pageNum_p=$next\".>Next>></a>";
}
echo "</center>";
?>
THANK-YOU,
Michelle.
•
•
Join Date: Mar 2004
Posts: 763
Reputation:
Solved Threads: 38
Take a look at this example, it might help you see what you're doing wrong. I'd look through your code, but I dont feel like getting a headache staring at unformatted code. Use code tags next time.
PHP Syntax (Toggle Plain Text)
//determine number of rows from search result $row_count = //whatever //how many pages can the result be broken into, //given the row count allowed per page $page_count = ceil($row_count / $rows_per_page); //page to display $page = 1; //rows to display that correspond to that page $start = $page * $rows_per_page; $end = ($page+1) * $rows_per_page; //make sure we dont display more than whats available if ($end > $row_count) $end = $row_count; //display the rows for the specified page for ($i=$start; $i<$end; $i++) { echo "Row: ".$i; } //out put the links to all available pages for ($j=1; $j<=$page_count; $j++) echo "Page ".$j." link";
•
•
Join Date: Apr 2006
Posts: 4
Reputation:
Solved Threads: 0
Hi,
Thanks so much for your response, this issue has been causing me grief for days now. What do you mean by code tags? Please let me know and I'll try to fix it up. I have had a look at your example, and tried to use some stuff from it instead, but I still come up with the same issues.
I've looked through heaps of tutes, but no matter what I try it doesn't seem to work. What I have come up with is truly the best I can do
If you could help me out my looking at my code, I would truly, truly appreciate it :o
Thank-you,
Michelle
Thanks so much for your response, this issue has been causing me grief for days now. What do you mean by code tags? Please let me know and I'll try to fix it up. I have had a look at your example, and tried to use some stuff from it instead, but I still come up with the same issues.
I've looked through heaps of tutes, but no matter what I try it doesn't seem to work. What I have come up with is truly the best I can do
If you could help me out my looking at my code, I would truly, truly appreciate it :o
Thank-you,
Michelle
•
•
Join Date: Apr 2006
Posts: 4
Reputation:
Solved Threads: 0
Hi again,
Ok I have attempted
the code you sent above, and I also couldn't get it to work. Now, I am getting all the results showing on the first page, a next link that doesn't work and it says, " showing 1 of listings"
//set rows per page, page to display and rows to display that correspond to that page
$rows_per_page = 3;
if(!isset($_GET['page'])){
$page;
} else {
$page = $_GET['page'];
}
$start = $page * $rows_per_page;
$end = ($page+1) * $rows_per_page;
//make sure we dont display more than whats available
if ($end > $row_count)
$end = $row_count;
//determine number of rows from search result
if (isset($_GET['row_count'])) {
$row_count = $_GET['row_count'];
} else {
$all = mysql_query($sql);
$row_count = @mysql_num_rows($all);
}
$page_count = ceil($row_count/$rows_per_page)-1;
## Start building sql for GET varables for advanced search
//Add Price From
if(isset($_REQUEST['pfrom']) && ($_REQUEST['pfrom'] != '-1'))
$search[] = ' price >= '.$_REQUEST['pfrom'];
//Add Max Price
if(isset($_REQUEST['pto']) && ($_REQUEST['pto'] != '-1'))
$search[] = ' price <= '.$_REQUEST['pto'];
//Add Property Type
if(isset($_REQUEST['category']) && ($_REQUEST['category'] != ''))
$search[] = ' cid = '.$_REQUEST['category'];
//Add Property Style Type
if(isset($_REQUEST['style']) && ($_REQUEST['style'] != ''))
$search[] = ' styleID = '.$_REQUEST['style'];
//Add Min Bed
if(isset($_REQUEST['bed']) && ($_REQUEST['bed'] != ''))
$search[] =' bed >= "'.$_REQUEST['bed'].'"';
//Add Min Bath
if(isset($_REQUEST['bath']) && ($_REQUEST['bath'] != ''))
$search[] = ' bath >= "'.$_REQUEST['bath'].'"';
//Add city
if(isset($_REQUEST['city']) && ($_REQUEST['city'] != ''))
$search[] = ' city = "'.$_REQUEST['city'].'"';
//Add State
if(isset($_REQUEST['state']) && ($_REQUEST['state'] != ''))
$search[] = ' state = "'.$_REQUEST['state'].'"';
//Add min square feet
if(isset($_REQUEST['sqft']) && ($_REQUEST['sqft'] != ''))
$search[] = ' sqft >= '.$_REQUEST['sqft'];
//Add Garage
if(isset($_REQUEST['garage']) && ($_REQUEST['garage'] != ''))
$search[] = ' garage = "'.$_REQUEST['garage'].'"';
//Add lot size
if(isset($_REQUEST['lot_size']) && ($_REQUEST['lot_size'] != ''))
$search[] = ' lot_size >= '.$_REQUEST['lot_size'];
//implode to search string on ' and ';
$searchStr = @implode(' and ',$search);
$sql = 'select * FROM items WHERE (expires > NOW()) and active = "Yes" and ';
$sql .= $searchStr;
### DEBUG
if($debugP) echo 'Advanced Search Sql<hr>'.$sql;
$error['Results'] = 'No results found, please search again';
//Perform search
$searchResults = $mysql->exSql($sql);
### BUILD OUTPUT ####
//display the rows for the specified page
for ($i=$start; $i<$end; $i++)
{
echo "Row: ".$i;
}
//out put the links to all available pages
for ($j=1; $j<=$page_count; $j++)
echo "Page ".$j." link";
// Build Page Number Hyperlinks
echo "<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 ($j=1; $j<=$page_count; $j++){
if(($page_count) == $j){
echo "$j ";
} else {
echo "<a href=\"".$_SERVER['PHP_SELF']."?pageNum_p=$j\">$j</a> ";
}
}
// Build Next Link
if($page < $page_count){
$next = ($page_count + 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page_count=$next\".>Next>></a>";
}
echo "</center>";
?>
<div align="left" class="locText"><a href="index.php" class="locLink">Home</a> <span class="locArrow"> > </span> Search Results</div>
<hr size="1" color="#666666">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="pageText" >Showing: <strong><?php echo ($start + 1) ?> to <?php echo min($start + $rows_per_page, $row_count) ?> of <?php echo $row_count ?></strong> Listings</td>
<td align="right" class="pageText"></td>
</tr>
</table></td>
</tr>
<tr>
<td height="5"><img src="images/pixel.gif" width="1" height="1" alt=""></td>
</tr>
<tr>
<td><table width="100%" border="0" cellspacing="1" cellpadding="4" class="resBorder">
<tr>
<td class="colText">City</td>
<td class="colText">ST</td>
<td class="colText">Price</td>
<td class="colText">Beds</td>
<td class="colText">Baths</td>
<td class="colText">Sqft</td>
</tr>
<? while($row_p = @mysql_fetch_assoc($searchResults)) { ?>
<tr valign="top">
<td class="rowText"><? echo $row_p['city']; ?></td>
<td class="rowText"><? echo $row_p['state']; ?></td>
<td class="rowText"><? echo Money($row_p['price'],1); ?></td>
<td class="rowText"><? echo $row_p['bed']; ?></td>
<td class="rowText"><? echo $row_p['bath']; ?></td>
<td class="rowText"><? echo $row_p['sqft']; ?></td>
</tr>
<tr valign="top">
<td class="descText" colspan="8"><table border="0" cellspacing="0" cellpadding="4">
<tr valign="top">
<? //fetch photo from database
$sql = "select * from photos where ptid = ". $row_p['id']." and porder = '1' ";
//echo $sql; //debug sql output
$pRS = mysql_query($sql,$myconn) or die(mysql_error());
//fetch assoc array
$row_photo = mysql_fetch_assoc($pRS);
// if image exist
if ( mysql_num_rows($pRS) > 0 )
$imageSRC = $row_photo['location'];
else //no photo
$imageSRC = "noimage.jpg";
?>
<td class="descText"><a href="detail.php?id=<? echo $row_p['id']; ?>"><img src="admin/photos/uploads/small_thumbs/tn_<? echo $imageSRC; ?>" border="0" alt="<? echo $row_photo['caption']; ?>"></a></td>
<td class="descText"><? echo substr($row_p['description'],0,150); ?>...<a href="detail.php?id=<? echo $row_p['id']; ?>" class="rowLink">more..</a><a class="rowLink" href="saveListing.php?id=<? echo $row_p['id']; ?>" title="Click to save listing"><br>
save listing</a></td>
</tr>
</table></td>
</tr>
<? } ?>
Ok I have attempted
the code you sent above, and I also couldn't get it to work. Now, I am getting all the results showing on the first page, a next link that doesn't work and it says, " showing 1 of listings"//set rows per page, page to display and rows to display that correspond to that page
$rows_per_page = 3;
if(!isset($_GET['page'])){
$page;
} else {
$page = $_GET['page'];
}
$start = $page * $rows_per_page;
$end = ($page+1) * $rows_per_page;
//make sure we dont display more than whats available
if ($end > $row_count)
$end = $row_count;
//determine number of rows from search result
if (isset($_GET['row_count'])) {
$row_count = $_GET['row_count'];
} else {
$all = mysql_query($sql);
$row_count = @mysql_num_rows($all);
}
$page_count = ceil($row_count/$rows_per_page)-1;
## Start building sql for GET varables for advanced search
//Add Price From
if(isset($_REQUEST['pfrom']) && ($_REQUEST['pfrom'] != '-1'))
$search[] = ' price >= '.$_REQUEST['pfrom'];
//Add Max Price
if(isset($_REQUEST['pto']) && ($_REQUEST['pto'] != '-1'))
$search[] = ' price <= '.$_REQUEST['pto'];
//Add Property Type
if(isset($_REQUEST['category']) && ($_REQUEST['category'] != ''))
$search[] = ' cid = '.$_REQUEST['category'];
//Add Property Style Type
if(isset($_REQUEST['style']) && ($_REQUEST['style'] != ''))
$search[] = ' styleID = '.$_REQUEST['style'];
//Add Min Bed
if(isset($_REQUEST['bed']) && ($_REQUEST['bed'] != ''))
$search[] =' bed >= "'.$_REQUEST['bed'].'"';
//Add Min Bath
if(isset($_REQUEST['bath']) && ($_REQUEST['bath'] != ''))
$search[] = ' bath >= "'.$_REQUEST['bath'].'"';
//Add city
if(isset($_REQUEST['city']) && ($_REQUEST['city'] != ''))
$search[] = ' city = "'.$_REQUEST['city'].'"';
//Add State
if(isset($_REQUEST['state']) && ($_REQUEST['state'] != ''))
$search[] = ' state = "'.$_REQUEST['state'].'"';
//Add min square feet
if(isset($_REQUEST['sqft']) && ($_REQUEST['sqft'] != ''))
$search[] = ' sqft >= '.$_REQUEST['sqft'];
//Add Garage
if(isset($_REQUEST['garage']) && ($_REQUEST['garage'] != ''))
$search[] = ' garage = "'.$_REQUEST['garage'].'"';
//Add lot size
if(isset($_REQUEST['lot_size']) && ($_REQUEST['lot_size'] != ''))
$search[] = ' lot_size >= '.$_REQUEST['lot_size'];
//implode to search string on ' and ';
$searchStr = @implode(' and ',$search);
$sql = 'select * FROM items WHERE (expires > NOW()) and active = "Yes" and ';
$sql .= $searchStr;
### DEBUG
if($debugP) echo 'Advanced Search Sql<hr>'.$sql;
$error['Results'] = 'No results found, please search again';
//Perform search
$searchResults = $mysql->exSql($sql);
### BUILD OUTPUT ####
//display the rows for the specified page
for ($i=$start; $i<$end; $i++)
{
echo "Row: ".$i;
}
//out put the links to all available pages
for ($j=1; $j<=$page_count; $j++)
echo "Page ".$j." link";
// Build Page Number Hyperlinks
echo "<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 ($j=1; $j<=$page_count; $j++){
if(($page_count) == $j){
echo "$j ";
} else {
echo "<a href=\"".$_SERVER['PHP_SELF']."?pageNum_p=$j\">$j</a> ";
}
}
// Build Next Link
if($page < $page_count){
$next = ($page_count + 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page_count=$next\".>Next>></a>";
}
echo "</center>";
?>
<div align="left" class="locText"><a href="index.php" class="locLink">Home</a> <span class="locArrow"> > </span> Search Results</div>
<hr size="1" color="#666666">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="pageText" >Showing: <strong><?php echo ($start + 1) ?> to <?php echo min($start + $rows_per_page, $row_count) ?> of <?php echo $row_count ?></strong> Listings</td>
<td align="right" class="pageText"></td>
</tr>
</table></td>
</tr>
<tr>
<td height="5"><img src="images/pixel.gif" width="1" height="1" alt=""></td>
</tr>
<tr>
<td><table width="100%" border="0" cellspacing="1" cellpadding="4" class="resBorder">
<tr>
<td class="colText">City</td>
<td class="colText">ST</td>
<td class="colText">Price</td>
<td class="colText">Beds</td>
<td class="colText">Baths</td>
<td class="colText">Sqft</td>
</tr>
<? while($row_p = @mysql_fetch_assoc($searchResults)) { ?>
<tr valign="top">
<td class="rowText"><? echo $row_p['city']; ?></td>
<td class="rowText"><? echo $row_p['state']; ?></td>
<td class="rowText"><? echo Money($row_p['price'],1); ?></td>
<td class="rowText"><? echo $row_p['bed']; ?></td>
<td class="rowText"><? echo $row_p['bath']; ?></td>
<td class="rowText"><? echo $row_p['sqft']; ?></td>
</tr>
<tr valign="top">
<td class="descText" colspan="8"><table border="0" cellspacing="0" cellpadding="4">
<tr valign="top">
<? //fetch photo from database
$sql = "select * from photos where ptid = ". $row_p['id']." and porder = '1' ";
//echo $sql; //debug sql output
$pRS = mysql_query($sql,$myconn) or die(mysql_error());
//fetch assoc array
$row_photo = mysql_fetch_assoc($pRS);
// if image exist
if ( mysql_num_rows($pRS) > 0 )
$imageSRC = $row_photo['location'];
else //no photo
$imageSRC = "noimage.jpg";
?>
<td class="descText"><a href="detail.php?id=<? echo $row_p['id']; ?>"><img src="admin/photos/uploads/small_thumbs/tn_<? echo $imageSRC; ?>" border="0" alt="<? echo $row_photo['caption']; ?>"></a></td>
<td class="descText"><? echo substr($row_p['description'],0,150); ?>...<a href="detail.php?id=<? echo $row_p['id']; ?>" class="rowLink">more..</a><a class="rowLink" href="saveListing.php?id=<? echo $row_p['id']; ?>" title="Click to save listing"><br>
save listing</a></td>
</tr>
</table></td>
</tr>
<? } ?>
![]() |
Similar Threads
- Clean Previous Next Script for MySQL results (PHP)
- Store multiple selection from pagination info into single array (PHP)
- IE 5.0 not displaying pages properly (Web Browsers)
Other Threads in the PHP Forum
- Previous Thread: php script displays the source code on invokation
- Next Thread: tomorrow date??? .
| Thread Tools | Search this Thread |
5.2.10 action apache api array beginner binary broken cakephp checkbox class classes cms code cron curl database date destroy display dynamic echo echo$_get[x]changingitintovariable... email encode error fcc file files folder form forms function functions google header howtowriteathesis href htaccess html if-else image images include insert ip javascript joomla limit link local login mail memberships menu mlm mod_rewrite multiple multipletables mysql mysqlquery neutrality oop open passwords paypal pdf php provider query radio random record remote rss script search server sessions sockets source space sql strip_tags syntax system table template thesishelp tutorial update upload url validator variable video voteup web window.onbeforeunload=closeme; youtube





