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条回答 默认 最新

相关推荐 更多相似问题