943,578 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 2492
  • PHP RSS
Nov 12th, 2008
0

How to limit page numbers for search results for database website

Expand Post »
Good day everyone from beautiful California,

I have been able to limit the number of listings showing per page ($per_page=10)
But how do I limit the number of pages so I do not have rows after rows of pages.

The way it looks now:
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 >>

The way it should look:
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 >>

The code:

php Syntax (Toggle Plain Text)
  1. $option_page=$_REQUEST[option_page];
  2. $per_page = 10;
  3. $sqlsearch="select a.*,b.session_id from fsbo_listing a, searchresults b where a.listingid=b.listingid and b.session_id=\"$sessionid\" order by b.searchid";
  4. //echo"<br>Final Query-->".$sqlsearch;
  5. if (!isset($option_page)) {
  6. $option_page = 1;
  7. }
  8. $prev_option_page = $option_page - 1;
  9. $next_option_page = $option_page + 1;
  10. $option_query = mysql_query($sqlsearch);
  11. $option_page_start = ($per_page * $option_page) - $per_page;
  12. $num_rows = mysql_num_rows($option_query);
  13. if ($num_rows <= $per_page) {
  14. $num_pages = 1;
  15. } else if (($num_rows % $per_page) == 0) {
  16. $num_pages = (int)($num_rows / $per_page);
  17. } else {
  18. $num_pages = (int)($num_rows / $per_page) + 1;
  19. }
  20. $sqlsearch = $sqlsearch . " LIMIT $option_page_start, $per_page";

THANK YOU A MILLION TIMES.

Natasha
Last edited by peter_budo; Nov 13th, 2008 at 5:43 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
123468743867143 is offline Offline
37 posts
since Aug 2008
Nov 13th, 2008
0

Re: How to limit page numbers for search results for database website

Ok, i just have a short question: Your $sqlsearch is used both for displaying the results from the search and making the page numbers in the bottom, or you use it ONLY to create the page numbers on the bottom of your search page.

I would suggest you the following:
You should have 2 querries - one responsible for retrieving the search results, and one dedicated for the page numbers only.
Your display query should look something like:
PHP Syntax (Toggle Plain Text)
  1. if (!isset($current_page))
  2. {
  3. $current_page=1;
  4. }
  5. SELECT a.* b.8 FROM a, b WHERE .... LIMIT ($omit_records, $display_this_number_per_page);
Where $display_this_number_per_page = 10; ( e.g. you want to show 10 records on your page)
and $omit_records= $display_this_number_per_page * $current_page;

The second query will take care of the page numbers only.
It should look something like
PHP Syntax (Toggle Plain Text)
  1. SELECT a.* b.8 FROM a, b WHERE (whatever you like);
Note that there is no limit clause in the select statement, wich will return always the total number of records. You need to count this number of records in order to create the number of pages. So
PHP Syntax (Toggle Plain Text)
  1. $total_records = count(mysql_num_rows(query));
  2. if (($total_records % $display_this_number_per_page)==0)
  3. {
  4. $total_pages = $total_records / $display_this_number_per_page;
  5. }
  6. else
  7. {
  8. $total_pages = ($total_records / $display_this_number_per_page)+1;
  9. }
Then you have to display your page numbers
PHP Syntax (Toggle Plain Text)
  1. for($i=0;$i<$total_pages;$i++)
  2. {
  3. echo '<a href="?current_page="'.$i.'">'.$i.'</a>';
  4. }
So, in short this should look like this:

e.g. you have 45 records in your db. You want to show 10 records per page, and you want to show the page numbers for quick selection, the bottom line should look like
1 | 2 | 3 | 4 | 5
Reputation Points: 21
Solved Threads: 26
Posting Whiz in Training
Rhyan is offline Offline
240 posts
since Oct 2006
Nov 13th, 2008
0

Re: How to limit page numbers for search results for database website

Thank you Rhyanfor your reply. Unfortunatly I am unable to work your suggestion into my code. It seems that all it does is limit to 10 the number of listings displayed per page, not the number of pages.

