Jump to content

If statement within an array


stefanlesik

Recommended Posts

Hi - I'm using the following code to print different the correct app link based on which device the user is viewing the page (using Wurfl). <a href="<?php echo $appLink ?>">Download the App link</a>

 if ($os == 'Android') { 
   $appName = 'Android app';
   $appLink = 'http://androidlink.com/1';
   
   } 
   else if ($os == 'iOS') 
   {  
   $appName = 'iPhone app';
   $appLink = 'http://iphonelink.com/1';
   } 

However, now I want display different links on different Wordpress pages using the same idea.

 if ($os == 'Android') { 
	if (is_page( 1 )) {
	$appLink = 'http://androidlink.com/1';
}
	if (is_page( 2 )) {
	$appLink = 'http://androidlink.com/2';
}
      
   } 

Is there a neat way of doing this?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/285712-if-statement-within-an-array/
Share on other sites

Try using arrays - 

$devices = array(
		'android'		=>		array(
			'name'			=>			'Android app',
			'link'			=>			'http://androidlink.com/1',
		),
		'ios'		=>		array(
			'name'			=>			'iPhone app',
			'link'			=>			'http://iphonelink.com/1',
		),
	);
	
	print $devices[$os]['name'] . ': ' . $devices[$os]['link'];

 

Try using arrays - 

$devices = array(
		'android'		=>		array(
			'name'			=>			'Android app',
			'link'			=>			'http://androidlink.com/1',
		),
		'ios'		=>		array(
			'name'			=>			'iPhone app',
			'link'			=>			'http://iphonelink.com/1',
		),
	);
	
	print $devices[$os]['name'] . ': ' . $devices[$os]['link'];

 

 

That has nothing to do with his issue and I question if you actually read the OP's post.

 

stefanlesik, I would ask what your real issue is apart from you not liking how your code is setup.

There is another method however, you can build your own custom array set storing the page id as a key and the value as the page url you want.

 

 <?php // This pseudo code would need to be replaced with your own setup.// The key is the page id while the value is the url you want to provide. $data = array(1 => 'first_route', 2 => 'second_route'); foreach($data as $page_id => $url){    if(is_page($page_id))    {        $appLink = 'http://site.com/' . $url;    }} 
 
Remember, is_page() accepts things other than the pages id such as the title or slug. e.g is_page('Homepage');

Is there a function to get the active page id?

 

You can adapt the array method to this - 

<?php

	$devices = array(
		'android'		=>		array(
			'name'					=>			'Android app',
			'1'						=>			'http://androidlink.com/1',
			'2'						=>			'http://androidlink.com/2',
		),
		'ios'		=>		array(
			'name'					=>			'iPhone app',
			'1'						=>			'http://iphonelink.com/1',
			'2'						=>			'http://iphonelink.com/2',
		),
	);
	
	$page = (is_page( 1 )) ? 1 : 2; // Replace this with function to get active page
	
	print $devices[$os]['name'] . ': ' . $devices[$os][$page];

?>

Archived

This topic is now archived and is closed to further replies.

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