Jump to content

How I look for to categories of products using Data Acess Object?


robsonrdasilva

Recommended Posts

Hey, what's up?

I'm making a site control panel and I've a question about a product listing.

 

I'm using a DAO class (ProductDAO), which became all register of products from MySQL database.

 

The structure of this class is (for example):

 

class ProductDAO
{
public function get_products()
{
	$sql = 'SELECT * FROM products';
	$query = mysql_query($sql);

	return mysql_fetch_object($query, 'Product');
}
}

 

And the Product class

 

class Product
{
   private $id_product;
   private $id_category;
   private $name;
}

 

It is working very well, the function get_products return a array of products with class Product, but now I need to look for the category name from each product.

 

The class Category has the structure:

 

class Category
{
   private $id_category;
   private $name;
}

 

And to return a category name I change my function get_products:

 

class ProductDAO
{
public function get_products()
{
	$sql = 'SELECT products.*, categories.name as category_name FROM products INNER JOIN categories ON products.id_category = categories.id_category';
	$query = mysql_query($sql);

	return mysql_fetch_object($query, 'Product');
}
}

 

But in class Product I don't have the variable category_name.

 

How do I return those products to listing?

 

I need to create a DAO class for categories and for each item of products that I list in my site I look for the specific category?

 

Sorry, my english isn't so good.

Thanks.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.