Hi all,

Basically, here's what I'm trying to do.

I have a DB table called products_ssd.

This table contains the columns named:

- id
- manufacturer
- item
- description
- price
- size
- pic


This table contains the information for SSD drives, from varying manufacturers.


Now, to the code I currently have.

I have created a parent class called SSD. The code is below:

class ssd{
	
	public $item;
	public $manufacturer;
	public $pic;
	public $description;
	public $size;
	public $price;
	
	public function __construct(){

		$q = mysql_query("SELECT * FROM product_ssd") or die(mysql_error());	
		while ($ssd = mysql_fetch_assoc($q))
		{
			 $this->item = $ssd['item'];
			 $this->manufacturer = $ssd['manufacturer'];
			 $this->description = $ssd['description'];
			 $this->pic = $ssd['pic'];
			 $this->price = "$" . $ssd['price'];
			 $this->size = $ssd['size'];
		}
	}
}

This will get ALL the SSD drives from the database.


Next, I have a child class. The intent of this class is to get all the SSD drives, from the manufacturer named OCZ:

class ocz extends ssd{

	public function show(){
		echo $this->item;
	}
}

As it is right now, it will echo out ALL the item columns, regardless of manufacturer.
How can I adjust the OCZ class so that it modifies the MySQL query to select only items where manufacturer = 'OCZ'?

Thanks in advance for any advice!

Member Avatar for diafol

I think this is a bit extreme - are you going to create a child class for every manufacturer? I don't think this is maintainable. If you add a manufacturer, you also have to change your source code. Not good.

Anyway

does creating an object have to have all the records?
Why not have a function like:

public function getSSD($man=false){
   $where = "";
   if($man){
      $man = mysql_real_escape_string($man);
      $where = " WHERE manufacturer = '$man'";
   }
   $q = mysql_query("SELECT ... FROM table{$where}");
   $records = false;
    while ($ssd = mysql_fetch_assoc($q)){
      $records[] = $ssd;
   }
   return $records;
}

I'm sure evstevemd will say use PDO or mysqli, but that's beyond the scope of the question (IMO).

You can use the 'parent' class, I don't see the need for a child class.

Your first example will only return the last entry as you overwrite in every iteration of the loop. You also have loads of public variables. You can 'clean it up' by returning an array - this is more versatile for the external code.

$ssd_array = $sdd->getSSD('OCZ');

if($ssd_array){
  ...
}

that was off the top of my head and i'm a noob at OOP. Anybody else?

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.