jmac2501 Posted June 6, 2007 Share Posted June 6, 2007 Hey- I have a Database and script where it SELECT * FROM my database and lists the info on my website. I want to be able to limit the quary to only 20 per page. Here is an example of what i am talking about on this page http://sacvalleyhomes.com/Sacvalley/property_inquiry.php Everything listed under Property Inquiry is genarated be my database. As it is now i have 5 diffrent tables for 5 diffrent pages. This is way to much work to keep updated. What i want to do is put all 5 tables in to 1 table and have my php script only show the first 20 listing then generate the page numbers on the bottom or some way to view next 20 results and so on. <? $link = mysql_connect('*****', '*****', '*****') or die('Could not connect: ' . mysql_error()); mysql_select_db('*****') or die('Could not select database'); $query="SELECT * FROM Listings ORDER BY price"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); ?> <? $i=0; while ($i < $num) { $streetaddress=mysql_result($result,$i,"streetaddress"); $city=mysql_result($result,$i,"city"); $price=mysql_result($result,$i,"price"); $squarefeet=mysql_result($result,$i,"squarefeet"); $bed=mysql_result($result,$i,"bed"); $Fbath=mysql_result($result,$i,"Fbath"); $Hbath=mysql_result($result,$i,"Hbath"); $reo=mysql_result($result,$i,"reo"); $agent=mysql_result($result,$i,"agent"); $pic=mysql_result($result,$i,"pic"); ?> //more scripts stuff... <? ++$i; } echo "</table>"; ?> ###########This is how i am now switching pages but i no longer want to switch pages just change the results from in this page upto 20 listings.############# <p align="center"><font size="8"><b> <a href="Property_inquiry2.php"><font size="3">Next Page ></font></a></b></font></p> <p align="center"><b><font size="3">< 1 </font> <font size="1"> <a href="Property_inquiry2.php"><font size="3">2</font></a></font><font size="3"> </font> <font size="4"> <a href="Property_inquiry3.php"><font size="3">3</font></a></font><font size="3"> <a href="Property_inquiry4.php"><font size="3">4</font></a></font><font size="3"> </font> <font size="4"> <a href="property_inquiry5.php"><font size="3">5</font></a></font><font size="3"> <a href="Property_inquiry2.php"><font size="3">></font></a></font><font size="3"> </font> <font size="4"> //More stuff Quote Link to comment https://forums.phpfreaks.com/topic/54374-solved-limit-results-to-20-per-page/ Share on other sites More sharing options...
dbillings Posted June 6, 2007 Share Posted June 6, 2007 <?php $display = 5; // Sets number of items to display per page if(isset($_GET['np'])) { $num_pages = $_GET['np']; }else{ $query= "SELECT * FROM listings ORDER BY price "; $result = mysql_query($query); $num_records = @mysql_num_rows($result); if($num_records>$display){ $num_pages = ceil($num_records/$display); }else{ $num_pages = 1; } } // Determine where in the database to start returning results if (isset($_GET['s'])) { $start = $_GET['s']; }else{ $start = 0; } // Run the query to select given group of items $query= "SELECT * FROM Listings ORDER BY price LIMIT $start, $display"; $result = mysql_query($query); $num = @mysql_num_rows($result); mysql_close(); if($num>0){//If query returned data, display data. // Links to other pages if($num_pages > 1){ echo '<p>'; // determine what page the script is on $current_page = ($start/$display) + 1; //If it's not the first page display a previous button if($current_page !=1){ echo '<a class="nav" href="yourpage.php?s=' .($start - $display) . // Set the title of // the page your using. '&np=' . $num_pages. '">Previous</a>'.' '; } // Make all the numbered pages for($i=1; $i <= $num_pages; $i++){ if($i != $current_page){ echo '<a class="nav" href="yourpage.php?s=' . (($display * ($i - 1))) . '&np=' . $num_pages .'">' .$i. '</a>'.' '; }else{ echo $i . ' '; } } //If it's not the last page display next button if($current_page != $num_pages){ echo '<a class="nav" href="yourpage.php?s=' . ($start+$display) . '&np=' . $num_pages .'">Next</a>'; } echo '</p><br/>'; } } ?> kudos, good luck! Quote Link to comment https://forums.phpfreaks.com/topic/54374-solved-limit-results-to-20-per-page/#findComment-268908 Share on other sites More sharing options...
jmac2501 Posted June 6, 2007 Author Share Posted June 6, 2007 Works Great. Just what i needed. Thanks SO much. Quote Link to comment https://forums.phpfreaks.com/topic/54374-solved-limit-results-to-20-per-page/#findComment-269050 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.