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();
各位不好意思,分就只有那麼多了,分的有點少,大家多擔待~