我试图显示两个列表:一个用于类别和品牌,但只有类别正在显示。当我删除类别的代码时,品牌正在显示。是否因为无法在同一个php页面中创建两个类的实例? 在的index.php:创建两个类的实例
<?php
$obj = new CategoryList();
if (method_exists($obj, 'init'))
{
$obj->init();
}
for($i = 0;$i< count($obj->mCategory); $i++)
{
echo "<a href=''>";
echo $obj->mCategory[$i]['name']. "<br/>";
echo "</a>";
}
$obj2 = new BrandList();
if (method_exists($obj2, 'init'))
{
$obj2->init();
}
for($i = 0;$i< count($obj2->mBrand);$i++)
{
echo "<a href=''>";
echo $obj2->mBrand[$i]['name']. "<br/>";
echo "</a>";
}
?>
下面是类的代码:
$ mSelectedCategory =(int)的$ _ GET [ 'CATEGORY_ID']; () } public function init() {this-> mCategory = Catalog :: GetCategory(); } } ><?php
class BrandList
{
public $mSelectedBrand = 0;
public $mBrand;
public function __construct()
{
if (isset ($_GET['brand_id']))
$this->$mSelectedBrand = (int)$_GET['brand_id'];
}
public function init()
{
$this->mBrand = Catalog::GetBrand();
}
}
?>
也许这可能帮助:?
在数据库处理器类class Catalog
{
//get id and name of category
public static function GetCategory()
{
$sql = 'CALL catalog_get_category_list()';
return DatabaseHandler::GetAll($sql);
}
public static function GetBrand()
{
$sql = 'CALL catalog_get_brands_list()';
return DatabaseHandler::GetAll($sql);
}
}
:
public static function GetAll($sqlQuery, $params = null, $fetchStyle = PDO::FETCH_ASSOC)
{
$result = null;
try
{
$database_handler = self::GetHandler();
$statement_handler = $database_handler->prepare($sqlQuery);
$statement_handler->execute($params);
$result = $statement_handler->fetchAll($fetchStyle);
}
catch(PDOException $e)
{
self::Close();
trigger_error($e->getMessage(), E_USER_ERROR);
}
return $result;
}
如果您正在浏览器上查看,请查看HTML源代码以查看品牌是否实际打印。如果不是,您的BrandList对象没有被填充条目,或者存在错误,可能是在构造函数或init()函数中。 – Anurag 2009-12-31 11:23:47
我已经通过html源代码,只有类别正在打印.. – chupinette 2009-12-31 11:34:01
'$ this - > $ mSelectedBrand' - $ $太多,'$ this-> mSelectedBrand'。你是否修改了Stackoverflow的代码,或者这真的是一个错误? – VolkerK 2009-12-31 11:46:11