2014-10-30 90 views
-3

我得到的错误: 注意:未定义指数:对,页,SRT等在那里PHP分页值误差

// set default pagenation values 
if(!$_GET['p']) $_GET['p'] = $conf['perpage']; // default number of entries per page 
if(!$_GET['page']) $_GET['page'] = 1; // current page 
if(!$_GET['srt']) $_GET['srt'] = $conf['srt']; // default sort order 
$start = ($_GET['page'] - 1) * $_GET['p']; // start row for query 

// get total number of entries for pagenation 
$result = mysql_query("SELECT COUNT(*) FROM articals $where", $link); 
$total = mysql_fetch_array($result); $total = $total[0]; // total number of listings 
$pages = ceil($total/$_GET['p']); // number of pages 

兼谈: 注意:未定义指数:类别,描述,型号,条件,位置,照片,特色和在哪里

$_GET = safe_data($_GET, 'query'); 

if($_GET['category']) $where .= "AND (category='$_GET[category]' OR category2='$_GET[category]') "; 
if($_GET['description']) $where .= "AND description LIKE '%$_GET[description]%' "; 
if($_GET['model']) $where .= "AND model LIKE '%$_GET[model]%' "; 
if($_GET['cond']) $where .= "AND cond='$_GET[cond]' "; 


if($_GET['location']) $where .= "AND location='$_GET[location]' "; 
if($_GET['photos']) $where .= "AND images>'0' "; 
if($_GET['featured']) $where .= "AND featured='1' "; 

// finialize query string if necessary 
if(substr($where, 0, 3) == 'AND') { 
    $where = ltrim($where, 'AND'); 
    $where = 'WHERE'.$where; 
} 

// do not show hidden and expired listings 
$display = "hide!='1' AND (expire>'".time()."' OR expire='' OR expire='0') AND (user_expire>'".time()."' OR user_expire='' OR user_expire='0')"; 
$display = "hide!='1'"; 
if(!$where) $where = "WHERE ".$display; 
else $where .= "AND ".$display; 

请帮忙吗?

+0

这意味着您的http请求在url中没有'?p = [您的数据]' – 2014-10-30 10:53:40

+0

尝试if(isset($ _ GET ['p'])) – 2014-10-30 10:54:24

+0

[PHP:“可能的重复:Undefined variable “和”注意:未定义的索引“](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – andrewsi 2014-10-31 00:48:02

回答

-2

在你的网址中,你有参数如http://www.yoursite.com/yourpage.php?p=XXX&page=YYYY&srt=ZZZZ

如果这些参数是optionnal,使用方法:

if (!isset($_GET["p"])) $_GET["p"] = $conf["perpage"]; 

但实际上,更好的做法是不要自行分配$ _GET。相反,设置一个变量,然后用它来代替使用$ _GET内容:

$p = (isset($_GET["p"])?$_GET["p"]:$conf["perpage"]); 

如果你是做在你的htaccess的重定向,请务必转发使用[QSA]的GET参数。

0

你必须包装isset()围绕每一个获取请求摆脱通知。

该通知显示是因为它们没有价值。

您可以使用它像这样:if(isset($_GET['p']))

0

此代码可以帮助你,它会包裹HTTP PHP响应:

class Input { 

    public static function Post($item,$default=null){ 
     //If the value was posted, then return that value, else return the $default value. 
     return isset($_POST[$item]) ? $_POST[$item] : $default; 
    } 

    public static function Get($item,$default=null){ 
     return isset($_GET[$item]) ? $_GET[$item] : $default; 
    } 

    public static function Cookie($item,$default=null){ 
     return isset($_COOKIE[$item]) ? $_COOKIE[$item] : $default; 
    } 

    public static function Param($item,$default=null){ 
     return self::Post($item) ?: self::Get($item) ?: self::Cookie($item) ?: $default; 
    } 

} 

用法:

Input::Get('p',$conf['perpage']); // First argument is the index, second is default if not set. 
在你的榜样

$p = Input::Get('p',$conf['perpage']); 
$page = Input::Get('page',1); 
$srt= Input::Get('page',$conf['srt']); 

$start = ($page - 1) *$p; // start row for query 
+1

方便,但'Param()'应该使用' $ _REQUEST',或者至少是'variables_order' http://php.net/manual/en/ini.core.php#ini.variables-order – rjdown 2014-10-30 11:05:25