7 replies
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
#needed #php
  • Profile picture of the author SteveJohnson
    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?
    Signature

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

    Gun control means never having to say, "I missed you."

    {{ DiscussionBoard.errors[3558281].message }}
    • Profile picture of the author andy black
      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
      {{ DiscussionBoard.errors[3558444].message }}
  • Profile picture of the author imarketstuff
    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

    Signature
    I MARKET STUFF

    {{ DiscussionBoard.errors[3558739].message }}
    • Profile picture of the author andy black
      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
      {{ DiscussionBoard.errors[3558812].message }}
  • Profile picture of the author SteveJohnson
    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.
    Signature

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

    Gun control means never having to say, "I missed you."

    {{ DiscussionBoard.errors[3558898].message }}
  • Profile picture of the author andy black
    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
    {{ DiscussionBoard.errors[3558989].message }}
    • Profile picture of the author SteveJohnson
      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?
      Signature

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

      Gun control means never having to say, "I missed you."

      {{ DiscussionBoard.errors[3559309].message }}

Trending Topics