Jump to content

help with array check


CyberShot
Go to solution Solved by CyberShot,

Recommended Posts

I am trying to make a function that displays some social media stuff. It's working but I needed to check if the options was set and if it was not set to skip that one and move to the next. My code will either display all four options or none at all. I have tried a few different methods but this is the one I am on now. The social array as is will print

 

array(4) {
["facebook"]=>
string(1) "#"
["flickr"]=>
string(0) ""
["twiiter"]=>
string(0) ""
["google"]=>
string(0) ""

 

I figured the code below would only print out the Facebook logo but as it is, it does not. I can't figure out a good way to check each value of the array to see if it is set so that if it isn't it will skip that setting and move one to the next.

function social(){
	if(function_exists('ot_get_option')){
		$facebook	= ot_get_option('facebook');
		$flickr		= ot_get_option('flickr');
		$twitter	= ot_get_option('twitter');
		$google		= ot_get_option('google_plus');
		
		$social = array('facebook' => $facebook, 'flickr' => $flickr, 'twiiter' => $twitter, 'google' => $google);
		
		$count = 1;
		echo '<ul class="list-social pull-right">';
		foreach($social as $link => $title){
			$the_link = trim($link);
			if($the_link != 0){
				echo '<li>';
				echo '<a class="tipsy2 icon-'.$count.'" title="'.$title.'" href="'.$link.'"></a>';
				echo '</li>';
			}
			$count ++;
		}
		echo '</ul>';
	}
}
Link to comment
Share on other sites

In their wisdom, the designers of PHP used exactly the same relationships between key and value in the array definnitions and in a foreach loop syntax.

 

An array element is defined by $key => $value and these are retrieved as $key =>$value in the foreach.

 

You have you array defined as $link => $title so when you retrieve them you get 'facebook' as the $link and whatever in the $title

 

Why, therefore, are you testing if the $link==0 ?

Link to comment
Share on other sites

phpmilliion. I posted the array that the ot_get_option returns instead of the function. It does exactly what I need it to do. That would be this

 

array(4) {
["facebook"]=>
string(1) "#"
["flickr"]=>
string(0) ""
["twiiter"]=>
string(0) ""
["google"]=>
string(0) ""

 

as you can see, the facebook key has a value of "#". What i want the code to do is go through each key and when there is no value or just an empty string, I want it to skip that one and move to the next one so that it won't print the list item for that empty social link.

 

I am edited my code to be more proper; however, it still does not do what I need it to do.

function social(){
	if(function_exists('ot_get_option')){
		$facebook	= ot_get_option('facebook');
		$flickr		= ot_get_option('flickr');
		$twitter	= ot_get_option('twitter');
		$google		= ot_get_option('google_plus');
		
		$social = array('facebook' => $facebook, 'flickr' => $flickr, 'twiiter' => $twitter, 'google' => $google);
		
		$count = 1;
		echo '<ul class="list-social pull-right">';
		foreach($social as $title => $link){
			$the_link = trim($link);
			if($the_link != 0){
				echo '<li>';
				echo '<a class="tipsy2 icon-'.$count.'" title="'.$title.'" href="'.$link.'"></a>';
				echo '</li>';
			}
			$count ++;
		}
		echo '</ul>';
	}
}
Link to comment
Share on other sites

Alright, that makes sense. However, take a look at your code:

if($the_link != 0)

Each and every element in your array matches this condition. Hence, none of them is skipped. You should either modify your ot_get_option function to return 0 for elements that need to be skipped, or use other condition inside if statement.

Link to comment
Share on other sites

modifying the ot_get_option function is not an option. It's a WordPress plugin. I was trying to target this

 

["flickr"]=>
string(0) ""

 

as you can see. [flickr] is a string (0)

 

I wanted to say if that string is a 0, then skip it.

 

On a side note, I do not get instant notifications from this site even though the settings are set to provide them. Have you ever had this issue?

Link to comment
Share on other sites

  • Solution

It appears I have figured it out.

function bliss_social(){
	if(function_exists('ot_get_option')){
		$facebook	= ot_get_option('facebook');
		$flickr		= ot_get_option('flickr');
		$twitter	= ot_get_option('twitter');
		$google		= ot_get_option('google_plus');
		
		$social = array(
			'facebook' => array(
			        'title'	=> 'Facebook',
				'link' => $facebook,
				'icon' => '1'
						),
			'flickr' =>   array(
				'title' => 'Flickr',
				'link' => $flickr, 
				'icon' => '2'
				),
			'twiiter' =>  array(
				'title' => 'Twitter',
				'link' => $twitter,
				'icon' => '3'
				),
			'google' =>   array(
				'title' => 'Google+',
				'link' => $google,
				'icon' => '4'
			    )
			);
		
		echo '<ul class="list-social pull-right">';
		foreach($social as $row ){
			if( !empty($row['link'])){
			echo '<li>';
			echo '<a class="tipsy2 icon-'.$row['icon'].'" original-title="'.$row['title'].'" href="'.$row['link'].'"></a>';
			echo '</li>';
			}
		}
		echo '</ul>';
	}
}
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.