简体   繁体   English

php访问第二页后分页中的值

[英]php loose the value in pagination after visit 2nd page

I'm using php pagination, but problem is when I go to 2nd page in pagination, php loses the value which I REQUEST from the last page from hyperlink, and the error is: 我正在使用php分页,但是问题是当我转到分页的第二页时,php丢失了我从超链接的最后一页请求的值,并且错误是:

Undefined index: title_no in E:........\\product.php on line 5 未定义的索引:第5行的E:........ \\ product.php中的title_no

but when I'm using a static value like $title_no=1; 但是当我使用$title_no=1;类的静态值时$title_no=1; then it works fine in all pages in pagination, but in dynamic it's loses the value, This is code of pagination where I'm getting problem 那么它在分页中的所有页面上都可以正常工作,但是在动态情况下它会失去价值,这是我遇到问题的分页代码

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";
//}
}

Please guide me where I'm going wrong 请指导我我要去哪里了

Thanks 谢谢

Try the following. 尝试以下方法。

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']."'";

Well, the problem is you're trying to check for a value which does not yet exist. 好吧,问题是您正在尝试检查尚不存在的值。

The $_GET is an array and only gets values when you have submitted the form, so on load it will show you the undefined index error. $_GET是一个数组,仅在提交表单后才获取值,因此在加载时它将显示未定义的索引错误。

From the docs : 文档

Attempting to access an array key which has not been defined is the same as accessing any other undefined variable: an E_NOTICE-level error message will be issued, and the result will be NULL. 尝试访问尚未定义的数组键与访问任何其他未定义的变量相同:将发出E_NOTICE级错误消息,并且结果为NULL。

So the usual method for such values is to check whether they exist first using isset() : 因此,此类值的常用方法是使用isset()首先检查它们是否存在:

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

This is the same as writing: 这与写作相同:

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

Use the isset() function wherever you have $_GET or $_POST variables and do not suppress the errors like you have done @$_GET['pagesize']; 在有$_GET$_POST变量的任何地方使用isset()函数,不要像执行@$_GET['pagesize'];那样抑制错误@$_GET['pagesize']; , fix it with the isset() as shown above. ,使用isset()修复,如上所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM