dsk49208 2013-04-14 12:01
浏览 57
已采纳

当您有条件选择数据时,页面的PHP / MySQL分页

I'm trying to do pagination for my index page. Index page show data from database with some conditions getting from url. EDITED: Here is whole code:

   $town = $_GET['label_town'];
$sub = ucfirst($_GET['label_sub']);

if (isset($_GET['label_town']) === true && isset($_GET['label_sub']) === true) {
$where = "WHERE p.label_town = '$town' AND p.label_sub = '$sub' AND `visible` = 1";
} else if (isset($_GET['label_town']) === true && isset($_GET['label_sub']) === false) {
$where = "WHERE p.label_town = '$town' AND `visible` = 1";
} else {
$where = "WHERE `visible` = 1";
}

$adjacents = 3;

$query = "SELECT COUNT(p.page_id) as num FROM `data_page` AS p $where";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];

$targetpage = "index.php";
$limit = 1;

//how many items to show per page
    $page = $_GET['page'];
    if(isset($page)) { 
        $start = ($page - 1) * $limit;  //first item to display on this page
    } else {
        $start = 0; //if no page var is given, set start to 0
    }
// Example ##1##
$all_page_index = mysql_query("SELECT p.page_id, p.timestamp, p.label_town, p.label_sub, p.ime_nekretnine, p.mjesto, p.cijena_noc, p.krevet_apart, p.broj_apart, p.min_nocenja, p.description, p.visits, i.image_id, i.page_id, i.ext
FROM data_page AS p
LEFT JOIN (
SELECT MAX( image_id ) AS max, page_id
FROM images
GROUP BY page_id
) AS n ON p.page_id = n.page_id
LEFT JOIN images AS i ON i.image_id = n.max
$where
ORDER BY p.page_id DESC
LIMIT $start, $limit");

if ($page == 0) $page = 1;                  //if no page var is given, default to 1.
    $prev = $page - 1;                          //previous page is page - 1
    $next = $page + 1;                          //next page is page + 1
    $lastpage = ceil($total_pages/$limit);      //lastpage is = total pages / items per page, rounded up.
    $lpm1 = $lastpage - 1;                      //last page minus 1

    /* 
        Now we apply our rules and draw the pagination object. 
        We're actually saving the code to a variable in case we want to draw it more than once.
    */
    $pagination = "";
    if($lastpage > 1)
    {   
        $pagination .= "<div class=\"pagination\">";
        //previous button
        if ($page > 1) 
            $pagination.= "<a href=\"$targetpage?page=$prev\">&lsaquo;&lsaquo; previous</a>";
        else
            $pagination.= "<span class=\"disabled\">&lsaquo;&lsaquo; 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\">$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\">$counter</a>";                 
                }
                $pagination.= "...";
                $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";       
            }
            //in middle; hide some front and some back
            elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
            {
                $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
                $pagination.= "<a href=\"$targetpage?page=2\">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\">$counter</a>";                 
                }
                $pagination.= "...";
                $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";       
            }
            //close to end; only hide early pages
            else
            {
                $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
                $pagination.= "<a href=\"$targetpage?page=2\">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\">$counter</a>";                 
                }
            }
        }
        //next button
        if ($page < $counter - 1) 
            $pagination.= "<a href=\"$targetpage?page=$next\">next &rsaquo;&rsaquo;</a>";
        else
            $pagination.= "<span class=\"disabled\">next &rsaquo;&rsaquo;</span>";
        $pagination.= "</div>
";       
    }
?> 
<?php include "widgets/header.php"; ?>
<?php include "widgets/left_side.php"; ?>
<div class="middle">
<h2>Crnogorsko primorje<?php if(!empty($_GET['label_town'])) { echo ' - ' . $town; } ?></h2>
<?php if (mysql_num_rows($all_page_index) > 0) {

    while ($rowAllPage = mysql_fetch_array($all_page_index)) { ?>

    // Here is my loop, I remove code. It's too big.
}
}
?>

<?=$pagination?> 

There is no error in code. On index.php page works everything fine, but probelm is with $targetpage = "index.php". When I have condition url "index.php?label_town=bar&label_sub=apartmani" and when I click on pagination number browser redirect me to index.php?page=somenumber. I try to use $_SERVER['HTTP_REFERER'] but it doubled page=somenumber I also try

$targetpage = strtok($_SERVER['QUERY_STRING'], "page");

But this also doesn't work.

  • 写回答

1条回答 默认 最新

  • duanou1904 2013-04-14 14:36
    关注

    Ok! I solved a problem on this way. If anyone got better solution please share.

    Here is my. In if else statement I add $targetpage with different values:

    if (isset($_GET['label_town']) === true && isset($_GET['label_sub']) === true) {
    $where = "WHERE p.label_town = '$town' AND p.label_sub = '$sub' AND `visible` = 1"; 
    $targetpage = "index.php?label_town=$town&label_sub=$sub&";
    } else if (isset($_GET['label_town']) === true && isset($_GET['label_sub']) === false) {
    $where = "WHERE p.label_town = '$town' AND `visible` = 1";
    $targetpage = "index.php?label_town=$town&";
    } else {
    $where = "WHERE `visible` = 1";
    $targetpage = "index.php?";
    }
    

    Than make a change in every dynamic links like this:

    $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
    

    with this:

    $pagination.= "<a href=\"" . $targetpage ."page=$counter\">$counter</a>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