Jump to content

jolyon_russ

New Members
  • Posts

    8
  • Joined

  • Last visited

Profile Information

  • Gender
    Not Telling

jolyon_russ's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Thanks barand, from what I can gather the array_values function reorders the array to fill any gaps, wicked! You've all be a great help, thank again. Jolyon
  2. Cheers guys, I did try unset already, but it screwed with my indexing: $people = array([0] => 'Tom', [1] => 'Dick', [2] => 'Harriet', [3] => 'Brenda', [4] => 'Jo'); became... $people = array([0] => 'Tom', [1] => 'Dick', [3] => 'Brenda', [4] => 'Jo'); If I do use unset is there a way that I can then, re-index the array to fill the gap? I know that array_splice does this automatically unless specified. Thanks again for the help. Jolyon
  3. I should probably be posting this in the OOP section but my problem is only half OOP related. I have an array that contain objects, I want to be able to test against the id of that object and remove it from the array. In it's simplest for I want to go from this: $people = array('Tom', 'Dick', 'Harriet', 'Brenda', 'Jo'); ...to this... $people = array('Tom', 'Dick', 'Brenda', 'Jo'); The fact that the items in the array are objects should not matter right? Plus I've already written my loops and conditionals to check which item i need to remove. function removeItem ( $item ) { $len = count ( $_SESSION['basket_arr'] ) ; if ( $len == 1 ) { emptyBasket ( ) ; } else { for ( $i = 0 ; $i < $len ; $i++ ) { if ( $item->id == $_SESSION['basket_arr'][$i]->id ) { //$_SESSION['basket_arr'][$i] must go } } } } Thanks in advance for your help. Jolyon
  4. Hey emehrkay, Yeah, it's what I want but I need them to be unique objects. If the object already exists it add the value to the existing one. The problem I'm having is that it's adding the same object twice and more. The array I pasted in shows 3 objects their id's are "carrots", "peas" and "peas" when it should just be "carrots" and "peas". Obviously these are hypothetical items for sale on my website. Is there any reason why this line would actually add a new item to the array? $_SESSION['basket_arr'][$i]->number += $item->number ; Thanks in advance. Jolyon
  5. I thought I'd got it working, but after building it up to multiple pages passing items to the session array it seems to break. By break I mean it adds extra items to the array when it should be adding to the number. I can add as many of 1 item as I want, but when I add a different item more than once I get this: Array ( [0] => itemVO Object ( [id] => carrots [price] => 1.50 [number] => 10 ) [1] => itemVO Object ( [id] => peas [price] => 2.50 [number] => 2 ) [2] => itemVO Object ( [id] => peas [price] => 2.50 [number] => 1 ) ) I've attached a zip with all the files, if anyone has 5 mins to take a look at these I would really appreciate it. Cheers. Jolyon [attachment deleted by admin]
  6. It's working, thanks for the help, including the files before the session_start() worked and since then I've written a getBasketItem method which gets called from my checkout page. I may be back for more questions but in the mean time I'm cracking on with it. Cheers again. Jolyon
  7. Cheers Paul, you're right I was trying to pass in $itemVO, this error appeared while I was formatting the code for the post. Emehrkay, thanks for the include/require tip, I'll give that a try and report back, hopefully it should do the trick. I did have error reporting on, but it was throwing a weird include-ish error (can't remember it off the top of my head hence the rather lame explaination) but maybe your suggestion will sort this out. Thanks a lot both. Jolyon
  8. I'm a Flash developer by trade and use OOP on a daily basis so understand how things should work. I'm hoping my problem is just syntactical. Anyway, I'm building the simplest shopping cart, all it has to do is store the products temporarily, they will be passed out to Nochex's payment completion system, then dumped. So, I have my page with an add to cart button. It kicks off with creating basket_arr, then on _POST creating an instance of itemVO, which in tern is passed to an instance of basket <?php session_start ( ) ; if ( isset ( $_SESSION['basket_arr'] ) ) { print 'basket exists ' . $_SESSION['basket_arr'] ; } else { $_SESSION['basket_arr'] = array ( ) ; } if(isset($_POST['add'])) { $id = $_POST['id'] ; $price = $_POST['price'] ; $number = $_POST['number'] ; require ( 'vo/itemVO.php' ) ; $item = new itemVO ( ) ; $item -> id = $id ; $item -> price = $price ; $item -> number = $number ; require ( 'basket.php' ) ; $basket = new basket ( ) ; $basket -> addItem ( $item ) ; header("Location: " . $_SERVER['PHP_SELF']); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>Untitled Document</title> </head> <body> <form method="post"> <p>Carrots - £6.99 - <input name="id" type="hidden" value="carrots" /> <input name="price" type="hidden" value="1.50" /> <input name="number" type="text" value="1" size="3" maxlength="3" /> <input name="add" type="submit" class="box" id="carrots" value="Add To Basket"> </p> </form> </body> </html> I have my itemVO, simple enough: <?php class itemVO { var $id ; var $price ; var $number ; function UserVO() { } } ?> Now the area that I'm having my trouble with is my basket.php, the plan is to have my session hold an array of itemVO's. Obviously if I submit the same item more than once I need to check that it exists, if it does, add it to the existing one if not add the new itemVO to the array. <?php class basket { function addItem ( $item ) { $len = count ( $_SESSION['basket_arr'] ) ; if ( $len > 0 ) { for ( $i = 0 ; $i < $len ; $i++ ) { //loop through basket array if ( $_SESSION['basket_arr'][$i]->id == $item->id ) { //if the id of the session array is the same as the submitted item, add it echo ( 'the item trying to be added already exists' ) ; $_SESSION['basket_arr'][$i]->number += $item->number ; } else { echo ( 'there is no match add the new itemVO' ) ; array_merge ( $_SESSION['basket_arr'] , $itemVO ) ; } } } else { //basket_arr has no length so go ahead and add it $_SESSION['basket_arr'][0] = $item ; } } } ?> I hope this doesn't come across as another, "why doesn't this work" post, and more of a, "I know this should be working, any pointers as to why is might not be". I know that I could navigate to basket.php passing the itemVO as a query and add it that way, but think I'll probably just have the same problem. Ultimately I want to be able to add items from the product page without navigating away. Thanks for all your help in advance. Jolyon
×
×
  • 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.