0

I have a MySQL query:

SELECT id, order_no, reference, description, total_cost FROM bookorders

where the results within the while loop are building an array:

if (!array_key_exists($order_no, $orders)) {
    $orders[$order_no] = array(array(
        'reference' => $reference,
        'description' => $description, 
        'total_cost' => $total_cost,
        'discount' => 0,
        'total_cost_withdiscount' => $total_cost
    ));
} else {
    $orders[$order_no][] = array(
        'reference' => $reference,
        'description' => $description, 
        'total_cost' => $total_cost,
        'discount' => 0,
        'total_cost_withdiscount' => $total_cost
    );
}

and this is what it looks like:

Array
(
    [12345] => Array
        (
            [0] => Array
                (
                    [reference] => abc
                    [description] => Book Product 1
                    [total_cost] => 8.99
                    [discount] => 0
                    [total_cost_withdiscount] => 8.99
                )   
        )  
    [67890] => Array
        (
            [0] => Array
                (
                    [reference] => abc
                    [description] => Book Product 1
                    [total_cost] => 8.99
                    [discount] => 0
                    [total_cost_withdiscount] => 8.99
                )
            [1] => Array
                (
                    [reference] => def
                    [description] => Book Product 2
                    [total_cost] => 24.99
                    [discount] => 0
                    [total_cost_withdiscount] => 24.99
                )
        )
)

In the same table there's also discount lines so for order 67890, there was a discount of 8 for reference def.

Whilst running through the results from the query (which will bring out the discounts as a separate line), how can I find out if that reference already exists and update the 'discount' and 'total_cost_withdiscount' values?

So for the example above, I'd like to change this:

[67890] => Array
        (
            [0] => Array
                (
                    [reference] => abc
                    [description] => Book Product 1
                    [total_cost] => 8.99
                    [discount] => 0
                    [total_cost_withdiscount] => 8.99
                )
            [1] => Array
                (
                    [reference] => def
                    [description] => Book Product 2
                    [total_cost] => 24.99
                    [discount] => 0
                    [total_cost_withdiscount] => 24.99
                )
        )

To this:

[67890] => Array
        (
            [0] => Array
                (
                    [reference] => abc
                    [description] => Book Product 1
                    [total_cost] => 8.99
                    [discount] => 0
                    [total_cost_withdiscount] => 8.99
                )
            [1] => Array
                (
                    [reference] => def
                    [description] => Book Product 2
                    [total_cost] => 24.99
                    [discount] => 8.00
                    [total_cost_withdiscount] => 16.99
                )
        )

I need to update the values whilst I'm building the overall array within the while loop from the mysql query. I've tried this but it didn't work:

if ($orders[$order_no]["reference"] == $reference)

Edit

The discount is another line in the same table so the discount amount will come out as $total_cost.

So for above, it would be:

$id, $order_no, $reference, $description, $total_cost
1, 12345, abc, Book Product 1, 8.99
2, 67890, abc, Book Product 1, 8.99
3, 67890, def, Book Product 2, 24.99
4, 67890, def, discount, 8.00
2
  • I'm not sure how you are getting the value for discount. If from another table then why not include this information within the original query.
    – user2417483
    Jan 10, 2014 at 11:12
  • i've edited the question above to explain how the discount lines come out of the database
    – Jo H
    Jan 10, 2014 at 11:20

1 Answer 1

1
$reference_to_find = 'def';
$order_no_to_find = 12345;
foreach ($orders as $order_no => $infos)
{
    if ($order_no  == $order_no_to_find)
    {
        foreach ($infos as $index => $item)
        {
            if ($item['reference'] == $reference_to_find)
            {
                // change
                // $item['discount'] = ....

                // set
                $orders[$order_no][$index] = $item;
            }
        }
    }
}
5
  • Can I do this in my while loop while I'm building the array?
    – Jo H
    Jan 10, 2014 at 11:24
  • sure :). Try it, any problem, comment here.
    – srain
    Jan 10, 2014 at 11:38
  • it's finding the reference and updating the discounts with that result. So, in my example above, if order_no 12345 had a discount on reference abc of £2, it would edit the discount and total_cost_withdiscount values fine but then would also update order_no 67890 with the same discount for the same reference...
    – Jo H
    Jan 10, 2014 at 11:55
  • this is what I've used (sorry for the formatting, can't make it look like the coding above!): foreach ($orders as $order_no => $infos) { foreach ($infos as $index => $item) { if ($item['reference'] == $reference && $item['order_no'] == $order_no) { $total_cost_withdiscount = $item['total_cost'] + $total_cost; $item['discount'] = $total_cost; $item['total_cost_withdiscount'] = $total_cost_withdiscount; $orders[$order_no][$index] = $item; } $total_cost_withdiscount = 0; } }
    – Jo H
    Jan 10, 2014 at 11:56
  • You need a order_no to make the info unique. Look at the updated answer.
    – srain
    Jan 10, 2014 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.