epicalex Posted June 9, 2008 Share Posted June 9, 2008 I'm fairly new to php, but I'm working my way through things fine, but I've hit a problem. I've stored some values in an array, and they are in a mysql database. I get the array back and find a value from it, and I want to test if that value is equal to something. I've echoed the value out so I know that it is what it is supposed to be but I can't get this code to work. I'm using WordPress for this. function my_function() { if(is_single() && $myOption['my_option_key'] == 'true') { echo "whatever"; } else {} } This is supposed to echo 'whatever' if I'm on a single post page, and 'my_option_key' is equal to true, which it is. My problem is that nothing is being echoed out. I have if($myOption['my_option_key'] == 'true') working somewhere else, but in this instance it won't work. Is there anything wrong with my code above? If everything looks fine then I'll assume its a WordPress issue and post in their forums. Thanks in advance Link to comment https://forums.phpfreaks.com/topic/109339-if-array-value-equal-to-xxx/ Share on other sites More sharing options...
keeB Posted June 9, 2008 Share Posted June 9, 2008 Are you sure 'my_option_key' is inside $myOption array? print_r($myOption); Post the results. Link to comment https://forums.phpfreaks.com/topic/109339-if-array-value-equal-to-xxx/#findComment-560839 Share on other sites More sharing options...
mushroom Posted June 9, 2008 Share Posted June 9, 2008 I've stored some values in an array, and they are in a mysql database. I get the array back and find a value from it, and I want to test if that value is equal to something. you may wish to incorporate this if (in_array($your_value,$array)) Link to comment https://forums.phpfreaks.com/topic/109339-if-array-value-equal-to-xxx/#findComment-560857 Share on other sites More sharing options...
epicalex Posted June 9, 2008 Author Share Posted June 9, 2008 I used print_r and I got a list of keys and values, and my key was there with the value 'true', which it needed to be. Just to add, if you hadn't guessed, that I'm using the function I defined in a php file, with the hope that if the above conditions are true, then it will echo 'whatever'. I've read a bit about 'return', do I need to use that in this case? Mushroom, could you explain that code to me, and how it would help, thanks. Thanks again Link to comment https://forums.phpfreaks.com/topic/109339-if-array-value-equal-to-xxx/#findComment-561020 Share on other sites More sharing options...
keeB Posted June 9, 2008 Share Posted June 9, 2008 Can I see the function you are using, please? Link to comment https://forums.phpfreaks.com/topic/109339-if-array-value-equal-to-xxx/#findComment-561146 Share on other sites More sharing options...
mushroom Posted June 9, 2008 Share Posted June 9, 2008 Mushroom, could you explain that code to me, and how it would help, thanks. Thanks again If you look at the line I quoted, there is a built in php function to do what you are asking. http://ca.php.net/manual/en/function.in-array.php Link to comment https://forums.phpfreaks.com/topic/109339-if-array-value-equal-to-xxx/#findComment-561183 Share on other sites More sharing options...
epicalex Posted June 16, 2008 Author Share Posted June 16, 2008 OK, so i've been away for a few days and come back and done some more digging and reading. I've found that my problem is because I am using a php class structure, and then trying to call variables that are not globally defined. I'm sure there must be a way to get around this, and my array needs to be in the class. So the relevant code(with some wordpress functions there too) is: function EpicTMAdminOptions() { $epic_t_m_admin_options = array( 'epictmcolumns' => 'true', 'epictmmidtextask' => 'true', 'epictmmidtext' => 'This is some sample text to put in the middle of your post', 'epictmmidcss' => '<style type="text/css"></style>'); $EpicTMOptions = get_option($this->epic_t_m_admin_options_name); if (!empty($EpicTMOptions)) { foreach ($EpicTMOptions as $key => $option) $epic_t_m_admin_options[$key] = $option; } update_option($this->epic_t_m_admin_options_name, $epic_t_m_admin_options); return $epic_t_m_admin_options; } And I'm calling that $EpicTMOptions array like this in another file: if(is_single() && $EpicTMOptions['epictmcolumns'] == "true") { echo "blah"; } else {} } What do I need to do to access this? Thanks again everyone Link to comment https://forums.phpfreaks.com/topic/109339-if-array-value-equal-to-xxx/#findComment-566815 Share on other sites More sharing options...
keeB Posted June 17, 2008 Share Posted June 17, 2008 Pass it as a parameter function EpicTMAdminOptions($EpicTMOptions) { $epic_t_m_admin_options = array( 'epictmcolumns' => 'true', 'epictmmidtextask' => 'true', 'epictmmidtext' => 'This is some sample text to put in the middle of your post', 'epictmmidcss' => '<style type="text/css"></style>'); $EpicTMOptions = get_option($this->epic_t_m_admin_options_name); if (!empty($EpicTMOptions)) { foreach ($EpicTMOptions as $key => $option) $epic_t_m_admin_options[$key] = $option; } update_option($this->epic_t_m_admin_options_name, $epic_t_m_admin_options); return $epic_t_m_admin_options; } Link to comment https://forums.phpfreaks.com/topic/109339-if-array-value-equal-to-xxx/#findComment-566956 Share on other sites More sharing options...
epicalex Posted June 17, 2008 Author Share Posted June 17, 2008 OK, I tried doing that and to start with I got the following warning: Warning: Missing argument 1 for EpicTextManipulator::EpicTMAdminOptions() EpicTextManipulator is my class name. But then I added that parameter again to another reference to that function(EpicTMAdminOptions), and now the warning has gone, but it still isn't working. I'm thinking of starting again and removing the class structure. Any last ideas? Thanks again Link to comment https://forums.phpfreaks.com/topic/109339-if-array-value-equal-to-xxx/#findComment-567178 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.