Solved

page wise search in php

Posted on 2004-08-23
4
637 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now