创建下一个/上一个链接基于以前的MySQL搜索

时间:2013-03-24 17:58:16

标签: php mysql

我有一个可以搜索的会员数据库,它返回一个分页的结果列表。

我想要做的是,如果有人点击搜索结果列表中的成员个人资料,该个人资料也可以使用指向搜索结果中的下一个个人资料的链接,或前一个个人资料。

所有这些链接需要'动态'包含用户ID。例如

<p><a href = 'memberprofile.php?$userid'>Previous</a></p>
<p><a href = 'memberprofile.php?$userid'>Next</a></p>

我的搜索结果页面的伪代码大致为:

Run query to find number of results which match search criteria
Calculate Number of pages of results and generate page number links
while($row = mysql_fetch_array($result))
{
    //Pull out content from each matching row which match search criteria
}

所以这给了我搜索结果。大。

因此,为了做我想做的事情,我需要从用户实际点击的行中获取AND行之前的行的'userid'。

示例如果我的结果显示用户ID为3,5,12,13,22,41,并且访问者点击查看用户12(新页面),我该如何获取5&amp; 13用作下一个/上一个?

此外,假设我可以生成下一个/上一个链接,并且访问者点击它,我该如何为该个人资料生成上一个/下一个按钮?

我正在考虑的方式是它几乎是一个辅助的“分页”脚本,但我无法理解它。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

你可以这样做(也是伪代码)

$prev_row= NULL;
$next_row=NULL;
while($row = mysql_fetch_array($result))
{
  - this element is the $next_row for previous element, if it is not null, of course
  - the last element is the $prev_row for this element (or default NULL if this one is first)

  - Pull out content from each matching row which match search criteria

  - store this element so that you can use it in next element as the previous one
}