从SQL查询创建多个页面 [英] Creating multiple pages from sql query

查看:93
本文介绍了从SQL查询创建多个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create multiple pages from my query. If I have 100 results, and I want 10 results per page, I would like there to be ten pages created, with each page showing a different query. This is how my query is set up:

$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; }?>

这将从数据库中提取每个帖子",并将其全部显示出来.

This pulls each "post" from the database and puts displays them all out.

我想做这样的事情:

$results = mysql_num_rows($result);


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

while($numpages<$results)
{

//run code from above\\
}

用我使用的方法做到这一点的最佳方法是什么?感谢您的意见,因为我正处于迷失自我的那些阶段之一.谢谢!

What is the best way to do this with the method that I use? I appreciate your opinions because I'm at one of those stages where I'm lost in my own logic. Thank You!

推荐答案

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

Most pagination examples fail to meet real life requirements. A custom query string, for example.
So, here is a complete yet concise example:

<?
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 ?> 

这篇关于从SQL查询创建多个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