分页搜索结果 [英] pagination for search result

查看:75
本文介绍了分页搜索结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个搜索功能,它将返回大量数据,我想做的是对它们进行分页以避免长时间的加载,我已经尝试了很长时间,但仍然无法做确切的事情,当我在第一页限制为10,显示10个结果,但是当我单击第二页时,显示空白页,下面是我的代码,任何人都可以帮助更正我的代码,
非常感谢所有帮助. thx


im having a search function that will return large amount of data, what i wanna do is to page them to avoid long loading time, i have try it for long and still cannot do the exact thing, what happen is when im at the first page with limit of 10, it shows 10 result but when i click the second page, it display a blank page, below are my code, can anyone help to correct my codes,
all help are much appreciated. thx


<?php $cari = $_POST['carian'];?>
    <div align="center">
         <?php include 'dbconnect.php';

         $targetpage = "dummy.php";

         $limit = 10;

        $stages = 3;

        $page = mysql_escape_string($_GET['page']);

        if($page){

        $start = ($page - 1) * $limit;

        }else{

        $start = 0;

        }

        $keywords = $cari;

        $pieces = explode(" ",$keywords);

        $count = count($pieces);

        $query="SELECT * FROM mysurah WHERE";

        $query1="SELECT COUNT(*) as num FROM mysurah WHERE";

        if ($count==1)

        {

        //only one keyword user entered

        $query.=" Terjemahan LIKE '%$keywords%'";

        $query1.=" Terjemahan LIKE '%$keywords%'";

        }

        else if( $count>=1)
        {
        //More than one keywords..
        for($a=0;$a<$count;$a++)

        {

        //loop through all the keywords and complete select statement.

        $query.=" Terjemahan LIKE '%$pieces[$a]%' ";

        $query1.=" Terjemahan LIKE '%$pieces[$a]%' ";

        if ($a != $count-1)

        {

        //if it is last keyword then don't put OR in select statment.



        $query.="AND";

        $query1.="AND";

        }

        }

        }

        $total_pages = mysql_fetch_array(mysql_query($query1));

        $total_pages = $total_pages[num];

        $query.=" LIMIT $start, $limit";

        $result = mysql_query($query)or die('SQL error');

while($row = mysql_fetch_array($result, MYSQL_ASSOC))

            {

            $message = "";

            if ($cari !="")

            {

            // Initial page num setup

        if ($page == 0){$page = 1;}

        $prev = $page - 1;

        $next = $page + 1;

        $lastpage = ceil($total_pages/$limit);

        $LastPagem1 = $lastpage - 1;

        $paginate = '';

    if($lastpage > 1)
    {

$paginate .= "<div class='paginate'>";
        // Previous
        if ($page > 1){
            $paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
        }else{
            $paginate.= "<span class='disabled'>previous</span>";   }

        // Pages
        if ($lastpage < 7 + ($stages * 2))  // Not enough pages to breaking it up

        {

            for ($counter = 1; $counter <= $lastpage; $counter++)

            {

                if ($counter == $page){

                    $paginate.= "<span class='current'>$counter</span>";

                }else{

                    $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}

            }

        }

        elseif($lastpage > 5 + ($stages * 2))   // Enough pages to hide a few?
        {
            // Beginning only hide later pages
            if($page < 1 + ($stages * 2))

            {

                for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)

                {

                    if ($counter == $page){

                        $paginate.= "<span class='current'>$counter</span>";

                    }else{

                        $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}

                }

                $paginate.= "...";

                $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";

                $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";

            }

            // Middle hide some front and some back

            elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
            {
                $paginate.= "<a href='$targetpage?page=1'>1</a>";
                $paginate.= "<a href='$targetpage?page=2'>2</a>";
                $paginate.= "...";
                for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)

                {

                    if ($counter == $page){

                        $paginate.= "<span class='current'>$counter</span>";

                    }else{

                        $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}

                }

                $paginate.= "...";

                $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";

                $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";

            }

            // End only hide early pages

            else

            {

                $paginate.= "<a href='$targetpage?page=1'>1</a>";

                $paginate.= "<a href='$targetpage?page=2'>2</a>";

                $paginate.= "...";

                for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)

                {

                    if ($counter == $page){

                        $paginate.= "<span class='current'>$counter</span>";

                    }else{

                        $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}

                }

            }

        }

                // Next

        if ($page < $counter - 1){

            $paginate.= "<a href='$targetpage?page=$next'>next</a>";

        }else{

            $paginate.= "<span class='disabled'>next</span>";

            }

        $paginate.= "</div>";



}

推荐答案

cari =
cari =


_POST['carian'];?> < div =" center" <?php include 'dbconnect.php';
_POST['carian'];?> <div align="center"> <?php include 'dbconnect.php';


targetpage = "dummy.php";
targetpage = "dummy.php";


这篇关于分页搜索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