doumi4676
doumi4676
2015-04-05 16:56

如何使用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 douxie2029 6年前

    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.

    点赞 评论 复制链接分享