I already have it set up for 10 listings per page. Problem arises when they are (let's say) 9000 listings which will show 900 page numbers on numerous rows.

1 | 2 | ETC |900 ... Too many page numbers showing on too many rows.

I tried:

php Syntax (Toggle Plain Text)
  1. if ($num_rows > 100) {
  2. $num_pages = 10;
  3. }
But what it does is only display the first 10 page numbers with no possibility to go to the next 800 pages.

It should be simple … what is wrong with my code?

All I want is to show:

1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 >>

If someone clicks on >>, it takes them to the next 10 pages and so forth.

Thank you for your help.

Natasha
Last edited by peter_budo; Nov 13th, 2008 at 5:42 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 10
Solved Threads: 0
Light Poster
123468743867143 is offline Offline
37 posts
since Aug 2008
Nov 13th, 2008
0

Re: How to limit page numbers for search results for database website

Where is the code for your loop that is actually printing out the links? It's not in any of the code you've posted.
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Nov 13th, 2008
0

Re: How to limit page numbers for search results for database website

php Syntax (Toggle Plain Text)
  1. echo"<tr><td align='right'>";
  2. if ($prev_option_page) {
  3. echo "<a href='qualify_search.php?option_page=$prev_option_page&property_type=$_REQUEST[property_type]&region_id=$_REQUEST[region_id]&country_id=$_REQUEST[country_id]&state_id=$_REQUEST[state_id]&minprice=$_REQUEST[minprice]&maxprice=$_REQUEST[maxprice]' class='smalltext'> &lt;&lt; </a> | ";
  4. }
  5. for ($i = 1; $i <= $num_pages; $i++) {
  6. if ($i != $option_page) {
  7. echo "<a href='qualify_search.php?option_page=$i&property_type=$_REQUEST[property_type]&region_id=$_REQUEST[region_id]&country_id=$_REQUEST[country_id]&state_id=$_REQUEST[state_id]&minprice=$_REQUEST[minprice]&maxprice=$_REQUEST[maxprice]' class='smalltext'> $i</a> | ";
  8. } else {
  9. echo '<b><font color=#0C0C7E>' . $i . '</font></b> | ';
  10. }
  11. }
  12. // Next
  13. if ($option_page < $num_pages) {
  14. echo"<a href='qualify_search.php?option_page=$next_option_page&property_type=$_REQUEST[property_type]&region_id=$_REQUEST[region_id]&country_id=$_REQUEST[country_id]&state_id=$_REQUEST[state_id]&minprice=$_REQUEST[minprice]&maxprice=$_REQUEST[maxprice]' class='smalltext'> &gt;&gt; </a>";
  15. }
  16. echo"</td></tr>";
  17. }else{
  18. $country_name=getcountryname($cntid);
Last edited by peter_budo; Nov 13th, 2008 at 5:44 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 10
Solved Threads: 0
Light Poster
123468743867143 is offline Offline
37 posts
since Aug 2008
Nov 13th, 2008
0

Re: How to limit page numbers for search results for database website

Firsly, use code tags. Secondly this is what you need to change
php Syntax (Toggle Plain Text)
  1. for ($i = 1; $i <= $num_pages; $i++) {
  2. if ($i != $option_page) {
  3. echo "<a href='qualify_search.php?option_page=$i&property_type=$_REQUEST[property_type]&region_id=$_REQUEST[region_id]&country_id=$_REQUEST[country_id]&state_id=$_REQUEST[state_id]&minprice=$_REQUEST[minprice]&maxprice=$_REQUEST[maxprice]' class='smalltext'> $i</a> | ";
  4. } else {
  5. echo '<b><font color=#0C0C7E>' . $i . '</font></b> | ';
  6. }
  7. }

The loop needs to be changed to do something along the lines of
php Syntax (Toggle Plain Text)
  1. $start = $option_page - 5;
  2. $end = $option_page + 5;
  3. for( $i = $start; $i < $end; $i++) {
  4. <your code here>
  5. }
That will limit it to 10, however, you should add in your own checks to make sure it doesn't go below 0 or past the final page.
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Nov 13th, 2008
1

Re: How to limit page numbers for search results for database website

I went on solving your issue, but I just stumbled upon the following:

Your task is to display pages in groups of 10, so e.g. 4500 records will display on 45 pages, instead of showing page numbers from 1 to 45, you want to display the pages in groups of 10. so you will have numbers 1, 2, 3, 4, 5 only.
But in this way you will display only first 10 records from each page group. I mean - if you click on 1 - you will have there only 10 records, corresponding to firts page only. This means that either you should display 100 records per page, or, you have to revise your page numbering requirement.

I think that your numbering should look something like

<< Previous 10 | 11 |12|.... next 10 >>, instead of 1|2|3....>>

Please advise...
Reputation Points: 21
Solved Threads: 26
Posting Whiz in Training
Rhyan is offline Offline
240 posts
since Oct 2006
Nov 13th, 2008
0

Re: How to limit page numbers for search results for database website

Impossible to have 100 listings per page.

But not impossible to display groups of 10 pages, 10 listings per page. That will reduce the number of rows of page numbers by ten folds.

Thank you for the idea.

I will try this as soon as my server clears up. I am on a shared Linux and it keeps jamming every time I do lots of changes. A real pain.

Thanks again!


________________________________________
GB d- s+:- a? C--- U P L E !W? N o K- w(+++++) !O? !M !V PS? PE++ !Y !PGP !t- !5 !X !R tv>$ b++ DI !D? G- e++ h(++) r y(+++)
Reputation Points: 10
Solved Threads: 0
Light Poster
123468743867143 is offline Offline
37 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Dynamic DropDownMenu - HELP
Next Thread in PHP Forum Timeline: Urgent - 2 days only to find a solution





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC