Jump to content

OOP newb. need help creating pseudo shopping cart.


boo_lolly

Recommended Posts

as the title illustrates... i've only recently adopted OOP. i have become a huge proponent of OOP after reading many articles and tutorials. i see the usability and the importance of OOP, especially when working on large-scale software. i am eager to attempt my first application in OOP. but i really don't know where to start. i can do this simply and easily using 'procedural' code... but i don't want to use that. i want to  write this application completely OOP style. since this is a relatively small application, i figured this would be a good place to start =). first, let me describe the scope of the project:

 

it is basically a pseudo shopping cart. there will be no CMS or backend affilliated with this shopping cart. there will be no database to store data. the data will be stored via $_SESSIONs. hence the name 'pseudo'. basically, my client has a site where they'd like to adopt online ordering. they have a massive piece of proprietary software that can be integrated with 3rd party software (which would be this small pseudo shopping cart), which is why i don't need a database, and i don't need a CMS. this pseudo shopping cart is designed specifically to store customer orders in sessions, and send this data through the internet to be recieved by this massive piece of proprietary software... i can handle everything after this OOP pseudo shopping cart... i just have no clue where to start, but i believe this will be a fairly simple application... especially if done in OOP =).

 

can i get any help or advice? i haven't even begun coding... maybe some tutorials (other than the ones that are seen in my signature, of course) would be in order? examples? i guess my main confusion revolves around the ideology, then implimenting the ideology. the tutorials i've found are quite basic and don't truly depict how robust OOP really is. and when the tutorial gets complex... it immediately goes right over my head, since i have a loose understanding of OOP to begin with.

 

so could i get a little help with this? i'd really appreciate it.

 

thanks

-boo

Link to comment
Share on other sites

Sounds like a fun project, if you have aim or msn hit me up later and we can talk about it some more but here is a start. Also what version of PHP are you running 4 or 5? It makes a huge difference.

 

<?php
// This is for php 4 
class cartItem {
         var $itemName;
         var $itemCost;
         var $itemDesc;

         function cartItem($itemName, $itemCost, $itemDesc) {
                  //put data in the class
                  $this->itemName = $itemName;
                  $this->itemCost = $itemCost;
                  $this->itemDesc = $itemDesc;
          }

}

class shoppingCart {
           var $items;

           function shoppingCart() {

                   // constructor
            }

            function addItem($itemName, $itemCost, $itemDesc) {
                    $this->items[$itemName] = new cartItem($itemName, $itemCost, $itemDesc);
            }

            function deleteItem($itemName) {
                    unset($this->items[$itemName]);
            }

            //.... etc
}
?>

Link to comment
Share on other sites

thanks frost! i'm sending you a pm with my aim and msn. i won't be on until later in the evenings, but i look forward to getting a better grasp of this OOP stuff. thanks for the sample code by the way, i'm gonna play around with that.

 

once i get some code together myself, i'm going to post it here in this thread and we'll go from there.

 

thanks again frost! btw i'm running PHP 4.3.2

Link to comment
Share on other sites

frost, i was looking at the code sample you so generously drew up for me, and i've got a question a couple of questions about something...

 

<?php

        class cartItem
        {
                var $itemName;
                var $itemCost;
                var $itemInfo;

                /*constructor*/
                function cartItem($itemName, $itemCost, $itemInfo){
                        $this->itemName = $itemName;
                        $this->itemCost = $itemCost;
                        $this->itemInfo = $itemInfo;
                }
        }//end class cartItem

        class shoppingCart
        {
                var $items = array();

                function shoppingCart(){
                        /*constructor*/
                }

                function addItem($itemName, $itemCost, $itemInfo){
                        $this->items[$itemName] = new cartItem($itemName, $itemCost, $itemInfo);
                }

                function deleteItem($itemName){
                        unset($this->items[$itemName]);
                }
        }//end class shoppingCart

?>

 

can you explain to me how this functions? more specifically, in the addItem function found in the shoppingCart class, we are creating an object of cartItem where we are passing variables to the constructor, correct? obviously you can't pass arguments to the class itself, so that's what the constructor is for, correct?

 

