Forum Moderators: coopster
Here's what I'd like to know, with a pseudo code example. Say a user is logged in with an account and they have a list of grocery items. I'm hitting the DB and feeding each line of the returned info into an object like so:
class GroceryItem {
protected $_name;
protected $_id;
protected $_price; public function __construct($attributes){
$this->$_name = $attributes['name'];
$this->$_id = $attributes['id'];
$this->$_price = $attributes['price'];
}
public function getName(){
return $this->_name;
}
public function getId(){
return $this->_id;
}
public function getPrice(){
return $this->_price;
}
public function printOut(){
return "name=". $this->getName()
." price=$". $this->getPrice()
."<a href='/process.php?removeItem=". $this->getId() ."'>remove</a><br />";
}
}
//Table "item" has columns id,name,price,user_id
$q = "SELECT * FROM item WHERE user_id = 1";
$result = $database->query($q);
while($array = @mysql_fetch_assoc($result)){
$item = new GroceryItem($array);
echo $item->printOut();
}
/* Sample output:
name=Pickles price=$5 remove
name=Eggs price=$2 remove
name=Chips price=$3 remove
*/
So, essentially what you'd have is a list of all the items in their cart with a link to remove them if they wanted. The link goes to process.php which would see the GET var, grab the item id and use that to remove the item.
My question is, how exactly should this removal work? Right now what I'm doing is I have a static function in the GroceryItem class that I'm passing the id to like so:
//in GroceryItem class
static function remove($id){
global $database;
$q = "DELETE FROM item WHERE id =".$id;
return mysql_query($q, $database->connection);
}//in process.php
if(isset($_GET['removeItem']){
$itemId = $_GET['removeItem'];
GroceryItem::remove($itemId);
}
But should I somehow be passing the grocery item objects from one page to the next and using a nonstatic method like so?
//in GroceryItem class
public function remove(){
global $database;
$q = "DELETE FROM item WHERE id =".$this->getId();
return mysql_query($q, $database->connection);
}
I'm a bit confused about how objects should be passed around, if at all. It seems like if I was passing the objects as a whole, I'd be able to more easily manipulate their contents with methods like setName() or setPrice() without hitting the DB all the time. Also, just for the sake of argument, lets say I want to keep track of the number of items the user has as a separate field in the DB. When I remove an item this needs to decrease the number in the DB by one. How would this look? Like this?
//in GroceryItem class
static function decreaseTotal(){
global $database;
$q = "UPDATE user SET total_items = total_items -1 WHERE id = 1";
return mysql_query($q, $database->connection);
}
static function remove($id){
global $database;
$q = "DELETE FROM item WHERE id =".$id;
if(mysql_query($q, $database->connection)){
return self::decreaseTotal();
} else {
return false;
}
}
//in process.php
if(isset($_GET['removeItem']){
$itemId = $_GET['removeItem'];
GroceryItem::remove($itemId)
GroceryItem::decreaseTotal();
}
I'm definitely a newbie so I appreciate any help you're able to give me.
I'm a bit confused about how objects should be passed around, if at all. It seems like if I was passing the objects as a whole, I'd be able to more easily manipulate their contents with methods like setName() or setPrice() without hitting the DB all the time.
I don't pass the objects around, I leave it on the server stored in a session, whether that session be a filesystem based session or in the database, I store it on the server and use the PHP garbage collection routine to handle how I (sometimes analyze and process and eventually ...) purge abandoned data. "Hitting" the filesystem or the database versus bandwidth consumption and security issues. Passing all that data is just more information that the end user has to wait for every round trip to the server. Also, if you have a lot of information, you may be pushing byte limitations in regards to GET/POST data as well as COOKIEs. Use your server, that's the reason it is called a server ;)
Also, just for the sake of argument, lets say I want to keep track of the number of items the user has as a separate field in the DB. When I remove an item this needs to decrease the number in the DB by one. How would this look? Like this?
Here, I would not even considering storing that number -- a synchronization mess if you ask me. If your cart class is setup correctly you can store the number of items in a variable or better yet, count [php.net] the item objects in your cart.