[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Php Real Estate Script - Pagination

Posted on 2010-03-30
7
Medium Priority
?
470 Views
Last Modified: 2013-12-12
I am using a real estate script and have the listings displayed on the results page. Now I have a table with the details organized and I want to display the results in the table to their specific location on the table, and also I want to display 5 results per page, page numbers and next and previous pages as well as total records
The code is below - first is the php code that displays the information (used as a test) and below is the table where all needs to be placed!!!
<?php 
if(isset($_POST['search'])) { 
        foreach($_POST as $key=>$value) { 
                $$key = $value; 
        } 
        $query = mysql_query("SELECT * FROM listings WHERE price BETWEEN $minPrice AND $maxPrice AND listingType IN($listingType) AND region IN($region) AND propType IN($propType)") or die(mysql_error());        
        $numrows = mysql_num_rows($query); 
       ?>
	   
	   
	   <?php
	   if($numrows <= 0) { 
                echo 'No Results'; 
        } else { 
                while($row = mysql_fetch_array($query)) { 
                        echo 'Listing: '.$row['listingType'].', Region: '.$row['region'].', Property: '.$row['propType'].', Price: '.$row['price'].', Beds: '.$row['beds'].'<br />'; 
                } 
        } 
} 
?>
      
      <table width="100%" border="0">
        <tr>
          <td colspan="2"><div id="searchResults">
            <table width="100%" border="0">
              <tr>
                <td width="1%" rowspan="8">&nbsp;</td>
                <td height="17" colspan="4" class="style8"><?php echo $row['beds']; ?> bedroom <?php echo $row['propType']; ?> for <?php echo $row['listingType']; ?> in <?php echo $row['area']; ?>, <?php echo $row['region']; ?><br />
                  <br /></td>
              </tr>
              <tr>
                <td width="36%" rowspan="7"><div align="center"><?php echo '<img src=" images/' . $row['image1'] . '" />' . PHP_EOL;
 ?></div></td>
                <td class="style8"><strong>Ref: </strong></td>
                <td colspan="2" class="style8"><?php echo $row['refNum']; ?></td>
              </tr>
              <tr>
                <td class="style8"><strong>Location:</strong></td>
                <td colspan="2" class="style8"><?php echo $row['area']; ?>, <?php echo $row['region']; ?></td>
              </tr>
              <tr>
                <td class="style8"><strong>Type:</strong></td>
                <td colspan="2" class="style8"><?php echo $row['propType']; ?></td>
              </tr>
              <tr>
                <td class="style8"><strong>Price:</strong></td>
                <td colspan="2" class="style8">&euro;<?php echo $row['price']; ?></td>
              </tr>
              <tr>
                <td height="18" class="style8"><strong>Bedrooms:</strong></td>
                <td colspan="2" class="style8"><?php echo $row['beds']; ?></td>
              </tr>
              <tr>
                <td class="style8"><strong>Bathrooms:</strong></td>
                <td colspan="2" class="style8"><?php echo $row['baths']; ?></td>
              </tr>
              <tr>
                <td width="13%" height="31"><span class="style26"><a href="propdetails.php">View Details</a> <a onclick="Login_Modal_Window(0);" href="javascript:void(0);"></a></span></td>
                <td width="27%" class="style26"><a onclick="Login_Modal_Window(0);" href="javascript:void(0);">Save Listing</a></td>
                <td width="23%" class="style22">Posted by <?php echo $row['postedBy']; ?></td>
              </tr>
            </table>
          </div>
          <span class="style8"> Page 1 of 1</span> <br /></td>
        </tr>
       
       
      </table>

Open in new window

0
Comment
Question by:dino_angelides
  • 4
  • 3
7 Comments
 
LVL 14

Expert Comment

by:john-formby
ID: 29116323
Give me a few minutes and I will have a look :-)
0
 

Author Comment

by:dino_angelides
ID: 29116769
ok john thank you :) take your time
0
 
LVL 14

Accepted Solution

by:
john-formby earned 2000 total points
ID: 29120866
Hi,

