SQL搜索结果和分页

时间:2013-04-11 09:50:05

标签: php mysql pagination

Thanx的早期回应和建议。

请与我一起承担,因为我仍然没有得到PHP MySQL编程的支持。

我的搜索工作正常,除了以下两个问题:

  1. 默认情况下,我希望查询返回所有记录WHERE c_id>访问页面时为0。 c_id是主键。目前,我访问该页面时不会显示任何记录。之前的情况并非如此,直到我为保存为外键的数值的类别添加了此条件:

    AND cat_id ='“。$ cat_id。” “

  2. 此外,我希望能够对搜索结果进行分页。我正在阅读一些帖子,并提到将链接添加到搜索词中。我不知道在我的情况下应该采用多种标准。在我根据标准进行搜索后单击下一个按钮时,我当前的分页会返回所有记录。

  3. 以下是我的全部代码:

    搜索页面:

    <?php
    
    $ctitle = mysql_real_escape_string($_POST['ctitle']);
    $csubject = mysql_real_escape_string($_POST['csubject']);
    $creference = mysql_real_escape_string($_POST['creference']);
    $cat_id = ($_POST['cat_id']);
    $cmaterial = mysql_real_escape_string($_POST['cmaterial']);
    $ctechnic = mysql_real_escape_string($_POST['ctechnic']);
    $cartist = mysql_real_escape_string($_POST['cartist']);
    $csource = mysql_real_escape_string($_POST['csource']);
    
    $sql = "SELECT * FROM collections WHERE c_id>0 AND `ctitle` LIKE '%".$ctitle."%' AND `csubject` LIKE '%".$csubject."%' AND `creference` LIKE '%".$creference."%' AND `cat_id` LIKE '%".$cat_id."%' AND `cmaterial` LIKE '%".$cmaterial."%' AND `ctechnic` LIKE '%".$ctechnic."%' AND `cartist` LIKE '%".$cartist."%' AND `csource` LIKE '%".$csource."%' ORDER BY c_id ASC";
    
    $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
    
    //    pagination code
        $PAGING=new PAGING($sql);
    
    //    There are two optional parameters as well:
    //    $records = 16 //Number of records to be displayed per page
    //    $pages = Number of pages to be displayed in the paging
        $sql=$PAGING->sql;
    
    if (mysql_num_rows($sql_result)>0) {
    
    //    The following line gives us an SQL statement with appropriate limits applied
        $sql_result=mysql_query($sql) or die($sql." - ".mysql_error());
    
        while ($row = mysql_fetch_assoc($sql_result)) {
        $c_id=$row['c_id'];
    ?>
    
    search results .....................
    
    <?=$PAGING->show_paging("gallery.php")?>
    

    您可能还想查看我正在使用的分页类:

    <?php
    /************************************************
    *    ========================================    *
    *    Perfect MySQL Paging                        *
    *    ========================================    *
    *    Script Name: class.paging.php                *
    *    Developed By: Khurram Adeeb Noorani            *
    *    Email: khurramnoorani@gmail.com                *
    *    My CV: http://www.visualcv.com/kanoorani    *
    *    Twitter: http://www.twitter.com/kanoorani    *
    *    Date Created: 08-JULY-2009                    *
    *    Last Modified: 08-JULY-2009                    *
    ************************************************/
    ?>
    <?php
    class PAGING
    {
        var $sql,$records,$pages;
        /*
        Variables that are passed via constructor parameters
        */
        var $page_no,$total,$limit,$first,$previous,$next,$last,$start,$end;
        /*
        Variables that will be computed inside constructor
        */
        function PAGING($sql,$records=24,$pages=4)
        {
            if($pages%2==0)
                $pages++;
            /*
            The pages should be odd not even
            */
            $res=mysql_query($sql) or die($sql." - ".mysql_error());
            $total=mysql_num_rows($res);
            $page_no=isset($_GET["page_no"])?$_GET["page_no"]:1;
            /*
            Checking the current page
            If there is no current page then the default is 1
            */
            $limit=($page_no-1)*$records;
            $sql.=" limit $limit,$records";
            /*
            The starting limit of the query
            */
            $first=1;
            $previous=$page_no>1?$page_no-1:1;
            $next=$page_no+1;
            $last=ceil($total/$records);
            if($next>$last)
                $next=$last;
            /*
            The first, previous, next and last page numbers have been calculated
            */
            $start=$page_no;
            $end=$start+$pages-1;
            if($end>$last)
                $end=$last;
            /*
            The starting and ending page numbers for the paging
            */
            if(($end-$start+1)<$pages)
            {
                $start-=$pages-($end-$start+1);
                if($start<1)
                    $start=1;
            }
            if(($end-$start+1)==$pages)
            {
                $start=$page_no-floor($pages/2);
                $end=$page_no+floor($pages/2);
                while($start<$first)
                {
                    $start++;
                    $end++;
                }
                while($end>$last)
                {
                    $start--;
                    $end--;
                }
            }
            /*
            The above two IF statements are kinda optional
            These IF statements bring the current page in center
            */
            $this->sql=$sql;
            $this->records=$records;
            $this->pages=$pages;
            $this->page_no=$page_no;
            $this->total=$total;
            $this->limit=$limit;
            $this->first=$first;
            $this->previous=$previous;
            $this->next=$next;
            $this->last=$last;
            $this->start=$start;
            $this->end=$end;
        }
        function show_paging($url,$params="")
        {
            $paging="";
            if($this->total>$this->records)
            {
                $page_no=$this->page_no;
                $first=$this->first;
                $previous=$this->previous;
                $next=$this->next;
                $last=$this->last;
                $start=$this->start;
                $end=$this->end;
                if($params=="")
                    $params="?page_no=";
                else
                    $params="?$params&page_no=";
                $paging.="<ul class='paging'>";
                $paging.="<li class='paging-current'>Page $page_no of $last</li>";
                if($page_no==$first)
                    $paging.="<li class='paging-disabled'><a href='javascript:void(0)'>&lt;&lt;</a></li>";
                else
                    $paging.="<li><a href='$url$params$first'>&lt;&lt;</a></li>";
                if($page_no==$previous)
                    $paging.="<li class='paging-disabled'><a href='javascript:void(0)'>&lt;</a></li>";
                else
                    $paging.="<li><a href='$url$params$previous'>&lt;</a></li>";
                for($p=$start;$p<=$end;$p++)
                {
                    $paging.="<li";
                    if($page_no==$p)
                        $paging.=" class='paging-active'";
                    $paging.="><a href='$url$params$p'>$p</a></li>";
                }
                if($page_no==$next)
                    $paging.="<li class='paging-disabled'><a href='javascript:void(0)'>&gt;</a></li>";
                else
                    $paging.="<li><a href='$url$params$next'>&gt;</a></li>";
                if($page_no==$last)
                    $paging.="<li class='paging-disabled'><a href='javascript:void(0)'>&gt;&gt;</a></li>";
                else
                    $paging.="<li><a href='$url$params$last'>&gt;&gt;</a></li>";
                $paging.="</ul>";
            }
            return $paging;
        }
    }
    ?>
    

    提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

范围结果

示例:

 SELECT * FROM `your_table` LIMIT 0, 10 

这将显示数据库中的前10个结果。

 SELECT * FROM `your_table` LIMIT 5, 5 

这将显示记录6,7,8,9和10

这是你要求的吗?