当前位置:Gxlcms > PHP教程 > Fatalerror:Calltoamemberfunctionfpage()onanon-object

Fatalerror:Calltoamemberfunctionfpage()onanon-object

时间:2021-07-01 10:21:17 帮助过:21人阅读

smarty里面我通过调用分页函数,page.class.php已经在页面调用过,并且已经实例化,具体语句是这样的:
/*产品列表*/
function get_product_list($cat_id)
{
if($cat_id)
{
$num=6;
$where = " where classid='$cat_id' and shenhe=1 ";
$sql1 = $GLOBALS['db']->query("select * from ".$GLOBALS['db']->table('product').$where."");
$total = $GLOBALS['db']->num_rows($sql1);
}
else
{
$num=6;
$where = " where shenhe=1 ";
$sql2=$GLOBALS['db']->query("select * from ".$GLOBALS['db']->table('product').$where."");
$total=$GLOBALS['db']->num_rows($sql2);
}
$page=new page($total,$num);
//echo $total;
//exit();
$sql="select * from ".$GLOBALS['db']->table('product').$where."order by addtime desc {$page->limit}";
while($row=$GLOBALS['db']->fetch_array($sql))
{
$url=$GLOBALS['db']->rewrite_url('cpxx_xx',$row['productid']);
$product_list[]=array(
"id" => $row['productid'],
"title" => $row['title'],
"image" => $row['image']
);
}
return $product_list;
}
/*初始化数据*/
$smarty->assign('ur_here',$db->ur_here('product_class',$cat_id));
$smarty->assign('product_category',$db->get_product_category('')); //产品展示左侧分类列表
$smarty->assign('product_list',get_product_list($cat_id)); //产品列表
$smarty->assign('page_nav',$page->fpage(array(3,4,5,6,7,0,1,2,8))); //产品分页这句出错,看看哪位大神给解惑,不胜感激~!


回复讨论(解决方案)

$page=new page($total,$num);
检查生成的对象是否是有效对象。还有total和num的值。

$page=new page($total,$num);
检查生成的对象是否是有效对象。还有total和num的值。


Page Object ( [total:private] => 5 [listRows:private] => 6 [limit:private] => Limit 0, 6 [uri:private] => /zangbaoshengwu/cpxx.php? [pageNum:private] => 1 [config:private] => Array ( [header] => 个记录 [prev] => 上一页 [next] => 下一页 [first] => 首 页 [last] => 尾 页 ) [listNum:private] => 8 [page] => 1 )
$total,$num都是有值的

$page 是在函数中初始化的。也就是他是一个局部变量,当然你不能在函数外使用。

$page 是在函数中初始化的。也就是他是一个局部变量,当然你不能在函数外使用。


那具体我应该怎么写呢?在函数内该怎么写?

作为函数的返回值返回,例如:
return $product_list;
改为 return array(
'list'=>$product_list,
'fpage'=>$page->fpage(array(3,4,5,6,7,0,1,2,8)),
)

作为函数的返回值返回,例如:
return $product_list;
改为 return array(
'list'=>$product_list,
'fpage'=>$page->fpage(array(3,4,5,6,7,0,1,2,8)),
)


理解这个意思,但是不太懂怎么去实现,而smarty我怎么写?
$smarty->assign('product_list',get_product_list($cat_id)); //产品列表
$smarty->assign('page_nav',$page->fpage(array(3,4,5,6,7,0,1,2,8))); //产品分页
就是这里。

函数返回值改成#5那样写。

$smarty->assign('product_list',get_product_list($cat_id)); //产品列表
$smarty->assign('page_nav',$page->fpage(array(3,4,5,6,7,0,1,2,8)));
改为:
$ar = get_product_list($cat_id);
$smarty->assign('product_list',$ar['list']); //产品列表
$smarty->assign('page_nav',$ar['fpage']);

函数返回值改成#5那样写。

$smarty->assign('product_list',get_product_list($cat_id)); //产品列表
$smarty->assign('page_nav',$page->fpage(array(3,4,5,6,7,0,1,2,8)));
改为:
$ar = get_product_list($cat_id);
$smarty->assign('product_list',$ar['list']); //产品列表
$smarty->assign('page_nav',$ar['fpage']);


怎么打印出的数组值list是空的:
Array ( [list] => [fpage] => 1 共有5个记录 每页显示5条,本页1-5条 1/1页 )
下面是完整的程序:
/*产品列表*/
function get_product_list($cat_id)
{
if($cat_id)
{
$num=6;
$where = " where classid=$cat_id and shenhe=1 ";
$sql1 = $GLOBALS['db']->query("select * from ".$GLOBALS['db']->table('product').$where."");
$total = $GLOBALS['db']->num_rows($sql1);
}
else
{
$num=6;
$where = " where shenhe=1 ";
$sql2=$GLOBALS['db']->query("select * from ".$GLOBALS['db']->table('product').$where."");
$total=$GLOBALS['db']->num_rows($sql2);
}
$page=new page($total,$num);
$sql="select * from ".$GLOBALS['db']->table('product').$where."order by addtime desc {$page->limit}";
while($row=$GLOBALS['db']->fetch_array($sql))
{
$url=$GLOBALS['db']->rewrite_url('cpxx_xx',$row['productid']);
$product_list[] = array(
"id" => $row['productid'],
"title" => $row['title'],
"image" => $row['image']
);
}
//return $product_list;
return array(
'list' => $product_list,
'fpage' => $page->fpage(array(3,4,5,6,7,0,1,2,8))
);
}

/*初始化数据*/
$smarty->assign('ur_here',$db->ur_here('product_class',$cat_id));
$smarty->assign('product_category',$db->get_product_category('')); //产品展示左侧分类列表
$ar = get_product_list($cat_id);

print_r($ar);

$smarty->assign('product_list',$ar['list']); //产品列表
$smarty->assign('page_nav',$ar['fpage']);//产品分页
实在找不到哪错了呢?

function get_product_list($cat_id)
{
global $page; //加上这句
if($cat_id)
..........

其他无需改动

对不起啦,是我自己把SQL查询语句写错了:
$sql="select * from ".$GLOBALS['db']->table('product').$where."order by addtime desc {$page->limit}";
while($row=$GLOBALS['db']->fetch_array($sql))
这里少了$GLOBALS['db']-query();

各位不好意思,分就只有那么多了,分的有点少,大家多担待~

人气教程排行