0

任何人都知道如何设置我正在寻找的这个分页的好地方或好的教程?

在你生气我在这里发布数百行之前,让我快速解释一下我想要实现的目标,我用来更好地解释我的情况的代码。

如果您只想分页或只想搜索,下面的代码可以工作:
1. 进入主页,分页工作。点击第 1 页、第 2 页等,你会得到你期望的结果。
2.使用搜索,它可以工作,但这会在一页内提供所有记录而不受限制。我可以设置一个限制,但分页仍然不起作用。

我只是不知道如何获取搜索变量并将其添加到分页中。

是否有可能有一个像这样的网址:search=Hello+World?orderby=custforename+custsurname?page=3..我认为它是......这样的东西,'?nested get variables?叫?有人知道高级分页教程的好地方吗?


我在我的主页上使用此代码:

<?php
    if(isset($_POST['search']))
    {
        $search = $_POST['search'];
        $search = ltrim($search, " ");
        $terms = explode(" ", $search);
        $customerlistquery = "
                            SELECT *
                            FROM customer
                            LEFT JOIN company
                            ON customer.compid=company.compid
                            WHERE
                            ";
        $i = 0;
        foreach ($terms as $each) 
        {
            if ($i++ > 0) 
            $customerlistquery .= ' OR ';

            $customerlistquery .= "concat(custsurname, custforename, custmidname, custpostal, custphone1, custphone2, custfax, custnamecode, datemodified, modifiedby) LIKE '%$each%' ";
        }
        $customerlistquery .= " ORDER BY $orderby ASC";
    }
    else
    {
        $page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
        $startpoint = ($page * $limit) - $limit;
        $customerlistquery = "  
                SELECT *
                FROM customer
                LEFT JOIN company
                ON customer.compid=company.compid
                ORDER BY $orderby ASC 
                LIMIT {$startpoint}, {$limit}
                ";
        echo pagination("customer
                LEFT JOIN company
                ON customer.compid=company.compid
                ORDER BY $orderby ASC",$limit,$page);       
    }
?>


$result=mysql_query($itemslistquery) or die("query fout " . mysql_error() );

最后是 pagination.php 代码:

<?php
    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"; 

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

        $prev = $page - 1;                          
        $next = $page + 1;
        $lastpage = ceil($total/$per_page);
        $lpm1 = $lastpage - 1;

        $pagination = "";
        if($lastpage > 1)
        {   
            $pagination .= "<ul class='pagination'>";
                    $pagination .= "<li class='details'>Page $page of $lastpage</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'>Next</a></li>";
                $pagination.= "<li><a href='{$url}page=$lastpage'>Last</a></li>";
            }else{
                $pagination.= "<li><a class='current'>Next</a></li>";
                $pagination.= "<li><a class='current'>Last</a></li>";
            }
            $pagination.= "</ul>\n";      
        }


        return $pagination;
    } 
?>
4

1 回答 1

1

如果你想通过 GET 传递超过 1 个变量,你必须像这样使用它:

url?var1=x&var2=y&var3=z

在 PHP 中,您可以像这样恢复它们:

$var1=$_GET['var1'];
$var2=$_GET['var2'];
$var3=$_GET['var3'];

关于您的“分页器”问题,我通常只进行 1 次查询,然后在客户端使用 javascript 对其进行分页,这样我就可以更改页面而无需等待服务器回答。

如果您只想使用 PHP,您应该在查询中设置 'LIMIT x,y',其中 x 是页面的第一个元素,y 是要在一个页面中显示的元素数。例如,如果您要显示 50 个元素的页面:

$x=50*($pagenumber-1); //the index of the 1st element is 0
$y=50;
$query="SELECT * FROM table LIMIT $x,$y";

像这样的东西应该可以正常工作。

于 2012-11-06T12:27:13.887 回答