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.

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

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)