Hello!
What I am trying to do is display a list of categories and another list of brands.
CategoryList : class
BrandList : class

I am getting only the list of classes displayed. Can anyone tell me what is wrong with my code..

<?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>";
				
}
		
?>

Thanks
:)

vedro-compota commented: ++++++++ +1

It's a bit hard to tell from the code you have posted. Do you have code for the classes in question that you can post?

Thanks for your reply.
I forgot to mention one thing. When I removed the codes for categories, the brands are being displayed. Is it because I cannot create instance of two different classes in one php page?:-/
Here's the code for the classes:

<?php

class CategoryList
{
	public $mSelectedCategory = 0;
	public $mCategory;
	
	
	public function __construct()
	{
		if (isset ($_GET['category_id']))
			$this->$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();
		
	}
	
	
}

?>

You can create as many instances of as many classes as you like (up to your memory limit). I will need to see your Catalog class as well in order to see what is going on, as the init() function in both your classes makes use of this.

Not sure what you are doing , but perhaps you need a return statement for the current object pointed to by the the $this pointer?
If you don't need a return value, try not using the pointer just to do background variable setting.
I don't know if this applies to your issue, but some thoughts on the subject anyway...

Here's the catalog class

class Catalog
{
	//get id and name of category
	public static function GetCategory()
	{
		$sql = 'CALL catalog_get_category_list()';
		return DatabaseHandler::GetAll($sql);
					
	}
	//get id and name of brand
	public static function GetBrand()
	{
		$sql = 'CALL catalog_get_brands_list()';
		return DatabaseHandler::GetAll($sql);
					
	}
}

DatabaseHandler class

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;

Someone suggested me to try with a dummy implementation of the two classes like:

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'), 
    ); 
  } 
}

And this actually works..Now I need to figure out what's wrong with my classes :S

if the dummy works, then your Catalog class must not be returning the array.
Are you sure the query happened?
var_dump your way backwards until you find what's not working.

Yes the catalog class actually returns the array..when i place the code for brand before the categories, the brands are being displayed. Its quite confusing...Im going to try what you suggested. Thanks JRM

Hello again! i have been able to solve this problem by replacing the stored procedure

$sql = 'CALL catalog_get_category_list()';

by an sql statement:

$sql = 'SELECT id,name FROM category;';

Does that mean I will not be able to use stored procedures anymore?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.