Jump to content

foreach loop duplicating last item


Twobears

Recommended Posts

The foreach loop in the following code block (starting at line: 18) duplicates the last item in the returned array.

I have tried unset to break the reference with the last element but that doesn't work.

Any help is greatly appreciated.

<?php
// [[orderDetailsInToSESSION]]

if ( empty($_GET['customer']) ) return "no customer account specified";
else {
   // sanitize and validate the input type
   $customer = $modx->db->escape($_GET['customer']);
   $orderId = $modx->db->escape($_GET['orderId']);
   // get the details from the orderId
   $db_query = $modx->db->select("productId, quantity", $modx->getFullTableName('customer_order_details'), "orderId='" . $orderId . "'");
   if ($modx->db->getRecordCount($db_query) > 0) {
       while ($row = $modx->db->getRow($db_query)) {
           // get the current prices and names of the products previously ordered
           $document_tvs = $modx->getTemplateVars(array("pagetitle", "price", "specialPrice"), "name", $row['productId']);

        // put the returned array into a simpler/easier to use array
              if (is_array($document_tvs)) {  
              foreach( $document_tvs as &$document_TV) {
               $docTVArray[$document_TV['name']] = $document_TV['value'];
              }
               unset($document_TV);
             }

         // put these details into the session
        $_SESSION['shoppingCartContents'][] = Array (
             'productId' => $row['productId'],
            'productTitle' => $docTVArray['pagetitle'],
             'price' => ( !empty( $docTVArray['specialPrice']) ) ?  $docTVArray['specialPrice'] :  $docTVArray['price'],
             'quantity' => $row['quantity']
         );
       }
   }
}
?>
Link to comment
https://forums.phpfreaks.com/topic/272719-foreach-loop-duplicating-last-item/
Share on other sites

Hi Requinix

Thank you for your reply.

I'm a bit of a novice with php. In my effort to solve this I read somewhere that the & was required.

I'll remove it and study it's correct usage some more.

The $document_tvs contains an array of the items in the order.

The last item in the order is definitely duplicated as it is always identical regardless whether there is only one item or many.

Here are the results as requested.

Although there is only one item in the array below, the item is duplicated in the shopping cart.

When there are multiple items in the order only the last item is duplicated.

 

Array
(
[0] => Array
(
[name] => price
[value] => 7.50
)
[1] => Array
(
[name] => specialPrice
[value] =>
)
[2] => Array
(
[name] => pagetitle
[value] => Caesar Dressing
)
)

Array
(
[price] => 7.50
[specialPrice] =>
[pagetitle] => Caesar Dressing
)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.