Jump to content

Recommended Posts

Not sure, I just tested it using the above code and it worked just fine.

 

The only thing I can think of is meallist is an array...

 

do me a favor and run this:

 

<?php
session_start();
include('order.php');
// include the order class.

if (isset($_SESSION['order'])) {
      $order = unserialize($_SESSION['order']);
}else {
   $order = new Order();
}

// incase we want to reset our test data.
if ($_REQUEST['reset'] == "ok") {
   $order = new Order();
}

$meallist = array();
foreach ($_REQUEST as $k=>$v) { if ($k=="meal") { $meallist[] = $v; } }

echo "<pre>";
print_r($meallist);

$order->addMultipleItems($meallist);
$items = $order->getItems();

foreach ($items as $item) {
   echo $item->getID() . "<br />";
}

$_SESSION['order'] = serialize($order);


// now use $order as you wish
$order->addItem(5);

$item = $order->getItemByIndex(0);
echo  "Item ID of " . $item->getID() . " is at index of 0<br />";

echo "We currently have " . $order->getItemCount() . " items in your order.<br />";
?>

 

Post the results that come from that.

It returns this:

 

Array

(

    [0] => Array

        (

            [0] => 28

            [1] => 27

            [2] => 26

            [3] => 25

            [4] => 24

            [5] => 23

            [6] => 22

        )

 

)

Array

Item ID of Array is at index of 0

We currently have 2 items in your order.

 

 

So meallist is a multi dimensional array.

 

try this.

 

<?php
session_start();
include('order.php');
// include the order class.

if (isset($_SESSION['order'])) {
      $order = unserialize($_SESSION['order']);
}else {
   $order = new Order();
}

// incase we want to reset our test data.
if ($_REQUEST['reset'] == "ok") {
   $order = new Order();
}

$meallist = array();
foreach ($_REQUEST as $k=>$v) { if ($k=="meal") { $meallist[] = $v; } }

$order->addMultipleItems($meallist[0]); //changed this to reference index of 0.
$items = $order->getItems();

foreach ($items as $item) {
   echo $item->getID() . "<br />";
}

$_SESSION['order'] = serialize($order);


// now use $order as you wish
$order->addItem(5);

$item = $order->getItemByIndex(0);
echo  "Item ID of " . $item->getID() . " is at index of 0<br />";

echo "We currently have " . $order->getItemCount() . " items in your order.<br />";
?>

It shouldnt increment the id's unless you tell it to

 

Given your output from above:

 

Array

(

 

 

    * => Array

 

        (

         

 

    * => 28

      [1] => 27

                  [2] => 26

                  [3] => 25

                  [4] => 24

                  [5] => 23

                  [6] => 22

              )

 

      )

 

Those are the values you are passing to the array...so this is working like expected.

Right thats how it should be...right?

 

I do not see how one item can have 5 different ids....?

 

This way you have a collection of items, this should replace the need to have multiple ids for an item, and just have one id.

 

If that is not what is suppose to happen, than I am afraid I may have wasted alot of your time.

No, that is not alot of changes. You have the right idea. Instead of having the class hold 5 different ids, it now can hold 5 different objects (which can be as many objects as you want) and you can just iterate (move) through the items or print them out like above instead of having to call 6 different function like before.

 

I hope that makes sense.

Oh sorry, my mind isnt paying attention today.

 

With the new code added you should be able to increment each ID by one by doing the following:

 

$order->incrementIDsByVal(1);

 

Should increment all the IDs by 1.

 

Change 1 to be whatever value you want and it will increment each item by that.

But if I do that it increments the id that is at index of 0 not the actual number of id's that exist.

 

What I want this entire program to do is the following: Have a meals class now called items class and allow this to take an array containing 5 ids each of which are meals.

 

I then want to have an Item class that once a new array of 5 meals is added 1 is added to the total number of items.

 

I have spent the day trying to understand your excelent piece of code but due to my lack of oop knowledge have struggled and instead tryed to revert back to the basics of understanding this.

 

However I still need to get a solution to the above problem if you could help me that would be great.

 

If you have time some explaination of the above code would also be great but I understand if you dont have time for any of this as you have helped a lot allready. Thanks

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.