Please check the following code for searchresults.php.

Hope this helps,

John
searchresults.php
=================

<?php
$dbHost = "localhost";
$dbUser = "YOUR_USERNAME";
$dbPass = "YOUR_PASSWORD";
$dbName = "YOUR_DATABASE";
$db = mysql_connect($dbHost,$dbUser,$dbPass);
mysql_select_db($dbName,$db);

foreach($_REQUEST as $key=>$value) {  
    $$key = $value;  
}

$pall = $_REQUEST['p'];
if($pall != 'all') {
	$total_pages = $_REQUEST['p'];
	$per_page = 5;
	$cur_page = isset($_REQUEST['p']) ? (int)$_REQUEST['p'] : 1;
	$query = "SELECT * FROM listings WHERE price BETWEEN $minPrice AND $maxPrice AND listingType IN($listingType) AND region IN($region) AND propType IN($propType)";
	$result = @mysql_query($query);
	$numrows = mysql_num_rows($result);
	$total_pages = ceil($numrows / $per_page);
	if ($cur_page <1 || $cur_page> $total_pages) {
		$cur_page = 1;
	}
	$offset = (($cur_page - 1) * $per_page);
	$query = "SELECT * FROM listings WHERE price BETWEEN $minPrice AND $maxPrice AND listingType IN($listingType) AND region IN($region) AND propType IN($propType) LIMIT $offset, $per_page";
	$result = @mysql_query($query);
	$total = mysql_num_rows($result);
	$counter = 1 + (($cur_page-1)*$per_page);
} else {
	$query = "SELECT * FROM listings WHERE price BETWEEN $minPrice AND $maxPrice AND listingType IN($listingType) AND region IN($region) AND propType IN($propType)";
	$result = @mysql_query($query);
	$numrows = mysql_num_rows($result);
}