second, (still regarding the addItem function) when we use the function is it going to append a string to a new index in the array? will it nest a new array into a new index in the $items array? because if that is the case, wouldn't the user only be able to add one of each item into the array? i'm confused to say the least =\ haha.

 

i think this:

$this->items[$itemName] = new cartItem($itemName, $itemCost, $itemInfo);

 

should be this:

$this->items[] = new cartItem($itemName, $itemCost, $itemInfo);

 

and i think this:

                /*constructor*/
                function cartItem($itemName, $itemCost, $itemInfo){
                        $this->itemName = $itemName;
                        $this->itemCost = $itemCost;
                        $this->itemInfo = $itemInfo;
                }

 

should be this:

                /*constructor*/
                function cartItem($itemName, $itemCost, $itemInfo){
                        $this->itemName = $itemName;
                        $this->itemCost = $itemCost;
                        $this->itemInfo = $itemInfo;
                        return;
                }

 

but i could be way off... can you help me understand this on a better level?

Link to comment
Share on other sites

Sure thing

 

can you explain to me how this functions? more specifically, in the addItem function found in the shoppingCart class, we are creating an object of cartItem where we are passing variables to the constructor, correct? obviously you can't pass arguments to the class itself, so that's what the constructor is for, correct?

 

Yes the constructor is for taking in arguments etc and initiating the class. When you call a class the constructor is the first thing ran, always. It "constructs" the class.

 

second, (still regarding the addItem function) when we use the function is it going to append a string to a new index in the array? will it nest a new array into a new index in the $items array? because if that is the case, wouldn't the user only be able to add one of each item into the array? i'm confused to say the least =\ haha.

 

It is not appending a string, it is using the $itemName as the index. This could be ItemID instead, if we are using ID's. All it provides is that in case we want to delete an item from the cart, we just have to unset the array at the index, in this case at the name, such as in the deleteItem function.

 

On that note I thought of a different way to do it, I am unsure if my logic is correct, I will test it tonight but yea, here is another way (maybe) it could be done.

 

<?php

        class cartItem
        {
                var $itemName;
                var $itemCost;
                var $itemInfo;


                /*constructor*/
                function cartItem(){
                    // nothing here
                }

			function createItem($itemName, $itemCost, $itemInfo){
                        $this->itemName = $itemName;
                        $this->itemCost = $itemCost;
                        $this->itemInfo = $itemInfo;

					return $this; // return this class instance
                }
        }//end class cartItem

        class shoppingCart
        {
                var $items = array();

                function shoppingCart(){
                     /*constructor*/
				$this->cartItem = new cartItem();
                }

                function addItem($itemName, $itemCost, $itemInfo){
					// I am not sure if this will work. Will test later tonight
                        $this->items[$itemName] = $this->cartItem->createItem($itemName, $itemCost, $itemInfo);
                }

                function deleteItem($itemName){
                        unset($this->items[$itemName]);
                }
        }//end class shoppingCart

?>

 

EDIT:::

$this->items[$itemName] = new cartItem($itemName, $itemCost, $itemInfo);

 

should be this:

 

$this->items[] = new cartItem($itemName, $itemCost, $itemInfo);

For the above I am just making it easier to reference the item in the array by assigning it an associative index.

 

and i think this:

Code:

 

                /*constructor*/

                function cartItem($itemName, $itemCost, $itemInfo){

                        $this->itemName = $itemName;

                        $this->itemCost = $itemCost;

                        $this->itemInfo = $itemInfo;

                }

 

 

should be this:

Code:

 

                /*constructor*/

                function cartItem($itemName, $itemCost, $itemInfo){

                        $this->itemName = $itemName;

                        $this->itemCost = $itemCost;

                        $this->itemInfo = $itemInfo;

                        return;

                }

 

 

You do not HAVE to return functions, it is optional to do that, yes it can be done but not necessary.

 

Link to comment
Share on other sites

Yet another version:

 

