jarvis Posted December 8, 2016 Share Posted December 8, 2016 Hi, I really hope someone can help! I have the following code: if ( ($advert_category == $page_id) && $advert_page == ''): echo 'cat ad'; $active_banner[]=$order->id; elseif ( $advert_category == '' && ($advert_page == $page_id) ): echo 'page ad'; $active_banner[]=$order->id; else: echo 'no matches - random'; $active_banner[]=$order->id; endif; Basically, depending on the criteria, it should add an order ID to an array. If I output the info, I know that that ID's should go into an array as I can see: order ID: 21495 page ad order ID: 1652 no matches - random However, when I dump out the main array, I only see one ID: Array ( [0] => 1652 ) Can someone explain why this would be? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/302704-array-missing-some-values/ Share on other sites More sharing options...
kicken Posted December 8, 2016 Share Posted December 8, 2016 I think you need to provide more code. It's not clear from the code you posted what is supposed to be happening or why you may be having issues. All three of your conditional branches add the order id to your array, so why have the conditions at all? Quote Link to comment https://forums.phpfreaks.com/topic/302704-array-missing-some-values/#findComment-1540139 Share on other sites More sharing options...
jarvis Posted December 8, 2016 Author Share Posted December 8, 2016 Hi @Kicken, The conditionals are used to meet other criteria, so depending which page or category of the site you're on, Basically, the order contains a banner ad that has been assigned to a page or a category or may not have been assigned at all The code detects what page or category of the site you're on. The conditional then checks all orders with that page ID against the page you're on. If it matches, it passes all order IDs to an array. It does the same for categories. If none match then it retrieves all orders where a page or category hasn't been assigned. Once I have the order IDs, I then randomise them and display the banner ad from the order ID I genuinely don't think any other code would help. However, here it is: $top_adverts = WC_Subscriptions_Manager::get_all_users_subscriptions(); ?><pre><?php #print_r($top_adverts); ?></pre><?php foreach ($top_adverts as $key => $value) { if ( $top_adverts[$key]["status"]=="active" && ($top_adverts[$key]["product_id"]=="21" || $top_adverts[$key]["product_id"]=="1285" || $top_adverts[$key]["product_id"]=="1639" || $top_adverts[$key]["product_id"]=="1642" )) { #get the order number $order_id = $top_adverts[$key]["order_id"]; #echo 'Order ID: '.$order_id.'<br/>'; $top_ad_args = array( 'post_type' => 'shop_order', 'post_status' => 'publish', 'posts_per_page' => '1', 'p'=>$order_id, 'tax_query' => array( array( 'taxonomy' => 'shop_order_status', 'field' => 'slug', 'terms' => array('completed') #completed ) ) ); $top_orders=get_posts($top_ad_args); #print_r($top_orders); foreach($top_orders as $order): $order_id = $order->ID; $order = new WC_Order($order_id); ?><pre><?php #print_r($order); ?></pre><?php $active_banner = array(); foreach( $order->get_items() as $item ): $advert_url = $item['Platinum Advert URL']; #echo 'Ad URL: '.$advert_url.'<br/>'; $advert = $item['Platinum Advert']; #echo 'Ad: '.$advert.'<br/>'; $advert_category = $item['Advert Category']; #echo 'Advert Category: '.$advert_category.'<br/>'; $advert_page = $item['Advert Page']; echo 'Advert Page: '.$advert_page.'<br/>'; echo 'ID: '.$order->id.'<br/>'; #if the below matches the ID passed via ajax, add the order ID to an array if ( ($advert_category == $page_id) && $advert_page == ''): echo 'cat ad'; $active_banner[]=$order->id; elseif ( $advert_category == '' && ($advert_page == $page_id) ): echo 'page ad'; $active_banner[]=$order->id; else: echo 'no matches - random'; $active_banner[]=$order->id; endif; echo '<hr/>'; endforeach; endforeach; } } ?> <hr /> <pre><?php print_r($active_banner); #debug ?></pre> I just can't see why if the code displays the IDs, it's not showing them in the array? Does that help? Quote Link to comment https://forums.phpfreaks.com/topic/302704-array-missing-some-values/#findComment-1540146 Share on other sites More sharing options...
Jacques1 Posted December 8, 2016 Share Posted December 8, 2016 You realize that you overwrite $active_banner with an empty array within your loop, right? Are you sure this is what you want? Quote Link to comment https://forums.phpfreaks.com/topic/302704-array-missing-some-values/#findComment-1540147 Share on other sites More sharing options...
jarvis Posted December 8, 2016 Author Share Posted December 8, 2016 (edited) Thanks @Jacques1 But I tried $cat_banner = array(); $page_banner = array(); $random_banner = array(); foreach( $order->get_items() as $item ): $advert_url = $item['Platinum Advert URL']; #echo 'Ad URL: '.$advert_url.'<br/>'; $advert = $item['Platinum Advert']; #echo 'Ad: '.$advert.'<br/>'; $advert_category = $item['Advert Category']; #echo 'Advert Category: '.$advert_category.'<br/>'; $advert_page = $item['Advert Page']; echo 'Advert Page: '.$advert_page.'<br/>'; echo 'ID: '.$order->id.'<br/>'; #if the below matches the ID passed via ajax, add the order ID to an array if ( ($advert_category == $page_id) && $advert_page == ''): echo 'cat ad'; $cat_banner[]=$order->id; elseif ( $advert_category == '' && ($advert_page == $page_id) ): echo 'page ad'; $page_banner[]=$order->id; else: echo 'no matches - random'; $random_banner[]=$order->id; endif; echo '<hr/>'; endforeach; endforeach; } } ?> <hr /> <pre><?php print_r($cat_banner); #debug ?></pre> <pre><?php print_r($page_banner); #debug ?></pre> <pre><?php print_r($random_banner); #debug ?></pre> And that still only shows 1 ID I thought I could go this route, then merge the arrays but still only got the 1 result: Array ( ) Array ( ) Array ( [0] => 1652 ) Edited December 8, 2016 by jarvis Quote Link to comment https://forums.phpfreaks.com/topic/302704-array-missing-some-values/#findComment-1540148 Share on other sites More sharing options...
Jacques1 Posted December 8, 2016 Share Posted December 8, 2016 You still keep overwriting your arrays, except that you now overwrite three arrays instead of one. This doesn't help. You need to actually understand the code and put the initialization where it belongs (probably before all the loops). By the way, your PHP syntax is very strange. This endif stuff is only meant for templates, not code. PHP code looks like this: if (...) { ... } else { ... } You know, like in C or Java. Quote Link to comment https://forums.phpfreaks.com/topic/302704-array-missing-some-values/#findComment-1540149 Share on other sites More sharing options...
jarvis Posted December 8, 2016 Author Share Posted December 8, 2016 Thanks Jacques1 Ok, for simplistic terms, if I did: if ( ($advert_category == $page_id) && $advert_page == ''): echo 'cat ad'; $cat_banner[]=$order->id; endif; if ( $advert_category == '' && ($advert_page == $page_id) ): echo 'page ad'; $page_banner[]=$order->id; endif; if ( $advert_category == '' && ($advert_page == '') ): echo 'no matches - random'; $random_banner[]=$order->id; endif; It still returns: Array ( ) Array ( ) Array ( [0] => 1652 ) Surely this method doesn't overwrite the array as it's 3 different arrays? Quote Link to comment https://forums.phpfreaks.com/topic/302704-array-missing-some-values/#findComment-1540151 Share on other sites More sharing options...
Jacques1 Posted December 8, 2016 Share Posted December 8, 2016 This code snippet doesn't tell me anything. I need to see the context. It doesn't matter if you have three arrays or one. If you overwrite them, you overwrite them. I'm not sure why this is so hard to understand. Quote Link to comment https://forums.phpfreaks.com/topic/302704-array-missing-some-values/#findComment-1540152 Share on other sites More sharing options...
jarvis Posted December 8, 2016 Author Share Posted December 8, 2016 Thanks Jacques1 I get what you're saying but can't see how I can get around it? Perhaps you could elaborate? Give me a clue?? I appreciate your time and help and am keen to get this working, so you assistance is very helpful - thanks Quote Link to comment https://forums.phpfreaks.com/topic/302704-array-missing-some-values/#findComment-1540153 Share on other sites More sharing options...
Solution Jacques1 Posted December 8, 2016 Solution Share Posted December 8, 2016 I've already told you what to do: Put the array initialization before the loops, so that they only happen once and aren't repeated in every iteration. You want to keep the same array throughout all iterations. Quote Link to comment https://forums.phpfreaks.com/topic/302704-array-missing-some-values/#findComment-1540154 Share on other sites More sharing options...
jarvis Posted December 8, 2016 Author Share Posted December 8, 2016 Thanks, it now works! Apologies Quote Link to comment https://forums.phpfreaks.com/topic/302704-array-missing-some-values/#findComment-1540155 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.