Page 1 of 1

large poduct design question - objects

Posted: Mon Nov 14, 2011 11:31 am
by nullroute
Hello

I am currently designing a product catalog and I have run into a design issue. I am new to php however familiar with C++.

The below is a class containing a function which is designed to query my database of products and return a single products row from the database containing the products data to be displayed in say product.php to the user.

Based on my understanding it is good practice to keep all logic within the classes and have basic output within the php files the user will be interacting with.

The below however returns an object array of products. The instantiation is done within products.php also attached below. The problem is that I believe that returning an object array or an array of product objects is overkill. I simply want to access the function query_product_db() within the class and return a single array containing product information. How would this be performed? This seems to contradict the idea of an object oriented programming language. Additionally, if I am forced to return an array of product objects would it not be wise to deconstruct the array to save memory? Each time a user views the product page a new product object array would be built.

Any suggestions would be helpful.

Currently its generating an array of product arrays. Would rather return just the single array.

products class:

Code: Select all

class product {	
onta
var $item_id;
var $product_id;
var $product_result;

	function query_product_db()  //function to query mySQL product database based on item variable matching primary key. 
	{
	    $item_id = $_GET['item'];  //obtain variables from user 
		if (!preg_match("/^[0-9]{1,2}$/", $item_id)) die("Bad Request.");  //security check via regular expression on item.
	
	 	$query  = "SELECT * FROM products WHERE product_key=$item_id" ;   //select all from products database where primary key equals item.
		$query_result = mysql_query($query);  
		$product_result = mysql_fetch_row($query_result);  //fetch mySQL product row based on primary key stored as an array.
		return $product_result;  //return array.
	
	}
	
}
product.php

Code: Select all


include 'class_products.php';


$product1 = new product();
echo $product1->query_product_db();


?>

Re: large poduct design question - objects

Posted: Mon Nov 14, 2011 11:43 am
by Celauran
Might I ask why you aren't passing the ID into the function as an argument? It should return only a single array.

Re: large poduct design question - objects

Posted: Mon Nov 14, 2011 3:53 pm
by nullroute
Rusty mostly.....passed the variable ...fixed the problem. Thanks for the help.