php访问第2页后,松散了分页的价值

时间:2014-04-16 05:35:15

标签: php pagination

我正在使用php分页,但问题是当我在分页中进入第2页时,php从超链接中删除了我从最后一页请求的值,并得到错误,

  

未定义索引:E中的title_no:第5行的........ \ product.php

但当我使用静态值时,如$ title_no = 1;然后它在分页的所有页面中工作得很好,但在动态中它的价值松散,这是我遇到问题的分页代码

ob_start();
@session_start();
include("config.php");
$tbl_name="products";
$title_no=$_GET['title_no'];

$adjacents = 2;

$productQuery = " where visible='1' AND title_no='$title_no'"; 
$query = "SELECT COUNT(*) as num FROM $tbl_name $productQuery";
$total_pages_row = mysql_query($query);
$total_pages_num = mysql_fetch_assoc($total_pages_row);
$total_pages = $total_pages_num['num'];

$targetpage = "product.php";    //your file name  (the name of this file)
$limit_val = @$_GET['pagesize'];
if($limit_val!='') {
    $limit = $limit_val;
} else {
    $limit = 3;             //how many items to show per page
}
if(!isset($_GET['page']) || $_GET['page']==""){
$page = "1";
}else{
// If page is set, let's get it
$page = $_GET['page'];
}
if($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

/* Get data. */
$sql = "SELECT * FROM $tbl_name $productQuery LIMIT $start, $limit";
//echo $sql;
$result = mysql_query($sql);

/* Setup page vars for display. */
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.
*/
$linkvariable = "";


$pagination = "";
if($lastpage > 1)
{   
    $pagination .= "<div class=\"pagination\">";
    //previous button
    if ($page > 1) 
        $pagination.= "<a href=\"$targetpage?page=$prev&q=sch&pagesize=$limit$linkvariable\">« 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&q=sch&pagesize=$limit$linkvariable\">$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&q=sch&pagesize=$limit$linkvariable\">$counter</a>";      
            }
            $pagination.= "...";
            $pagination.= "<a href=\"$targetpage?page=$lpm1&q=sch&pagesize=$limit$linkvariable\">$lpm1</a>";
            $pagination.= "<a href=\"$targetpage?page=$lastpage&q=sch&pagesize=$limit$linkvariable\">$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&q=sch&pagesize=$limit$linkvariable\">$counter</a>";      
            }
            $pagination.= "...";
            $pagination.= "<a href=\"$targetpage?page=$lpm1&q=sch&pagesize=$limit$linkvariable\">$lpm1</a>";
            $pagination.= "<a href=\"$targetpage?page=$lastpage&q=sch&pagesize=$limit$linkvariable\">$lastpage</a>";        
        }
        //close to end; only hide early pages
        else
        {
            $pagination.= "<a href=\"$targetpage?page=1&q=sch&pagesize=$limit$linkvariable\">1</a>";
            $pagination.= "<a href=\"$targetpage?page=2&q=sch&pagesize=$limit$linkvariable\">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&q=sch&pagesize=$limit$linkvariable\">$counter</a>";

            }
        }
    }

    //next button
    if ($page < $counter - 1) 
            $pagination.= "<a href=\"$targetpage?page=$next&q=sch&pagesize=$limit$linkvariable\">next »</a>";
    else
        $pagination.= "<span class=\"disabled\">next »</span>";
    $pagination.= "</div>\n";
//}
}

请指导我出错的地方

由于

2 个答案:

答案 0 :(得分:1)

请尝试以下操作。

if(isset($_GET['title_no']) && $_GET['title_no'] != NULL) {
      $_SESSION['title_no'] = $_GET['title_no'];
    }

$productQuery = " where visible='1' AND title_no='".$_SESSION['title_no']."'";

答案 1 :(得分:1)

嗯,问题是你正在尝试检查一个尚不存在的值。

$_GET是一个数组,只有在提交表单后才能获取值,因此在加载时它会显示未定义的索引错误。

来自 docs

  

尝试访问尚未定义的数组键是   与访问任何其他未定义变量相同:E_NOTICE级别   将发出错误消息,结果将为NULL。

因此,此类值的常用方法是首先使用 isset() 检查它们是否存在:

$title_no = isset($_GET['title_no'])?$_GET['title_no']:1; //using ternary operators

这与写作相同:

if(isset($_GET['title_no'])){
   $title_no = $_GET['title_no'];
}
else
{
   $title_no = 1;
}

isset()$_GET个变量的任何地方使用$_POST功能,并且不要像完成@$_GET['pagesize'];那样抑制错误,并使用{{1}修复它如上所示。