doumi4676 2015-04-05 16:56
浏览 24
已采纳

如何使用MySQL和PHP处理多个搜索结果的分页?

I'm working on a Web Search Engine project. I am working on the pagination feature. When I click on the page, it gives abrupt results and Undefined variable: page1 error is given. What to do?

if(isset($_GET['page'])) //results displayed based on page selection
    {
    $page=$_GET['page'];
    if($page=="" || $page=="1")
    {
        $page1=0;
    }
    else
    {
    $page1=($page*10)-10;
    }
    }

$numrows1 = mysqli_num_rows($query); // page evaluation and this statement calculates number of resultant rows
$a = $numrows1/10; //10 is for number of results per page and $a gives number of pages
$a = ceil($a);
echo "<br>"; echo "<br>";
    for($b=1; $b<=$a; $b++)
    {
        ?><a href="search.php?page=<?php echo $b; ?>" style="text-decoration:none;"><?php echo $b."  ";?></a><?php
    }

I hope I'm clear. Anyone can help me fix this?

  • 写回答

1条回答 默认 最新

  • douxie2029 2015-04-05 23:26
    关注

    You seem to never set the page unless you have a page in the URI.

    I'm assuming if you used "page.php?page=1" it wouldn't throw that error since $page1 would be set.

    I would personally do this:

    if(isset($_GET['page']) && $_GET['page'] != 1)//if page is set and page doesn't equal 1
    {
        $startingRecord = ($_GET['page'] * 10) - 10;
    }
    else
    {
        $startingRecord = 0;
    }
    

    Also, I switched $page1 to be $startingRecord to make more sense, as you are calculating the starting record, not the page.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站