从sql查询创建多个页面

时间:2011-02-16 17:36:01

标签: php mysql

我正在尝试从查询中创建多个页面。如果我有100个结果,并且每页需要10个结果,我希望创建10个页面,每个页面显示不同的查询。这就是我的查询设置方式:

$sql ="SELECT * FROM Post WHERE (active ='1') ORDER BY PostID DESC ";
$result = mysql_query($sql,$db);
$numrows = mysql_num_rows($result);

while(($post = mysql_fetch_assoc($result))) {
$posts[] = $post;
}


 <?php
if (!$numrows){ echo $errormessage;}
else{


 foreach($posts as $post): ?>

 echo stripslashes($post['text']);  

 <?php endforeach; }?>

这会从数据库中提取每个“帖子”并将它们全部显示出来。

我想做这样的事情:

$results = mysql_num_rows($result);


$numpages = 10/$results; //gives the number of pages

while($numpages<$results)
{

//run code from above\\
}

使用我使用的方法执行此操作的最佳方法是什么?我很感激你的意见,因为我处在我自己的逻辑迷失的那些阶段。谢谢!

3 个答案:

答案 0 :(得分:3)

大多数分页示例都无法满足现实生活中的要求。例如,自定义查询字符串 所以,这是一个完整而简洁的例子:

<?
per_page=10;
// Let's put FROM and WHERE parts of the query into variable
$from_where="FROM Post WHERE active ='1'";
// and get total number of records
$sql = "SELECT count(*) ".$from_where;
$res = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
$row = mysql_fetch_row($res);
$total_rows = $row[0];

//let's get page number from the query string 
if (isset($_GET['page'])) $CUR_PAGE = intval($_GET['page']); else $CUR_PAGE=1;
//and calculate $start variable for the LIMIT clause
$start = abs(($CUR_PAGE-1)*$per_page);

//Let's query database for the actual data
$sql = "SELECT * $from_where ORDER BY PostID DESC LIMIT $start,$per_page";
$res = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
// and fill an array
while ($row=mysql_fetch_array($res)) $DATA[++$start]=$row;

//now let's form new query string without page variable
$uri = strtok($_SERVER['REQUEST_URI'],"?")."?";    
$tmpget = $_GET;
unset($tmpget['page']);
if ($tmpget) {
  $uri .= http_build_query($tmpget)."&";
}    
//now we're getting total pages number and fill an array of links
$num_pages=ceil($total_rows/$per_page);
for($i=1;$i<=$num_pages;$i++) $PAGES[$i]=$uri.'page='.$i;

//and, finally, starting output in the template.
?>
Found rows: <b><?=$total_rows?></b><br><br>
<? foreach ($DATA as $i => $row): ?>
<?=$i?>. <a href="?id=<?=$row['id']?>"><?=$row['title']?></a><br>
<? endforeach ?> 

<br>
Pages: 
<? foreach ($PAGES as $i => $link): ?>
<? if ($i == $CUR_PAGE): ?>
<b><?=$i?></b>
<? else: ?> 
<a href="<?=$link?>"><?=$i?></a>
<? endif ?> 
<? endforeach ?> 

答案 1 :(得分:1)

从数据库中提取所有数据并从PHP管理它是很可怕的。它会杀死RAM和网络。

使用LIMIT子句进行分页。

首先,您需要查看记录总数。使用

$query = 'SELECT count(1) as cnt FROM Post WHERE active =1';

然后,

$result = mysql_query($query, $db);
$row = mysql_fetch_assoc($result);
$count = $row['cnt'];

$pages = ceil($count/10.0); //used to display page links. It's the total number of pages.
$page = 3; //get it from somewhere else, it's the page number.

然后提取数据

$query = 'SELECT count(1) as cnt FROM Post WHERE active =1 LIMIT '.$page*10.', 10';
$result = mysql_query($query, $db);
while($row = mysql_fetch_assoc($result))
{
    //display your row
}

然后输出数据。

答案 2 :(得分:-4)

我正在使用类似的代码来执行此操作:

$data = mysql_query("SELECT * FROM `table`");
$total_data = mysql_num_rows($data);

$step = 30;
$from = $_GET['p'];

$data = mysql_query("SELECT * FROM `table` LIMIT '.$from.','.$step.'"

该代码获得总行数

以及用于创建链接的内容:

$p=1;
for ($j = 0 ; $j <= $total_data+$step; $j+=$step) 
{ 
    echo ' <a href="page.php?p='.$j.'">'.$p.'</a> '; 
    $p++;

} ?>

您还可以阅读:pagination