doupi4649 2018-05-08 00:07
浏览 31
已采纳

快速PHP分页无法正常工作

Found a working code on PHP pagination here and i am trying to integrate this code into my search engine. I tried but the pagination function doesn't seem to work please can anybody tell me what's wrong with my code and any possible fixes.

<?php
if(isset($_GET['search']))
{
$search = $_GET['search'];
$condition = '';
$query = explode(" ", $_GET["search"]);
foreach($query as $text)
{
    $condition .= "question LIKE '%".SQLite3::escapeString($text)."%' OR answer LIKE '%".SQLite3::escapeString($text)."%' OR keywords LIKE '%".SQLite3::escapeString($text)."%' OR ";
}
$condition = substr($condition, 0, -4);

$sql_query = "SELECT * FROM questions WHERE " . $condition;
$result = $db->query($sql_query);
echo $sql_query;
$queryCount = "SELECT COUNT(*) as count FROM questions WHERE " . $condition;
$countRet = $db->querySingle($queryCount);
if ($countRet > 0)
{
    $perpage = 1;
    $start = isset($_GET['start']) ? $_GET['start']: '';
    $max_pages= ceil($countRet / $perpage);
    if (!$start) {
        $start = 0;
        $getQuery = $db->query("SELECT * FROM questions WHERE $condition LIMIT $start, $perpage");
        while($row = $getQuery->fetchArray(SQLITE3_ASSOC))
        {
            echo '<tr><td>'.$row["question"].'</td></tr>';
        }
        //Pagination Codes
        echo "<center>";
        $prev = $start - $perpage;
        $next = $start + $perpage;

        $adjacent = 3;
        $last = $max_pages - 1;

        if ($max_pages > 1) {
            //prev button
            if (!$start <= 0)
            {
                echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$prev'>Prev</a> "; 
            }
            if ($max_pages < 7 + ($adjacent * 2)) {
                $i = 0;
                for ($counter = 1; $counter < 4 + ($adjacent * 2); $counter++) {
                    if ($i == $start) {
                        echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
                    }
                    else {
                        echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
                    }
                    $i = $i + $perpage;
                }
            }
            elseif ($max_pages - ($adjacent * 2) > ($start / $perpage) && ($start / $perpage) > ($adjacent * 2)) {
                echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
                echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$perpage'>2</a> .... ";

                $i = $start;
                for ($counter = ($start / $perpage)+1; $counter < ($start /$perpage) + $adjacent + 2; $counter++) {
                    if ($i == $start) {
                        echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
                    }
                    else {
                        echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
                    }
                    $i = $i + $perpage;
                }
            }
            else {
                echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
                echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$perpage'>2</a> .... ";

                $i = $start;
                for ($counter = ($start / $perpage) + 1; $counter <= $max_pages; $counter++) {
                    if ($i = $start) {
                        echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
                    } 
                    else {
                        echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
                    }
                    $i = $i + $perpage;
                }
            }
        }
        //next button
        if (!($start >= $countRet - $perpage)) {
            echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$next'>Next</a> ";
        }
        echo "</center>";
    }
}
else
{
    echo '<label>No data found</label>';
}

}
?>

The code above doesn't seem to work for some reason. I have tried to debug but cant't seem to find the error or errors. Any reasonable help would do with this. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongpao5261 2018-05-08 00:16
    关注

    First you set $start in your ternary logic:

    $start = isset($_GET['start']) ? $_GET['start']: '';

    After this, you check whether $start exists or not, and if it doesn't, you set it 0:

    if (!$start) {
        $start = 0;
        $getQuery ...
        while($row = $getQuery ...
    

    The problem is all of your query and pagination logic is inside of this if conditional; if $GET['start'] is set, then your query and pagination logic will never trigger. And on top of this, all of your outputted <a> links do indeed send the start parameter:

    <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>
    

    To resolve this, simply set your ternary to 0 for the failure condition, and omit the if conditional entirely:

    $start = isset($_GET['start']) ? $_GET['start']: 0;
    

    This can be seen in the following:

    $start = isset($_GET['start']) ? $_GET['start']: 0;
    $max_pages= ceil($countRet / $perpage);
    $getQuery = $db->query("SELECT * FROM questions WHERE $condition LIMIT $start, $perpage");
    while($row = $getQuery->fetchArray(SQLITE3_ASSOC))
    {
        echo '<tr><td>'.$row["question"].'</td></tr>';
    }
    //Pagination Codes
    echo "<center>";
    $prev = $start - $perpage;
    $next = $start + $perpage;
    
    $adjacent = 3;
    $last = $max_pages - 1;
    
    if ($max_pages > 1) {
        //prev button
        if (!$start <= 0)
        {
            echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$prev'>Prev</a> "; 
        }
        if ($max_pages < 7 + ($adjacent * 2)) {
            $i = 0;
            for ($counter = 1; $counter < 4 + ($adjacent * 2); $counter++) {
                if ($i == $start) {
                    echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
                }
                else {
                    echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
                }
                $i = $i + $perpage;
            }
        }
        elseif ($max_pages - ($adjacent * 2) > ($start / $perpage) && ($start / $perpage) > ($adjacent * 2)) {
            echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
            echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$perpage'>2</a> .... ";
    
            $i = $start;
            for ($counter = ($start / $perpage)+1; $counter < ($start /$perpage) + $adjacent + 2; $counter++) {
                if ($i == $start) {
                    echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
                }
                else {
                    echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
                }
                $i = $i + $perpage;
            }
        }
        else {
            echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
            echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$perpage'>2</a> .... ";
    
            $i = $start;
            for ($counter = ($start / $perpage) + 1; $counter <= $max_pages; $counter++) {
                if ($i = $start) {
                    echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
                } 
                else {
                    echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
                }
                $i = $i + $perpage;
            }
        }
    }
    //next button
    if (!($start >= $countRet - $perpage)) {
        echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$next'>Next</a> ";
    }
    echo "</center>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划