第1页后分页不起作用?

时间:2014-09-28 06:44:37

标签: php mysql function object pagination

好吧我现在做了一个分页,当它被使用时它完美地显示了数据,但当点击第2页接下来的5个结果时它显示空白并且网址也在变化,即 - 首次搜索时,检索结果页面的网址为

http://myurl.com/search.php?find=sa&catname=All+Business+Type

当点击第2页时,它显示空白并带有网址

http://myurl.com/search.php?page=2

我该怎么做才能解决它?

 $find = $_GET['find'];
 $find = strtoupper($find); 
 $find = strip_tags($find); 
$find = trim ($find); 
$allcatmain=$_GET['catname'];


 //Now we search for our search term, in the field the user specified 

$page = (int)(!isset($_GET["page"]) ? 1 : $_GET["page"]);
if ($page <= 0) $page = 1;

$per_page = 5; // Set how many records do you want to display per page.

$startpoint = ($page * $per_page) - $per_page;

 $statement = "information 
    WHERE (title LIKE '%".$find."%' AND catname IN $allcat) OR (address LIKE '%".$find."%' AND        catname IN $allcat) ORDER BY id DESC"; 


  $results ="SELECT * FROM ".$statement." LIMIT ".$startpoint.",".$per_page."";
  $data=mysql_query($results);


   //And we display the results 
    while($result = mysql_fetch_array( $data )) 
            { 
    $link=$result['web']; 
   echo $result['title'];
   echo $result['address']; 

   }
    // displaying paginaiton.
   echo pagination($statement,$per_page,$page);

分页功能

         function pagination($query,$per_page=10,$page=1,$url='?'){  

$query = "SELECT COUNT(*) as `num` FROM {$query}";
$row = mysql_fetch_array(mysql_query($query));
$total = $row['num'];
$adjacents = "2";

$prevlabel = "&lsaquo; Prev";
$nextlabel = "Next &rsaquo;";

$page = ($page == 0 ? 1 : $page); 
$start = ($page - 1) * $per_page;                              

$prev = $page - 1;                         
$next = $page + 1;

$lastpage = ceil($total/$per_page);

$lpm1 = $lastpage - 1; // //last page minus 1

$pagination = "";
if($lastpage > 1){  
    $pagination .= "<ul class='pagination'>";


        if ($page > 1) $pagination.= "<li><a href='{$url}page={$prev}'>{$prevlabel}</a></li>";

    if ($lastpage < 7 + ($adjacents * 2)){  
        for ($counter = 1; $counter <= $lastpage; $counter++){
            if ($counter == $page)
                $pagination.= "<li><a class='current'>{$counter}</a></li>";
            else
                $pagination.= "<li><a href='{$url}page={$counter}'>{$counter}</a></li>";                   
        }

    } elseif($lastpage > 5 + ($adjacents * 2)){

        if($page < 1 + ($adjacents * 2)) {

            for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++){
                if ($counter == $page)
                    $pagination.= "<li><a class='current'>{$counter}</a></li>";
                else
                    $pagination.= "<li><a href='{$url}page={$counter}'>{$counter}</a></li>";                   
            }
            $pagination.= "<li class='dot'>...</li>";
            $pagination.= "<li><a href='{$url}page={$lpm1}'>{$lpm1}</a></li>";
            $pagination.= "<li><a href='{$url}page={$lastpage}'>{$lastpage}</a></li>"; 

        } elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) {

            $pagination.= "<li><a href='{$url}page=1'>1</a></li>";
            $pagination.= "<li><a href='{$url}page=2'>2</a></li>";
            $pagination.= "<li class='dot'>...</li>";
            for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) {
                if ($counter == $page)
                    $pagination.= "<li><a class='current'>{$counter}</a></li>";
                else
                    $pagination.= "<li><a href='{$url}page={$counter}'>{$counter}</a></li>";                   
            }
            $pagination.= "<li class='dot'>..</li>";
            $pagination.= "<li><a href='{$url}page={$lpm1}'>{$lpm1}</a></li>";
            $pagination.= "<li><a href='{$url}page={$lastpage}'>{$lastpage}</a></li>";     

        } else {

            $pagination.= "<li><a href='{$url}page=1'>1</a></li>";
            $pagination.= "<li><a href='{$url}page=2'>2</a></li>";
            $pagination.= "<li class='dot'>..</li>";
            for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) {
                if ($counter == $page)
                    $pagination.= "<li><a class='current'>{$counter}</a></li>";
                else
                    $pagination.= "<li><a href='{$url}page={$counter}'>{$counter}</a></li>";                   
            }
        }
    }

        if ($page < $counter - 1) $pagination.= "<li><a href='{$url}page={$next}'>{$nextlabel}</a></li>";

    $pagination.= "</ul>";       
}

return $pagination;
   }
   ?>

1 个答案:

答案 0 :(得分:0)

您需要将现有查询字符串(部分)传递给$ url:

$url = '?';
foreach ($_GET as $key => $value) {
    if ($key != 'page') {
        $url .= $key . '=' . $value . '&';
    }
}
// displaying paginaiton.
echo pagination($statement,$per_page,$page, $url);