2013-04-11 138 views
1

Thanx为您的早期反应和建议。SQL搜索结果和分页

请耐心等待,因为我还没有很好地归于PHP MySQL编程。

我的搜索工作得很好,除了两个问题如下:

  1. 默认情况下,我想查询返回WHERE C_ID> 0时,该页面被访问的所有记录。主键是c_id。目前,当我访问它时,页面不显示任何记录。直到我将这个条件添加到保存为外键数值的分类标准之前,情况并非如此:

    AND cat_id ='“。$ cat_id。” '

  2. 此外,我希望能够分页搜索结果。我正在阅读一些帖子,并提到了将链接添加搜索词。我不知道应该如何处理像我这样的多重标准。当我按照标准完成搜索后点击下一个按钮时,我当前的分页将返回所有记录。

这里是整个代码,我有:

搜索页面:

<?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: [email protected]    * 
* 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

范围导致

实例:

SELECT * FROM `your_table` LIMIT 0, 10 

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

SELECT * FROM `your_table` LIMIT 5, 5 

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

这是你问的是什么呢?

+0

不是。我认为LIMIT已经在分页类中设置为:function PAGING($ sql,$ records = 24,$ pages = 4)。我的问题是无法浏览我的搜索结果。 – 2013-04-11 13:36:22

+0

我得到的印象是,想要取消这种方法,因为它需要执行和检索整个表格,然后处理代码 - 我也同意在这种情况下进行查询更好。 – 2013-04-11 13:39:07

+0

你有没有在查询之前回应'cat_id' - 或者更好地回显整个查询并看到它们如预期的那样? – 2013-04-11 13:46:10