简体   繁体   English

PHP分页,出了点问题

[英]php pagination, something is wrong

I found a tutorial according pagination. 我根据分页找到了一个教程。 http://papermashup.com/easy-php-pagination/ http://papermashup.com/easy-php-pagination/

I tried the code and it work. 我尝试了代码,它可以工作。 but every time the first index is reload(first result in pagination). 但每次第一个索引被重载时(分页的第一个结果)。

I got this message. 我收到此消息。

Notice: Undefined index: page in C:\\xampp\\htdocs\\sim\\registrar\\index.php on line 207 注意:未定义的索引:第207行的C:\\ xampp \\ htdocs \\ sim \\ registrar \\ index.php中的页面

how can I remove this message? 如何删除此消息?

here's the code.. 这是代码。

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

$query = "SELECT COUNT(*) as num FROM advisoryclass";

$total_pages = mysql_fetch_array(mysql_query($query));

$total_pages = $total_pages['num'];

$stages = 3;

    $page = mysql_escape_string($_GET['page']);//this part

if($page){
$start = ($page - 1) * $limit;

}else{

$start = 0;
}

thanks 谢谢

You're assuming page is set when that's not always going to be the case; 您假设page设置并非总是如此; hence the error. 因此错误。

Change this: 更改此:

$page = mysql_escape_string($_GET['page']);//this part

To: 至:

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

In the code above we're checking the page $_GET variable has been set and using 1 as a fallback. 在上面的代码中,我们正在检查页面$_GET变量是否已设置并使用1作为后备。

You may want to look at the functions you're using with the database too. 您可能还需要查看与数据库一起使用的功能。

add a check in order to see if the parameter is in the get array: 添加检查以查看参数是否在get数组中:

$page = mysql_escape_string(isset($_GET['page']) ? $_GET['page'] : 0);

this is a ternary comparison in order to make it shorter than an if statement. 这是三元比较,以使其比if语句短。

如果$ page不存在,则需要处理。

$page = mysql_escape_string(isset($_GET['page']) ? $_GET['page'] : 0);

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

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