Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

page wise search in php

Posted on 2004-08-23
4
Medium Priority
?
650 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 300 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

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…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…
Suggested Courses

885 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