Jump to content

If Array Value Equal to xxx


epicalex

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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