Twobears Posted January 5, 2013 Share Posted January 5, 2013 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'] ); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/272719-foreach-loop-duplicating-last-item/ Share on other sites More sharing options...
requinix Posted January 5, 2013 Share Posted January 5, 2013 There shouldn't be a reference at all. Remove the &. Are you sure $document_tvs doesn't contain the duplicate? Quote Link to comment https://forums.phpfreaks.com/topic/272719-foreach-loop-duplicating-last-item/#findComment-1403366 Share on other sites More sharing options...
Twobears Posted January 5, 2013 Author Share Posted January 5, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/272719-foreach-loop-duplicating-last-item/#findComment-1403369 Share on other sites More sharing options...
requinix Posted January 5, 2013 Share Posted January 5, 2013 The & will almost always cause problems because of references. Can you do me a favor and post the output of print_r($document_tvs); print_r($docTVArray); right after the foreach? Quote Link to comment https://forums.phpfreaks.com/topic/272719-foreach-loop-duplicating-last-item/#findComment-1403370 Share on other sites More sharing options...
Twobears Posted January 5, 2013 Author Share Posted January 5, 2013 (edited) 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 ) Edited January 5, 2013 by Twobears Quote Link to comment https://forums.phpfreaks.com/topic/272719-foreach-loop-duplicating-last-item/#findComment-1403377 Share on other sites More sharing options...
Twobears Posted January 5, 2013 Author Share Posted January 5, 2013 As the generated arrays appear to be correct, would I be right in thinking that the problem is generated in one of the other files code? ie;(shopping cart)? Quote Link to comment https://forums.phpfreaks.com/topic/272719-foreach-loop-duplicating-last-item/#findComment-1403379 Share on other sites More sharing options...
requinix Posted January 6, 2013 Share Posted January 6, 2013 Yep. Quote Link to comment https://forums.phpfreaks.com/topic/272719-foreach-loop-duplicating-last-item/#findComment-1403521 Share on other sites More sharing options...
Twobears Posted January 6, 2013 Author Share Posted January 6, 2013 Many thanks for your help with this. Quote Link to comment https://forums.phpfreaks.com/topic/272719-foreach-loop-duplicating-last-item/#findComment-1403537 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.