PHP分页链接生成 [英] PHP Pagination Link Generation

查看:73
本文介绍了PHP分页链接生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下面的脚本,可以在其中找到分页,从广义上讲,它可以很好地工作,但是由于它是我的剪切粘贴工作,因此我不了解如何实际生成回显的链接带有$ pagination变量的脚本.

I have this script below which i found on SO to generate pagination and broadly speaking, its working great, however because its a cut and paste job from me, i don't understand how to actually generate the links which are echoed in the script with the variable $pagination.

它呼应的是:

1< a href="index.php?page=2">2< a href="index.php?page=3">3< a hr_ef="?page=2"> Next 

没有一个是有效的(可单击的)链接,我还希望能够设置它们的样式,因此宁愿以HTML而不是php echo的形式输出它们,例如:

None of which are working (clickable) links, and i also want to be able to style them so would rather output them in HTML, rather than a php echo, something like:

<p><?php 1< a href="index.php?page=2">2< a href="index.php?page=3">3< a hr_ef="?page=2"> Next ?> </p>

以下是我正在使用的脚本:

Below is the script i'm using:

<?php
/* Set current, prev and next page */
$page = (!isset($_GET['page']))? 1 : $_GET['page']; 
$prev = ($page - 1);
$next = ($page + 1);

/* Max results per page */
$max_results = 10;

/* Calculate the offset */
$from = (($page * $max_results) - $max_results);

/* Query the db for total results.*/
$result = mysql_query("...");
$total_results = mysql_num_rows($result);

$total_pages = ceil($total_results / $max_results);

$pagination = '';

/* Create a PREV link if there is one */
if($page > 1)
{
$pagination .= '< a href="?page='.$prev.'">Previous</a> ';
}

/* Loop through the total pages */
for($i = 1; $i <= $total_pages; $i++)
{
if(($page) == $i)
{
    $pagination .= $i;
}
else
{
    $pagination .= '< a href="index.php?page='.$i.'">'.$i.'</a>';
}
}

/* Print NEXT link if there is one */
if($page < $total_pages)
{
$pagination .= '< a hr_ef="?page='.$next.'"> Next</a>';
}

/* Below is how you query the db for ONLY the results for the current page */
$query ="SELECT * FROM ... LIMIT $from, $max_results";

$result=mysql_query($query) or die(mysql_error());
$rsjobinfo=mysql_fetch_assoc($result);

do {?>

<div>
[Individual Row Output]
</div>
<?php } while ($rsjobinfo=mysql_fetch_assoc($result));

echo $pagination;
?> 

有人可以帮忙吗?我想这是一个小问题,但一如既往,我们会朝着正确的方向前进.

Can someone help? I imagine its a small fix but as always, would appreciate a kick in the right direction.

谢谢 丹

推荐答案

也许这只是一个编辑错误,但是在您的输出中,>-标记似乎并没有再次关闭.另外,标记的开头不得有< a>之类的空格.而< a hr_ef= ...显然是错误的.

Maybe it's just an editing error, but in your output the <a>-tags don't seem to be closed again. Also, there should be no space like < a> at the beginning of the tag. And < a hr_ef= ... is obviously wrong.

要设置样式,可以在构建字符串时在标签中添加class属性,并在CSS中进行样式填充.

In order to style them, you can add a class attribute to the tags while building the string and do the style-stuff in css.

这篇关于PHP分页链接生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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