CyberShot Posted November 10, 2017 Share Posted November 10, 2017 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>'; } } Quote Link to comment Share on other sites More sharing options...
phpmillion Posted November 10, 2017 Share Posted November 10, 2017 You didn't post ot_get_option function, so we don't know what type of values it's supposed to set. You should either post it here, either describe what exactly this function returns (sets) and what exact values you want to be skipped. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 10, 2017 Share Posted November 10, 2017 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 ? Quote Link to comment Share on other sites More sharing options...
phpmillion Posted November 10, 2017 Share Posted November 10, 2017 My best guess that he was trying to test length (incorrectly), but it's hard to say without seeing the actual function. Quote Link to comment Share on other sites More sharing options...
CyberShot Posted November 11, 2017 Author Share Posted November 11, 2017 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>'; } } Quote Link to comment Share on other sites More sharing options...
phpmillion Posted November 11, 2017 Share Posted November 11, 2017 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. Quote Link to comment Share on other sites More sharing options...
CyberShot Posted November 11, 2017 Author Share Posted November 11, 2017 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? Quote Link to comment Share on other sites More sharing options...
Solution CyberShot Posted November 11, 2017 Author Solution Share Posted November 11, 2017 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>'; } } Quote Link to comment Share on other sites More sharing options...
phpmillion Posted November 12, 2017 Share Posted November 12, 2017 (edited) Yes, you did it correct this time. For future, keep in mind that: ["flickr"]=> string(0) "" 0 means the length of string, not the string itself. The value of string itself is between " symbols. Edited November 12, 2017 by phpmillion 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.