Register Advertise with usHelp Desk Today's Posts Search

Closed Thread
Thread Tools Search this Thread
Unread 19th Mar 2011, 07:21 AM   #1
HyperActive Warrior
War Room Member
 
andy black's Avatar
 
Join Date: 2008
Location: Yorkshire UK
Posts: 301
Thanks: 47
Thanked 23 Times in 14 Posts
Default
PHP Help Needed
Share on: 
fb share twitter share gplus share more share

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
andy black is offline  
Unread 19th Mar 2011, 10:00 AM   #2
VIP Warrior
War Room Member
 
SteveJohnson's Avatar
 
Join Date: 2007
Location: Caldwell, Idaho, USA.
Posts: 1,967
Thanks: 1,024
Thanked 2,043 Times in 1,183 Posts
Default
Re: PHP Help Needed
Share on: 
fb share twitter share gplus share more share

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?

The 2nd Amendment, 1789 - The Original Homeland Security.

Gun control means never having to say, "I missed you."
SteveJohnson is offline  
Unread 19th Mar 2011, 10:39 AM   #3
HyperActive Warrior
War Room Member
 
andy black's Avatar
 
Join Date: 2008
Location: Yorkshire UK
Posts: 301
Thanks: 47
Thanked 23 Times in 14 Posts
Default
Re: PHP Help Needed
Share on: 
fb share twitter share gplus share more share

Originally Posted by SteveJohnson View Post

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
andy black is offline  
Unread 19th Mar 2011, 11:43 AM   #4
HyperActive Warrior
War Room Member
 
Join Date: 2009
Location: Costa Rica
Posts: 164
Thanks: 63
Thanked 44 Times in 41 Posts
Default
Re: PHP Help Needed
Share on: 
fb share twitter share gplus share more share

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
} 

I MARKET STUFF

imarketstuff is offline  
Unread 19th Mar 2011, 11:55 AM   #5
HyperActive Warrior
War Room Member
 
andy black's Avatar
 
Join Date: 2008
Location: Yorkshire UK
Posts: 301
Thanks: 47
Thanked 23 Times in 14 Posts
Default
Re: PHP Help Needed
Share on: 
fb share twitter share gplus share more share

Originally Posted by imarketstuff View Post

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
andy black is offline  
Unread 19th Mar 2011, 12:13 PM   #6
VIP Warrior
War Room Member
 
SteveJohnson's Avatar
 
Join Date: 2007
Location: Caldwell, Idaho, USA.
Posts: 1,967
Thanks: 1,024
Thanked 2,043 Times in 1,183 Posts
Default
Re: PHP Help Needed
Share on: 
fb share twitter share gplus share more share

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.

The 2nd Amendment, 1789 - The Original Homeland Security.

Gun control means never having to say, "I missed you."
SteveJohnson is offline  
The Following User Says Thank You to SteveJohnson For This Useful Post:
Unread 19th Mar 2011, 12:29 PM   #7
HyperActive Warrior
War Room Member
 
andy black's Avatar
 
Join Date: 2008
Location: Yorkshire UK
Posts: 301
Thanks: 47
Thanked 23 Times in 14 Posts
Default
Re: PHP Help Needed
Share on: 
fb share twitter share gplus share more share

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
andy black is offline  
Unread 19th Mar 2011, 01:37 PM   #8
VIP Warrior
War Room Member
 
SteveJohnson's Avatar
 
Join Date: 2007
Location: Caldwell, Idaho, USA.
Posts: 1,967
Thanks: 1,024
Thanked 2,043 Times in 1,183 Posts
Default
Re: PHP Help Needed
Share on: 
fb share twitter share gplus share more share

Originally Posted by andy black View Post

Hi Steve

Thanks, that was what I needed
Good, glad it worked for you.

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?

The 2nd Amendment, 1789 - The Original Homeland Security.

Gun control means never having to say, "I missed you."
SteveJohnson is offline  
Closed Thread


Bookmarks

Tags
needed, php


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




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