Jump to content

persistent classes?


blueman378

Recommended Posts

hi guys im jsut starting oop and i have this basicl class,

 

class ShoppingCart
{

			var $items;

			function add_items($product_id, $qty) //ADD ITEMS TO THE CART

			{
							$this->items[$product_id] = $qty;
			}

			function update_items($product_id, $qty) // UPDATE QUANTITY OF ITEMS IN CART

			{
							if (array_key_exists($product_id, $this->items))
							{
											if ($this->items[$product_id] > $qty)
											{
															$this->items[$product_id] -= ($this->items[$product_id] - $qty);
											}
											if ($this->items[product_id] < $qty)
											{
															$this->items[$product_id] += abs($this->items[$product_id] - $qty);
											}
											if ($qty == 0)
											{
															unset($this->items[product_id]);
											}
							}
			}

			function remove_item($product_id) //REMOVE ITEMS FROM THE CART

			{
							if (array_key_exists($product_id, $this->items))
							{
											unset($this->items[$product_id]);
							}
			}

			function show_cart()
			{
							return $this->items;
			}
			function show_maincats()
			{
							echo "<Table width=\"50%\" cellpadding=\"10\" border=\"1\">
	<tr><td><strong>Category Name</strong></td><td><strong>Category Description</strong></td><td><strong>Subs</strong></td><tr>";
							$query = mysql_query("SELECT * FROM main_categories") or die(mysql_error());
							while ($mains = mysql_fetch_array($query))
							{
											echo "<tr><td>$mains[Name]</td><td>$mains[Description]</td><td>$mains[subs]</td></tr>";
							}
							echo "</table>";
							return sucess;
			}

}

 

which i can use it fine however how would i make this class persist over multiple pages?

Link to comment
https://forums.phpfreaks.com/topic/105376-persistent-classes/
Share on other sites

In simple terms:

 

$cart = new ShoppingCart();
$_SESSION['cart'] = $cart;

 

et voila, you have now put the shopping cart into the session. I'm guessing however that you are using php4 because you appear not to be using public/protected/private identifiers. Change to 5 at the first opportunity because php4 is no coming to it's end of life.

Link to comment
https://forums.phpfreaks.com/topic/105376-persistent-classes/#findComment-539726
Share on other sites

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.