Jump to content

Array missing some values


jarvis
Go to solution Solved by Jacques1,

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by jarvis
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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