Warrior Forum - The #1 Digital Marketing Forum & Marketplace

Warrior Forum - The #1 Digital Marketing Forum & Marketplace (https://www.warriorforum.com/)
-   Programming (https://www.warriorforum.com/programming/)
-   -   PHP Help Needed (https://www.warriorforum.com/programming/350864-php-help-needed.html)

andy black 19th March 2011 07:21 AM

PHP Help Needed
 
Can anyone help with this PHP problem

$dispnosidebar = get_option('it_disp_noside_bar');

The variable above gets the values I need from the options page in wordpress. The problem I am having is getting the variable/values into the code below where the 2,9 is.

if ($dispsidebar == "Left" && in_array( $post->ID, array(2,9))) { ?>

Thanks

Andrew Black

SteveJohnson 19th March 2011 10:00 AM

Re: PHP Help Needed
 
Maybe you could post a little more of what you're doing, Andrew? Or at least explain a little...

The section where the 2,9 is is testing that the post id is either 2 or 9. Are you wanting to add a third test for the new option?

andy black 19th March 2011 10:39 AM

Re: PHP Help Needed
 
Quote:

Originally Posted by SteveJohnson (Post 3558281)
Maybe you could post a little more of what you're doing, Andrew? Or at least explain a little...

The section where the 2,9 is is testing that the post id is either 2 or 9. Are you wanting to add a third test for the new option?

Hi Steve

Thanks for the reply

The bit of code that is 2,9 I need to replace with something that puts the contents of the variable $dispnosidebar there instead, the variable will contain the numbers for the posts/pages that I don't want the sidebar displayed on eg:

if ($dispsidebar == "Left" && in_array( $post->ID, array(echo $dispnosidebar))) { ?>

But that dosen't work

I think the code needs to be different but not sure what

Thanks

Andrew Black

imarketstuff 19th March 2011 11:43 AM

Re: PHP Help Needed
 
i don't follow you 100%, but how about this code snippet:

PHP Code:

.
.
$ 
dispsidebar = get_option('it_disp_noside_bar');

if (( $ 
dispsidebar == "Left") && (in_array($ post->ID,array('2','9')))
{
      
// do something
} 


andy black 19th March 2011 11:55 AM

Re: PHP Help Needed
 
Quote:

Originally Posted by imarketstuff (Post 3558739)
i don't follow you 100%, but how about this code snippet:

PHP Code:

.
.
$ 
dispsidebar = get_option('it_disp_noside_bar');

if (( $ 
dispsidebar == "Left") && (in_array($ post->ID,array('2','9')))
{
      
// do something
} 


Hi

The code I gave in the OP already works, I just need to replace the 2,9 with the values from the $dispnosidebar variable

Andrew Black

SteveJohnson 19th March 2011 12:13 PM

Re: PHP Help Needed
 
All right. You're going to make me get complicated here.

I'm assuming that your option contains a string from a form text input or textarea, containing numbers separated by commas. If that's not what's happening, none of the following will do you any good...

The option value isn't usable as-is. That is, it contains a string of characters, no different than a sentence. You have to get the number values out somehow before you can use them. You can't just stuff them into a function as you're trying to do.

SO - split the string on the commas. The following splits the string by any amount of commas and space characters, including tabs and returns/linefeeds:

$dispnosidebar_arr = preg_split( "/[\s,]+/", $dispnosidebar );

NOW you have an array you can insert into your test:

if ( $dispsidebar == "Left" && in_array( $post->ID, $dispnosidebar_arr ) ) {

Note that you're not going to want to use this, by itself, on any kind of production plugin or theme as the input ( the value of the option ) isn't sanitized or verified at all.

andy black 19th March 2011 12:29 PM

Re: PHP Help Needed
 
Hi Steve

Thanks, that was what I needed

What is the issue with the option value not being sanitized or verified?

Thanks again

Andrew Black

SteveJohnson 19th March 2011 01:37 PM

Re: PHP Help Needed
 
Quote:

Originally Posted by andy black (Post 3558989)
Hi Steve

Thanks, that was what I needed

Good, glad it worked for you.

Quote:

What is the issue with the option value not being sanitized or verified?
Simply this: you can never be sure, when you're working with user input, that what's there is what's supposed to be there. You always have to assume that the user is going to screw it up somehow, and your job is to try to anticipate what they might be able to do to mess it up, and catch it when it happens -- because it WILL HAPPEN, on that you can depend.

For instance, what if the user separated the list with semi-colons instead of commas? Or a slash, or even just a space? What if they put a word in there instead of a number character? What if they left it blank? What if they used two commas in a row?


All times are GMT -6. The time now is 02:50 AM.