<?php

        class cartItem {
                var $itemName;
                var $itemCost;
                var $itemInfo;
			var $itemQuantity;


                /*constructor*/
                function cartItem(){
                    // nothing here
                }

			function createItem($itemName, $itemCost, $itemInfo, $itemQuantity){
                        $this->itemName = $itemName;
                        $this->itemCost = $itemCost;
                        $this->itemInfo = $itemInfo;
					$this->itemQuantity = $itemQuantity;

					return $this; // return this class instance
                }
        }//end class cartItem

        class shoppingCart {
                var $items; // in php 4 the declaration does not matter here =)

                function shoppingCart(){
                     /*constructor*/
				$this->cartItem = new cartItem();
                }

                function addItem($itemName, $itemCost, $itemInfo, $itemQuantity){
					// I am not sure if this will work. Will test later tonight
                        $this->items[$itemName] = $this->cartItem->createItem($itemName, $itemCost, $itemInfo, $itemQuantity);
                }

                function deleteItem($itemName){
                        unset($this->items[$itemName]);
                }
        }//end class shoppingCart

?>

 

I added a quantity field this time. That should take care of being able to have multiple items of the same item in the cart =)

Link to comment
Share on other sites

that's awesome frost! i really appreciate your effort!

 

let me elaborate a little on the project and put this OOP stuff to the test =). the item quantity field is perfect, and it will serve the cart's functionality well. however, i'd like to add a feature (i believe this will be done inside the addItem() function) that will check if there is more than one of the same item in the shoppingcart. if so, compare their itemInfo arguments...

 

here's why... the $itemInfo variable will differ from item to item. this is where i will store 'specific' data about that specific item in the shopping cart. for instance, let's say we have an item 'sandwitch'. this sandwitch comes with lettuce, tomatoes, and cheese. this will be stored in the $itemInfo array for the sandwitch. however, if the user wants to remove lettuce and add mayo, there will be a checkbox form for this feature (which will 'pre-check' the things that already come on the sandwitch, and list other checkboxes with things like mayo and bacon and stuff. upon form submission, the $itemInfo for that particular item in the shopping cart will be changed to reflect the sandwiches new contents.

 

each item will have their own pre-defined $itemInfo array affilliation. so, one of my questions is how would i go about defining the $itemInfo for each item on the site using OOP? i'm sure there are many ways, but understanding where i'm going with this project, is there a specific way this feature could best be implimented?

 

does that make sense?

Link to comment
Share on other sites

Alright, sounds like a good scenario. Let's add ItemID to the list. How we will do it is keep the ItemID an increment value, that is housed at the class level. Here is an example:

 

<?php

        class cartItem {
			var $itemID;
                var $itemName;
                var $itemCost;
                var $itemInfo;
			var $itemQuantity;


                /*constructor*/
                function cartItem(){
                    // nothing here
                }

			function createItem($itemID, $itemName, $itemCost, $itemInfo, $itemQuantity){
                        $this->itemName = $itemName;
                        $this->itemCost = $itemCost;
                        $this->itemInfo = $itemInfo;
					$this->itemQuantity = $itemQuantity;

					return $this; // return this class instance
                }
        }//end class cartItem

        class shoppingCart {
                var $items; // in php 4 the declaration does not matter here =)
			var $itemID; // increment value

                function shoppingCart(){
                     /*constructor*/
				$this->cartItem = new cartItem();
				$this->itemID = 0;
                }

                function addItem($itemName, $itemCost, $itemInfo, $itemQuantity){
					// I am not sure if this will work. Will test later tonight
					$newItem = $this->cartItem->createItem($itemID, $itemName, $itemCost, $itemInfo, $itemQuantity);
					$index = $this->compareItems($newItem)
					if ($index == 0) {
						$this->items[$this->itemID++] = $this->cartItem->createItem($itemID, $itemName, $itemCost, $itemInfo, $itemQuantity);
					}else {
						$this->items[$index] = $newItem; // update the old item??
					}
                }

			// compares $item to the current $item array
			function compareItems($item) {
				if (is_array($this->items)) {
					foreach ($this->items as $key => $compItem) {
						if ($compItem->itemInfo == $item->itemInfo) {
							// they are similiar do something.
							return $key; // the same
						}
					}
				}

				return 0; // not the same
			}

                function deleteItem($itemID){
                        unset($this->items[$itemID]);
                }
        }//end class shoppingCart

?>

 

Note what this will do is simply checks the iteminfo to see if that is the same, if it is than it just updates the array and replace if it is not it adds the new item to the list.

 

There is many different solutions, but is probably better discussed than assumed =) I will wait till later tonight to elaborate on what can be done.

