Monday, 27 August 2018

Accessing an array element with PHP

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)
  );


foreach ($history_result['RESULT'] as $history) {
    $fooArray[] = $history['orders_id']
}

0 comments:

Post a Comment