if($numrows <= 0) {  
    echo 'No Results';  
} else {  
    while($row = mysql_fetch_array($result)) {
    	
    ?>
        <table width="100%" border="0"> 
	        <tr> 
	          <td colspan="2"><div id="searchResults"> 
	            <table width="100%" border="0"> 
	              <tr> 
	                <td width="1%" rowspan="8">&nbsp;</td> 
	                <td height="17" colspan="4" class="style8"><?php echo $row['beds']; ?> bedroom <?php echo $row['propType']; ?> for <?php echo $row['listingType']; ?> in <?php echo $row['area']; ?>, <?php echo $row['region']; ?><br /> 
	                  <br /></td> 
	              </tr> 
	              <tr> 
	                <td width="36%" rowspan="7"><div align="center"><?php echo '<img src=" images/' . $row['image1'] . '" />' . PHP_EOL; 
	 ?></div></td> 
	                <td class="style8"><strong>Ref: </strong></td> 
	                <td colspan="2" class="style8"><?php echo $row['refNum']; ?></td> 
	              </tr> 
	              <tr> 
	                <td class="style8"><strong>Location:</strong></td> 
	                <td colspan="2" class="style8"><?php echo $row['area']; ?>, <?php echo $row['region']; ?></td> 
	              </tr> 
	              <tr> 
	                <td class="style8"><strong>Type:</strong></td> 
	                <td colspan="2" class="style8"><?php echo $row['propType']; ?></td> 
	              </tr> 
	              <tr> 
	                <td class="style8"><strong>Price:</strong></td> 
	                <td colspan="2" class="style8">&euro;<?php echo $row['price']; ?></td> 
	              </tr> 
	              <tr> 
	                <td height="18" class="style8"><strong>Bedrooms:</strong></td> 
	                <td colspan="2" class="style8"><?php echo $row['beds']; ?></td> 
	              </tr> 
	              <tr> 
	                <td class="style8"><strong>Bathrooms:</strong></td> 
	                <td colspan="2" class="style8"><?php echo $row['baths']; ?></td> 
	              </tr> 
	              <tr> 
	                <td width="13%" height="31"><span class="style26"><a href="propdetails.php">View Details</a> <a onclick="Login_Modal_Window(0);" href="javascript:void(0);"></a></span></td> 
	                <td width="27%" class="style26"><a onclick="Login_Modal_Window(0);" href="javascript:void(0);">Save Listing</a></td> 
	                <td width="23%" class="style22">Posted by <?php echo $row['postedBy']; ?></td> 
	              </tr> 
	            </table> 
	          </div> 
	          </td> 
	        </tr> 
	      </table> 
	<?php
    }
}
if($pall != 'all') {
	if($numrows > $per_page) {
		echo '<br />';
		$page_pad = 2;
		$min_page = max(1, $cur_page - $page_pad);
		$max_page = min($total_pages, $cur_page + $page_pad);
		$show_prev = $cur_page> 1 ? $cur_page - 1 : false;
		$show_next = $cur_page <$total_pages ? $cur_page + 1 : false;
	  	if ($show_prev) {
			echo "<a href=\"?p=1&amp;region=$region&amp;listingType=$listingType&amp;propType=$propType&amp;minPrice=$minPrice&amp;maxPrice=$maxPrice\">&laquo; First</a> ";
			echo "<a href=\"?p=$show_prev&amp;region=$region&amp;listingType=$listingType&amp;propType=$propType&amp;minPrice=$minPrice&amp;maxPrice=$maxPrice\">&lt; Prev</a> ";
		}
		for ($page = $min_page; $page <= $max_page; $page++) {
			if($cur_page<>$page)
				echo "<a href=\"?p=$page&amp;region=$region&amp;listingType=$listingType&amp;propType=$propType&amp;minPrice=$minPrice&amp;maxPrice=$maxPrice\">$page</a> ";
	   		else
	   			echo "$page ";
	   	}
	   	if ($show_next) {
	   		echo "<a href=\"?p=$show_next&amp;region=$region&amp;listingType=$listingType&amp;propType=$propType&amp;minPrice=$minPrice&amp;maxPrice=$maxPrice\">Next &gt;</a> ";
	   		echo "<a href=\"?p=$total_pages&amp;region=$region&amp;listingType=$listingType&amp;propType=$propType&amp;minPrice=$minPrice&amp;maxPrice=$maxPrice\">Last &raquo;</a>";
	   	}
		echo " &nbsp; <a href=\"?p=all&amp;region=$region&amp;listingType=$listingType&amp;propType=$propType&amp;minPrice=$minPrice&amp;maxPrice=$maxPrice\">View All</a><br />";
	}
}
?>

Open in new window

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Author Comment

by:dino_angelides
ID: 29126201
Perfect it works :) just one more thing
where do i put the styling?
lets say style26 is the css i want to add to the section below how do i do that?
echo "<a href=\"?p=$page&amp;region=$region&amp;listingType=$listingType&amp;propType=$propType&amp;minPrice=$minPrice&amp;maxPrice=$maxPrice\">$page</a> ";
0
 
LVL 14

Expert Comment

by:john-formby
ID: 29127187
Hi,

You can add it like this:

echo "<a class=\"style26\" href=\"?p=$page&amp;region=$region&amp;listingType=$listingType&amp;propType=$propType&amp;minPrice=$minPrice&amp;maxPrice=$maxPrice\">$page</a> ";

Hope this helps,

John
0
 

Author Closing Comment

by:dino_angelides
ID: 31709082
it worked...thx John,
if you are up to it i will put another question up for sorting the columns based on Price High-Low and Price Low-High
Thx again for answering that question too, much appreciated
0
 
LVL 14

Expert Comment

by:john-formby
ID: 29130607
You're welcome, glad it works for you.  Thanks for the points :-)
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Strategic internal linking is often considered an SEO power technique, especially for content marketing. Do you need to hire an SEO agency to optimize you internal linking? No, this article will help you understand the basics of internal linking and…
There is a wide range of advantages associated with the use of ASP.NET. This is why this programming framework is used to create excellent enterprise-class websites, technologies, and web applications.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Suggested Courses

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question