Link to comment
Share on other sites

perfect man. thanks a lot for all your help so far! really, my only concern is how to understand the ideology of this OOP project. i think this project is a good opportunity to learn the benefits of OOP. needless to say i'm very excited about shifting from procedural programming to OOP. thanks again frost, you da man, and we'll chat later about this.

 

peace

Link to comment
Share on other sites

Just an update for everyone else from last nights discussion. Here is the new code:

 

<?php
    class cartItem {
var $itemID;
        var $itemName;
        var $itemCost;
        var $itemInfo;
	var $itemQuantity;


        /*constructor*/
        function cartItem(){
	// nothing here
        }

	function createItem($itemID, $itemName, $itemCost, $itemInfo, $itemQuantity){
		$this->itemID = $itemID;
		$this->itemName = $itemName;
                $this->itemCost = $itemCost;
                        $this->itemInfo = $itemInfo;
		$this->itemQuantity = $itemQuantity;

		return $this; // return this class instance
        }
    }//end class cartItem

    class shoppingCart {
	var $items; // in php 4 the declaration does not matter here =)
	var $itemID; // increment value

        function shoppingCart(){
		/*constructor*/
		$this->cartItem = new cartItem();
		$this->itemID = 0;
        }

        function addItem($itemName, $itemCost, $itemInfo, $itemQuantity){
		$newItem = $this->cartItem->createItem($this->itemID, $itemName, $itemCost, $itemInfo, $itemQuantity);
		$index = $this->compareItems($newItem);
		if ($index == -1) {
			$this->items[$this->itemID++] = $newItem;
		}else {
			$this->items[$index]->itemQuantity++; // update quantity
		}			
        }

	// compares $item to the current $item array
	function compareItems($item) {
		if (is_array($this->items)) {
			foreach ($this->items as $key => $compItem) {
				if ($compItem->itemName == $item->itemName) {
					// first check the count
					if (count($item->itemInfo) == count($compItem->itemInfo)) {
						$i=0;
						foreach ($item->itemInfo as $checkItem) {
							if (in_array($checkItem, $compItem->itemInfo)) {
								$i++;
							}
						}

						if ($i == count($item->itemInfo)) 
							return $key; 
					}
				}
			}
		}

		return -1; // not the same
	}

        function deleteItem($itemID){
		unset($this->items[$itemID]);
        }
}//end class shoppingCart

?>

Link to comment
Share on other sites

Adding a function for verifyItem, given that we have a list of all itemInfo values to make sure that we do not just get bogus information. The list will probably be static in a config file somewhere.

 

The following is strictly the new cartItem class.

 

<?php
    class cartItem {
	var $itemID;
        var $itemName;
        var $itemCost;
        var $itemInfo;
	var $itemQuantity;


        /*constructor*/
        function cartItem($itemList){
		// nothing here
		$this->itemList = $itemList;
        }

	function createItem($itemID, $itemName, $itemCost, $itemInfo, $itemQuantity){
		$this->itemID = $itemID;
		$this->itemName = $itemName;
        $this->itemCost = $itemCost;

		if (is_array($itemInfo)) {
			foreach ($itemInfo as $item) {
				$index = $this->verifyItem($item);

				// We need to make sure that our items are valid items.
				if ($index)
					$newArr[$this->itemList[$index]['infoid']] = $item;
			}	
		}

		$this->itemInfo = $newArr;
		$this->itemQuantity = $itemQuantity;

		return $this; // return this class instance
        }

	function verifyItem($item) {
		return array_search(strtolower($item), $this->itemList);
	}
    }//end class cartItem
?>

 

Using this we can check the iteminfo vs the iteminfoid incase somehow the case is changed etc.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.