PHP分页问题

时间:2012-06-04 22:54:33

标签: php mysql

我有一个搜索,我试图分页。我找到了一个很酷的PHP脚本,at PHPEasyStep(是的,我是PHP的新手,还有编程。)我实现了它,它可以显示分页并前进一个页面(从第1页到第2页) ,但是当您从第2页返回到第1页时,搜索查询已消失,然后显示每个数据库条目(但已分页)。搜索查询必定会在某处丢失?

What's Happening?

The Code(当我粘贴它时会被压缩所以我已经尽力缩进但是我很难过,如果有任何仍然难以阅读。如果它太难以阅读它主要是在网站上我得到它从更好的缩进...上面有一个链接!)

<?php
     $q = mysql_real_escape_string(ucfirst(trim($_GET['searchquery'])));
?>
 <?php

  REQUIRE('config.php');

  $tbl_name="companies";

  $adjacents = 3;


  $query = "SELECT COUNT(*) as num FROM $tbl_name WHERE company_name LIKE '%$q%' OR      company_description LIKE '%$q%' OR cat1 LIKE '%$q' OR cat2 LIKE '%$q' OR cat3 LIKE '%$q' OR company_phone LIKE '%$q'";

  $total_pages = mysql_fetch_array(mysql_query($query));
  $total_pages = $total_pages[num];


  $targetpage = "search.php";
  $limit = 10;                              
  $page = $_GET['page'];
  if($page){
    $start = ($page - 1) * $limit; 
  }         
  else{
    $start = 0;                             
  }

  $sql = "SELECT * FROM $tbl_name WHERE company_name LIKE '%$q%' OR company_description LIKE '%$q%' OR cat1 LIKE '%$q' OR cat2 LIKE '%$q' OR cat3 LIKE '%$q' OR company_phone LIKE '%$q' ORDER by company_name LIMIT $start, $limit";

  $result = mysql_query($sql);


  if ($page == 0) $page = 1;                    
  $prev = $page - 1;                            
  $next = $page + 1;                            
  $lastpage = ceil($total_pages/$limit);        
  $lpm1 = $lastpage - 1;                        

  $pagination = "";
  if($lastpage > 1)
  { 
    $pagination .= "<div class=\"pagination\">";
    //previous button
    if ($page > 1) 
        $pagination.= "<a href=\"$targetpage?page=$prev+searchquery=$q\">� previous</a>";
    else
        $pagination.= "<span class=\"disabled\">� previous</span>"; 

    //pages 
    if ($lastpage < 7 + ($adjacents * 2))   //not enough pages to bother breaking it up
    {   
        for ($counter = 1; $counter <= $lastpage; $counter++)
        {
            if ($counter == $page)
                $pagination.= "<span class=\"current\">$counter</span>";
            else
                $pagination.= "<a href=\"$targetpage?page=$counter+searchquery=$q\">$counter</a>";                  
        }
    }
    elseif($lastpage > 5 + ($adjacents * 2))    //enough pages to hide some
    {
        //close to beginning; only hide later pages
        if($page < 1 + ($adjacents * 2))        
        {
            for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class=\"current\">$counter</span>";
                else
                    $pagination.= "<a href=\"$targetpage?page=$counter+searchquery=$q\">$counter</a>";                  
            }
            $pagination.= "...";
            $pagination.= "<a href=\"$targetpage?page=$lpm1+searchquery=$q\">$lpm1</a>";
            $pagination.= "<a href=\"$targetpage?page=$lastpage+searchquery=$q\">$lastpage</a>";        
        }
        //in middle; hide some front and some back
        elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
        {
            $pagination.= "<a href=\"$targetpage?page=1+searchquery=$q\">1</a>";
            $pagination.= "<a href=\"$targetpage?page=2+searchquery=$q\">2</a>";
            $pagination.= "...";
            for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class=\"current\">$counter</span>";
                else
                    $pagination.= "<a href=\"$targetpage?page=$counter+searchquery=$q\">$counter</a>";                  
            }
            $pagination.= "...";
            $pagination.= "<a href=\"$targetpage?page=$lpm1+searchquery=$q\">$lpm1</a>";
            $pagination.= "<a href=\"$targetpage?page=$lastpage+searchquery=$q\">$lastpage</a>";        
        }
        //close to end; only hide early pages
        else
        {
            $pagination.= "<a href=\"$targetpage?page=1+searchquery=$q\">1</a>";
            $pagination.= "<a href=\"$targetpage?page=2+searchquery=$q\">2</a>";
            $pagination.= "...";
            for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class=\"current\">$counter</span>";
                else
                    $pagination.= "<a href=\"$targetpage?page=$counter+searchquery=$q\">$counter</a>";                  
            }
        }
    }

    //next button
    if ($page < $counter - 1) 
        $pagination.= "<a href=\"$targetpage?page=$next+searchquery=$q\">next �</a>";
    else
        $pagination.= "<span class=\"disabled\">next �</span>";
    $pagination.= "</div>\n";       
}
?>

如果您需要更多代码,我可以快速提供!感谢您的帮助!

哦,我忘了说我尝试通过在所有分页网址的末尾添加+ searchquery = $ q来解决这个问题,这就是为什么会这样。

2 个答案:

答案 0 :(得分:1)

该行:

if ($page > 1) 
        $pagination.= "<a href=\"$targetpage?page=$prev+searchquery=$q\">� previous</a>";

应该是:

if ($page > 1) 
        $pagination.= "<a href=\"$targetpage?page=$prev&searchquery=$q\">� previous</a>";

答案 1 :(得分:0)

尝试将+searchquery=$q替换为&searchquery=$q