All of my php code is posted below. I apologize for the length. I am new to using sessions and relatively new to getting pagination to work.
When the page first loads, it works fine. However, when you click on a Page # or the Next or Prev buttons, I get the message saying "Nothing to Display!" which is in my code. There are over 7000 results in the query and this first page only display 20, so I know there are plenty of results.
Also, I tried printing the queries on the page and it is populating the SQL query with results that were not originally in the $_SESSION[''] variables. The query somehow changes when the user clicks on the Page #, Next or Prev buttons.
I am sure that I am making some small error, but I am not picking up on it. Does anyone have any thoughts that could send me in the right direction?
Thanks in advance!
session_start();
$city = $_SESSION['city'];
$state = $_SESSION['state'];
$zip = $_SESSION['zip'];
$mls = $_SESSION['mls'];
$minPrice = $_SESSION['minPrice'];
$maxPrice = $_SESSION['maxPrice'];
$beds = $_SESSION['beds'];
$baths = $_SESSION['baths'];
$limit = 20;
$query_count = "SELECT * FROM residential WHERE ListPrice >= '$minPrice'";
if($city != "") {
$query_count .= " AND City = '$city'";
}
if($mls != "") {
$query_count .= " AND MLSNumber = '$mls'";
}
if($zip != "") {
$query_count .= " AND Zip5 = '$zip'";
}
echo $query_count;
$result_count = mysql_query($query_count);
$totalrows = mysql_num_rows($result_count);
echo "<tr>
<td>
<p align=\"right\"><font size=\"2\" face=\"Verdana\">Results ($totalrows)</font></td></tr>";
$PHP_SELF = $_SERVER['PHP_SELF'];
if(!isset($_GET['page'])){ // Checks if the $page variable is empty (not set)
$page = 1; // If it is empty, we're on page 1
} else{
$page = $_GET['page'];
}
$limitvalue = $page * $limit - ($limit );
// Ex: (page2 * 5(items per page) = 10) - 5 = 5 <- data starts at 5
$query = "SELECT * FROM residential WHERE ListPrice >= '$minPrice'";
if($city != "") {
$query .= " AND City = '$city'";
}
if($mls != "") {
$query .= " AND MLSNumber = '$mls'";
}
if($zip != "") {
$query .= " AND Zip5 = '$zip'";
}
$query .= " ORDER BY ListPrice ASC";
$query .= " LIMIT $limitvalue, $limit";
echo $query;
$result = mysql_query($query) or die("Error: " . mysql_error());
if(mysql_num_rows($result) == 0){
echo("Nothing to Display!");
}
$counter = 0;
$cells_per_row = 5;
while($row=mysql_fetch_array($result)){
$counter++;
if(($counter % $cells_per_row) == 1) { echo '<tr><td> </td></tr><tr>'; }
$mls = $row['MLSNumber'];
$streetNumber = $row['StreetNumber'];
$streetName = $row['StreetName'];
$address = $row['DisplayAddress'];
$city = $row['City'];
$state = $row['State'];
$zip = $row['Zip5'];
$squareft = $row['Tot_Square_Feet'];
$bedrooms = $row['No_Bedrooms'];
$baths = $row['Tot_Baths'];
$listPrice = $row['ListPrice'];
$firstName = stripslashes(strip_tags($row['ListAgentFirstName']));
$lastName = stripslashes(strip_tags($row['ListAgentLastName']));
$photo = $row['PhotoURL'];
print "<tr>
<td width=\"120\" rowspan=\"4\">
<font face=\"Verdana\" size=\"2\">
<img border=\"0\" src=\"$photo\" height=\"83\" width=\"83\" style=\"border: 1px solid #000000\"></font></td>
<td width=\"120\" valign=\"top\">
<font face=\"Verdana\" size=\"2\">$streetNumber $streetName</font></td>
<td width=\"90\" valign=\"top\" rowspan=\"4\">
<font face=\"Verdana\" size=\"2\">$listPrice</font></td>
<td width=\"90\" valign=\"top\" rowspan=\"4\">
<font face=\"Verdana\" size=\"2\">$bedrooms Br</font></td>
<td width=\"90\" valign=\"top\">
<font face=\"Verdana\" size=\"2\">$baths</font></td>
<td width=\"90\" valign=\"top\" rowspan=\"4\">
<font face=\"Verdana\" size=\"2\">$squareft Sq. Feet</font></td>
<td width=\"100\" valign=\"top\" rowspan=\"4\">
<font face=\"Verdana\" size=\"2\">$mls</font></td>
</tr>
<tr>
<td width=\"120\" valign=\"top\">
<font face=\"Verdana\" size=\"2\">$city, $state $zip</font></td>
<td width=\"90\" valign=\"top\">
<font face=\"Verdana\" size=\"2\"></font></td>
</tr>
<tr>
<td width=\"120\" valign=\"top\"> </td>
<td width=\"90\" valign=\"top\"> </td>
</tr>
<tr>
<td width=\"120\" valign=\"top\"> </td>
<td width=\"90\" valign=\"top\"> </td>
</tr>
<tr>
<td width=\"700\" colspan=\"7\"> </td>
</tr>";
if(($counter % $cells_per_row) == 0) { echo '</tr>'; }
}
// just in case we haven't closed the last row
// this would happen if our result set isn't divisible by $cells_per_row
if(($counter % $cells_per_row) != 0) { echo '</tr>'; }
echo '<tr><td align=center colspan=10>';
if($page != 1){
$pageprev = $page - 1; //decrementing $page-- does not work on all php configurations.
echo("<font face=\"Arial\" size=\"1\"><a href=\"PropertyResults.php?page=$pageprev\">PREV</a></font> ");
}else{
echo("<font face=\"Arial\" size=\"1\">PREV</font>");
}
$numofpages = $totalrows / $limit;
#echo "<br>", $totalrows;
#exit;
for($i = 1; $i <= $numofpages; $i++){
/* This for loop will add 1 to $i at the end of each pass until $i is greater
than $numofpages. */
if($i == $page){
echo("<font face=\"Arial\" size=\"1\" color=\"#810023\">".$i." </font>");
}else{
echo("<font face=\"Arial\" size=\"1\" color=\"#810023\"><a href=\"PropertyResults.php?page=$i\">$i</a> </font>");
}
} //code above has been decoded lozza
if(($totalrows % $limit) != 0){
if($i == $page){
echo("<font face=\"Arial\" size=\"1\" color=\"#810023\">".$i." </font>");
}else{
echo("<font face=\"Arial\" size=\"1\" color=\"#810023\"><a href=\"PropertyResults.php?page=$i\">$i</a> </font>");
}
}
if(($totalrows - ($limit * $page)) > 0){
$pagenext = $page + 1; //incrementin $page like $page++ does not work in all enviroments
echo("<font face=\"Arial\" size=\"1\"><a href=\"PropertyResults.php?page=$pagenext\">NEXT</a></font>");
}else{
echo("<font face=\"Arial\" size=\"1\">NEXT </font>");
}
echo '</td></tr>';
mysql_free_result($result);