Solved

page wise search in php

Posted on 2004-08-23
4
644 Views
Last Modified: 2008-01-09
Hi,
    I want to make a page in which there will be number of pages listed according to items in the database ....like 2 items per page....10 items means five pages need to be shown with links. I have used looping and a difficult method to carry out this. It works but I feel that its not an efficient way...could increase the processing speed though I am not sure. I know that one can use LIMIT (mysql) for this. Also I need to  show like  1,2,3,4,5 Next and in next page Previous 6,7,8,9,10. I havent worked with the LIMIT thing so was seeking out some help.

Sid
0
Comment
Question by:Sid123
  • 2
4 Comments
 
LVL 1

Expert Comment

by:rstorey2079
ID: 11874090
Here's a bit of code that may help you out.  This doesn't get into displaying 1 2 3 4 5  but it will point you in the right direction.  To display all the available pages, you'd have to get the total number of results and then just calculate how many pages there will be and display the links.

<?php

if($_URL['pageID']=="") {
   $pageID = 1;

}

$numItemsPerPage = 5;

$sql = "SELECT * FROM stuff LIMIT " .($pageID*$numItemsPerPage).",".(($pageID+1)*$numItemsPerPage);

//run the sql

//output your results



echo "<a href='myPage.php?pageID=".$pageID+1.'>Next page</a>";

?>
0
 
LVL 32

Accepted Solution

by:
ldbkutty earned 100 total points
ID: 11874219
>> It works but I feel that its not an efficient way...could increase the processing speed though I am not sure

As always, i suggest for this 'Helper' Class which you can use it for any pages(nicely explained):
http://www.phpnoise.com/tutorials/9/3

There are also other pagination links:
http://www.tonymarston.net/php-mysql/pagination.html
http://codewalkers.com/code.php?c=2&order=0&list=detail
http://www.designplace.org/scripts.php?page=1&c_id=25
0
 
LVL 3

Expert Comment

by:Boris Aranovich
ID: 11879438
$_URL?
Don't you mean $_REQUEST or $_GET?
Anyhow, you must intval() the page number, you don't want funny things happening with it, right? :)

the query is by the way:

$pageID = (isset($_REQUEST['pageID']) && ((int)$_REQUEST['pageID']>0))?(int)$_REQUEST['pageID']:1;
$recordsPerPage = 10;
$firstRec = ($pageID-1)*$recordsPerPage;

$sql = "SELECT * FROM table ORDER BY id LIMIT {$firstRec}, {$recordsPerPage};";

There isn't a much more efficient way to do it. This sends 1 query and fetches only the relevant records.
You can of course fetch all the DB to a Session, and then only read it from there, but... come on, that's just heavy on the server.
0
 
LVL 1

Expert Comment

by:rstorey2079
ID: 11881521
oops, confused with coldfusion there.  I do mean $_GET.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

791 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