开发者

how to show the sql query in different page?

开发者 https://www.devze.com 2023-02-15 17:02 出处:网络
how to show the开发者_C百科 sql query in different page?(using php) like when link 1 is clicked then 1-10 will appear ..

how to show the开发者_C百科 sql query in different page?(using php) like when link 1 is clicked then 1-10 will appear .. for 2 it is 11-20 and so on..


If you're looking like something like:

  • page1.php?show=10 (1-10)
  • page1.php?show=20 (11-20)

You'll have to do some calculations (max # of page, start & end of resultset) and then use the LIMIT clause if you're using mysql.


As Ali mentioned, the easiest approach is to LIMIT your query, and build the number of pages depending on the total count in the DB.

You may find this useful

http://www.phpeasystep.com/phptu/29.html


The "Easiest" approach is to add the LIMIT clause to your query and pass in the page number. so if page number is 1 (i.e. the default or first page)

$page = 1;
$query = "SELECT * FROM Product WHERE name LIKE 'John%' LIMIT ";

if($page == 1){

   $query .= $recordsPerPage;

}

else{

   $query .= ($recordsPerPage+1) ."," ($page-1 + $recordsPerPage);

}

You'll need to execute a separate query to get the number of matching records and divide them by the records per page.

only an acceptable approach for a not high traffic site. High traffic sites you'll probably have to use cursors.


I suggest looking at MySQL's LIMIT and OFFSET.

And a simple example:

// parameters
$ipp = 10; # items per page
$page = $_GET['page']; # lets fetch page from clicked link
if(!$page){ $page = 1; } # default page to be used when none is given (to be used when we're at index.php
$offset = ($page - 1) * $ipp; # lets calculate result offset for current page

// query
$sql = "SELECT * FROM `table` LIMIT {$ipp} OFFSET {$offset};";
// add your desired result processing

Links for this example are as simple as: index.php?page=1, index.php?page=2 etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消