We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30876 ☆ A M B ☆
    • 39 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']
              );
            }
        }
     }
    ?>
    [ed. note: Twobears last edited this post 13 years, 8 months ago.]
      • 30876 ☆ A M B ☆
      • 39 Posts
      It appears, after trawling through the shopping cart code, that the duplication error is being made once the array is passed to the next file for display in the cart.