2009-12-31 124 views
0

我试图显示两个列表:一个用于类别和品牌,但只有类别正在显示。当我删除类别的代码时,品牌正在显示。是否因为无法在同一个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; 
     } 
+0

如果您正在浏览器上查看,请查看HTML源代码以查看品牌是否实际打印。如果不是,您的BrandList对象没有被填充条目,或者存在错误,可能是在构造函数或init()函数中。 – Anurag 2009-12-31 11:23:47

+0

我已经通过html源代码,只有类别正在打印.. – chupinette 2009-12-31 11:34:01

+0

'$ this - > $ mSelectedBrand' - $ $太多,'$ this-> mSelectedBrand'。你是否修改了Stackoverflow的代码,或者这真的是一个错误? – VolkerK 2009-12-31 11:46:11

回答

1

可以实例几十/上百/ X的对象在同一个php实例中的任何类型。
使用调试器或添加更多调试(回显)代码来查找错误。

例如使用这种虚拟实现这两类

class CategoryList { 
    public $mCategory=null; 
    public function init() { 
    $this->mCategory = array(
     array('name'=>'Cat A'), 
     array('name'=>'Cat B'), 
    ); 
    } 
} 

class BrandList { 
    public $mBrand=null; 
    public function init() { 
    $this->mBrand = array(
     array('name'=>'Brand A'), 
     array('name'=>'Brand B'), 
    ); 
    } 
} 

你的代码打印

<a href=''>Cat A<br/></a><a href=''>Cat B<br/></a><a href=''>Brand A<br/></a><a href=''>Brand B<br/></a> 

没有任何问题。

+0

它实际上显示了这个虚拟实现的类别和品牌.. – chupinette 2009-12-31 11:43:16

+0

是的,这意味着:你吠叫错误的树;-)你最初发布的代码不是问题的原因。 – VolkerK 2009-12-31 11:49:16

+0

雅你是对的。也许从其他班级获得价值观的方式存在问题。我已经包含类目录代码 – chupinette 2009-12-31 12:16:59

1

不,您可以为同一个类创建任意数量的实例。确保您的两个课程都独立于您的脚本。

+0

感谢您的答复..似乎没有任何错误。是的,我已经包括这两个类。但任何人都可以解释为什么当我删除一个列表的代码,另一个列表正在显示?我已经尝试了很多方法......但无法找到解决方案 – chupinette 2009-12-31 11:15:32

+0

没有看到这两个类的代码,这是纯粹的猜测。 – VolkerK 2009-12-31 11:17:52