Solved

page wise search in php

Posted on 2004-08-23
4
647 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

739 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