【问题标题】:php loose the value in pagination after visit 2nd pagephp在访问第二页后会丢失分页中的值
【发布时间】:2019-08-03 17:30:53
【问题描述】:

我正在使用php分页,但问题是当我进入分页的第二页时,php丢失了我从超链接的最后一页请求的值,错误是:

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

但是当我使用像 $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";
//}
}

请指导我哪里出错了

谢谢

【问题讨论】:

  • $title_no=isset($_GET['title_no'])?$_GET['title_no']:''; :之后的默认值
  • 非常感谢@ICanHasCheezburger,它确实有效:),请您指导我的代码中的问题是什么,以及您是如何解决的,请您提供它的教程链接吗?再次感谢

标签: php pagination


【解决方案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']."'";

【讨论】:

    【解决方案2】:

    好吧,问题是您正在尝试检查一个尚不存在的值。

    $_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;
    }
    

    在有$_GET$_POST 变量的任何地方使用isset() 函数,不要像@$_GET['pagesize']; 那样抑制错误,如上所示使用isset() 修复它。

    【讨论】:

    • 感谢@ICanHasCheezburger 给我宝贵的时间,非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    相关资源
    最近更新 更多