简体   繁体   English

分页下一页应为2,但应为0

[英]Pagination next page should be 2 but is 0

The first page should be 1 but its -1 and the next page should be 2 but its 0. 第一页应为1,但其为-1,下一页应为2,但其为0。

How can I make this work? 我该如何进行这项工作? I have tried to edit the $page but when I do, the if/else get wierd. 我试图编辑$ page,但是当我这样做时,if / else变得很奇怪。

I added the whole code... 我添加了整个代码...

 ...
$resultt= "SELECT * FROM users WHERE AND country='$okc' AND state='$oks'";

if(!empty($a1) && empty($a2)){
  $resultt .= " AND aar>=";
  ...
}

There are more than 1 if statement. 超过1个if语句。

         $result = mysqli_query($con,"$resultt");
         $rec_limit = 19;
         $row = mysqli_fetch_array($result);
         $rec_count = $row[0];

         if( isset($_GET['page'] ) )
         {
            $page = $_GET['page'] + 1;
            $offset = $rec_limit * $page ;
         }
         else
         {
            $page = 0;
            $offset = 0;
         }
         $left_rec = $rec_count - ($page * $rec_limit);

         $resultt .= "LIMIT $offset, $rec_limit";
         $sql = $resultt;

         $retval = mysqli_query( $con, "$resultt" );

         if( $page > 0 )
         {
            $last = $page - 1;
            echo "<a href=\"$_PHP_SELF?page=$last\">Last 10 Records</a> |";
            echo "<a href=\"$_PHP_SELF?page=$page\">Next 10 Records</a>";
         }

         else if( $page == 0 )
         {
            echo "<a href=\"$_PHP_SELF?page=$page\">Next 10 Records</a>";
            }

         else if( $left_rec < $rec_limit )
         {
            $last = $page - 2;
            echo "<a href=\"$_PHP_SELF?page=$last\">Last 10 Records</a>";
         }

while($row = mysqli_fetch_array($retval, MYSQL_ASSOC))
{

}

$_GET is an array like your $row. $ _GET是类似于$ row的数组。

to access array keys you always use []. 要访问数组键,请始终使用[]。

change your get into $_GET['page'] 将您的钱更改为$ _GET ['page']

even better: 甚至更好:

$page = isset($_GET['page']) ? intval($_GET['page']) : 1;

is page set then convert value to int, because you dont know whats incomming. 是页面集,然后将值转换为int,因为您不知道要输入什么。 if not set take page 1. 如果未设置,请选择第1页。

btw. 顺便说一句 constructs like this: 像这样的构造:

$resultt = "LIMIT $offset, $rec_limit";

can/will cause sql injections bugs within you homepage! 会/将导致您首页中的sql注入错误! never build a sql with variables! 永远不要用变量构建sql! Use parameters instead! 请改用参数!

You should change parts of code to get it to work. 您应该更改部分代码才能使其正常工作。 $_GET['page'] should give you the queried page number itself, so don't try to manipulate it. $_GET['page']应该给您查询的页码本身,所以不要尝试操纵它。

$sql = "SELECT * FROM users WHERE country='$okc' AND state='$oks'"
$rec_limit = 10;
$result = mysqli_query( $con, $sql );
$row = mysqli_fetch_array( $result );
$rec_count = $row[0];
$left_rec = $rec_count - ( $rec_limit * $page );


$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$offset = $rec_limit * ( $page - 1 ) ;

$sql.= " LIMIT $rec_limit OFFSET $offset";
$result = mysqli_query( $con, $sql );

$row = mysqli_fetch_array( $result );

if( $page > 1 ){
    $last = $page - 1;
    echo "<a href=\"$_PHP_SELF?page=$last\">Last 10 Records</a> |";
    echo "<a href=\"$_PHP_SELF?page=$page\">Next 10 Records</a>";
}else if( $page == 1 ){
    echo "<a href=\"$_PHP_SELF?page=$page\">Next 10 Records</a>";
}else if( $left_rec < $rec_limit ){
    $last = $page - 2;
    echo "<a href=\"$_PHP_SELF?page=$last\">Last 10 Records</a>";
}

while( $row = mysqli_fetch_array( $result, MYSQL_ASSOC ) ){

    // use $row to echo row contents ...

}

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

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