1

To avoid multiple mysql SELECT queries I try to take the SELECT out of the foreach-loop.

The query is

 SELECT    o.orders_id,
          o.date_purchased,
          o.orders_status,
          ot.text AS order_total,
          os.orders_status_name
FROM      " . TABLE_ORDERS . " o
JOIN      " . TABLE_ORDERS_TOTAL . " ot ON (o.orders_id = ot.orders_id AND ot.class = 'ot_total')
JOIN      " . TABLE_ORDERS_STATUS . " os ON (o.orders_status = os.orders_status_id AND os.language_id = " . (int)$_SESSION['languages_id'] . ")
WHERE     o.customers_id = " . (int)$_SESSION['customer_id'] . "
ORDER BY  orders_id DESC

As the result I get the array called $history_result['RESULT'] and it looks like

Array
(
  [0] => Array
  (
    [orders_id] => 309
    [date_purchased] => 2013-10-02 15:49:54
    [orders_status] => 1
    [order_total] =>  9,00 €
    [orders_status_name] => Offen
  )
  [1] => Array
  (
    [orders_id] => 308
    [date_purchased] => 2013-10-02 15:39:54
    [orders_status] => 1
    [order_total] =>  9,00 €
    [orders_status_name] => Offen
  )
  [2] => Array
  (
    [orders_id] => 307
    [date_purchased] => 2013-10-02 15:33:48
    [orders_status] => 1
    [order_total] =>  9,00 €
    [orders_status_name] => Offen
  )
)

In the following foreach-loop is the next MySql-Query

foreach ($history_result['RESULT'] as $history) {
  $trackings   = $db->result("-- 03 account_history_info.php
    SELECT o.ortra_parcel_id,
           c.carrier_tracking_link,
           c.carrier_name
    FROM   " . TABLE_ORDERS_TRACKING . " o
    JOIN   " . TABLE_CARRIERS . " c ON o.ortra_id = c.carrier_id
    WHERE  ortra_order_id = " . $history['orders_id']
  );
}

How can I access directly to the array element 'orders_id' so that I could create an SQL statement like

$trackings   = $db->result("-- 03 account_history_info.php
    SELECT o.ortra_parcel_id,
           c.carrier_tracking_link,
           c.carrier_name
    FROM   " . TABLE_ORDERS_TRACKING . " o
    JOIN   " . TABLE_CARRIERS . " c ON o.ortra_id = c.carrier_id
    WHERE  ortra_order_id IN = " . implode(',', $fooArray)
  );
1
  • This function looks awesome and does it excactly what I'm looking for but it only works with PHP >= 5.5 Commented Oct 2, 2013 at 21:06

3 Answers 3

1
foreach ($history_result['RESULT'] as $history) {
    $fooArray[] = $history['orders_id']
}
Sign up to request clarification or add additional context in comments.

Comments

0

Can you just used a nested select to combine the queries you have to perform, into one query, like:

SELECT FROM (SELECT FROM ... )

Or am I perhaps missing some subtlety?

Comments

0
$fooArray = array_column($history_result['RESULT'], 'orders_id');

NB: PHP 5 >= 5.5.0

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.