blmg2009 Posted August 15, 2015 Share Posted August 15, 2015 Hi guys; I have an array on orders ($orders) which I loop through using foreach to create a table of orders from customer each order has it's own checkbox in the table. I'm wanting to build a method to merge the selected orders length, width, height and weight using the checkbox for orders in the table which have been made by the same person. I need a way to select which order_id is going to contain / update the merged data from the other orders, then the excess order_id(s) will need to be removed from the main $orders array. Here's an example of the table and data: $orders array produces the table ◻ ID CUSTOMER PRODUCT LENGTH WIDTH HEIGHT WEIGHT ◻ 1 Joe Bloggs iPhone 10 5 5 230◻ 2 John Smith Macbook 20 20 10 800 ◻ 3 Joe Bloggs Mouse 5 5 5 100 ◻ 4 Terry Fry USB 2 2 2 50 ◻ 5 Ben Tanner Charger 10 10 10 400 i would need to select the two orders from Joe bloggs and merge them, I need a way to decide which order_id will contain the merged data for example the lower value order maybe, then it would need to remove them from the $orders array To get the following ◻ ID CUSTOMER PRODUCT LENGTH WIDTH HEIGHT WEIGHT ◻ 1 Joe Bloggs iPhone 15 10 10 330◻ 2 John Smith Macbook 20 20 10 800 ◻ 4 Terry Fry USB 2 2 2 50 ◻ 5 Ben Tanner Charger 10 10 10 400 I'm seeking advice on the best way to go about this, any help would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 15, 2015 Share Posted August 15, 2015 It is difficult to see why one might want to this, particularly as you have now lost the data that Joe Bloggs also ordered a mouse. Quote Link to comment Share on other sites More sharing options...
blmg2009 Posted August 15, 2015 Author Share Posted August 15, 2015 It is difficult to see why one might want to this, particularly as you have now lost the data that Joe Bloggs also ordered a mouse. This is because the code above is for a dispatch manager; So orders will often get merged together when packed, the data will still remain in the database but we only want a single record for each parcel shipped as orders will be sent in one parcel not multiple parcels. The orders & merged orders will then be uploaded to our courier site, if left un merged the pickers and packers will have extra shipping labels for the orders which they have merged into one box. Therefore this way we can solve this issue before uploading the data files to the courier's website. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 15, 2015 Share Posted August 15, 2015 OK, the weight would be the total but the length, width and height will not all be sum of the items. You would pack them end-to-end, side-by-side or stack them, so only one of the dimensions would be the sum. The others would be the maximum values. Quote Link to comment Share on other sites More sharing options...
blmg2009 Posted August 15, 2015 Author Share Posted August 15, 2015 I would be stacking them, I can see the example table doesn't show this as I quickly knocked this up to try to explain the intentions of the code. Currently I'm working on the best way to choose the 1 single order_id out of the selected orders, I'm thinking the lower value id. Then it'll do the dimensions calculations and then it will remove the other order_ids from the $orders array. This part is the part i'm struggling, I'm fairly new to programming so I want to use the best practices; I don't want to be creating junk code Quote Link to comment Share on other sites More sharing options...
Barand Posted August 15, 2015 Share Posted August 15, 2015 I'd do it when I extract the data from the database instead of reading the data into an array then trying to manipulate that. SELECT order_id , customer_name , product_name , MAX(length) as length , MAX(width) as width , SUM(height) as height , SUM(weight) as weight FROM whichever tables GROUP BY customer_name Job done Quote Link to comment Share on other sites More sharing options...
blmg2009 Posted August 15, 2015 Author Share Posted August 15, 2015 I'd do it when I extract the data from the database instead of reading the data into an array then trying to manipulate that. SELECT order_id , customer_name , product_name , MAX(length) as length , MAX(width) as width , SUM(height) as height , SUM(weight) as weight FROM whichever tables GROUP BY customer_name Job done Thank you for your reply I saw this method before on another system however, when you get two customers with the same name but are actually different people it will merge they orders together. I know the odds of that happening is rare but it has happened before to me and the system I was using at the time merged two different customers orders together as they shared the same name. Then I asked the admins to also match the post codes which worked well, however the orders also come from other marketplaces such as Amazon which format the customer names differently to my web store. Therefore it's helpful to manually check the orders you are merging. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 15, 2015 Share Posted August 15, 2015 Extract the customer_id as well as the names and group by the id Quote Link to comment Share on other sites More sharing options...
blmg2009 Posted August 15, 2015 Author Share Posted August 15, 2015 Extract the customer_id as well as the names and group by the id The web store is linked into Amazon and eBay so if a customer purchases on eBay and then on Amazon the customer data is imported via an api. So two different customer_id will be created for the orders. Therefore when it comes to dispatching the two orders from eBay and Amazon the user will want to merge the data into one record to be downloaded but the two orders will have different customer_id as there have come from different marketplaces so this can not be used. The huddles in this task will most likely drive me crazy Quote Link to comment Share on other sites More sharing options...
Barand Posted August 15, 2015 Share Posted August 15, 2015 Then use whatever you get that is unique to a customer (name/postcode ?). How were you planning to get round the problem with your array merge? Quote Link to comment Share on other sites More sharing options...
blmg2009 Posted August 17, 2015 Author Share Posted August 17, 2015 Then use whatever you get that is unique to a customer (name/postcode ?). How were you planning to get round the problem with your array merge? I'm using the following: $selected_order = min($merge_orders); // Fetch the min value order_id unset($merge_orders[$selected_order]); // Take it out of the array. $orders_list = explode(',', $this->request->post['order_id_list']); // Passed from the hidden field $removeKeys = $merge_orders; // Remove the merged order_id from the $orders_list so they do not show on the page refresh foreach ($removeKeys as $key) { unset($orders_list[$key]); echo $key; } echo print_r($orders_list); Quote Link to comment Share on other sites More sharing options...
blmg2009 Posted August 17, 2015 Author Share Posted August 17, 2015 How the second unset does not delete from the array in the foreach loop?? Quote Link to comment 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.