Results 1 to 8 of 8

Thread: IF statement contains....

  1. #1
    Join Date
    Feb 2009
    Posts
    303
    Thanks
    18
    Thanked 36 Times in 36 Posts

    Question IF statement contains....

    Here's what I'm trying to do...

    I have a variable, $url, and it returns the current URL.

    If the URL is /portfolio, then the specific code is executed, but if the URL is /portfolio?something the code doesn't execute...

    Is there a way to make an IF statement where it queries if the variable CONTAINS a certain string?

    Sorry, but I can't find an easier way to explain it....

    Cheers,
    X96
    Last edited by X96 Web Design; 08-31-2009 at 03:09 AM.
    Alex Blackie, X96 Design
    My Website
    I specialize in: HTML5, CSS3, PHP, Ruby on Rails, MySQL, MongoDB, Linux Server Administration

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Like if url is http://www.site.com/directory?TEXTTOTEST

    and have if url contains texttotest do something?

  3. #3
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Yeah, like bluewalrus said, you could try to have it so that it reads the query string for it. You could use explode to determine the actual file name, then have it execute the query string. Problem is, $_SERVER['QUERY_STRING']; might not find the query string since the extension .php is missing, and you're having problems with it.

    PHP Code:
    <?php

    $ex 
    explode("?"$_SERVER[REQUEST_URI]); // explode the url

    foreach($ex as $x) {
    $s .= $x;
    }

    $replaced str_replace($_SERVER['PHP_SELF'], $s); // remove query

    if($replaced == 'something') {

    // execute code 

    }

    ?>
    Have you also considered this may be a .htaccess problem? That might be another solution. It seems very abnormal that php would have a problem executing/determining a query string, even w/o the .php extension.

    HTH
    - Josh

  4. #4
    Join Date
    Feb 2009
    Posts
    303
    Thanks
    18
    Thanked 36 Times in 36 Posts

    Default

    It'll do the execution fine - it's if the URI is: /portfolio?something, I want to know if it contains "some".... Let me speak in code..

    I'm sure I need to change the operator sign. But which sign do I use?
    Code:
    <?php
      $uri = $_SERVER["REQUEST_URI"];
      if($uri == "some"){ //But it doesn't work if the URL is not EXACTLY some.
        echo "Some.";
      }
    ?>
    I don't think it's a .htaccess problem...
    // X96 \\
    Alex Blackie, X96 Design
    My Website
    I specialize in: HTML5, CSS3, PHP, Ruby on Rails, MySQL, MongoDB, Linux Server Administration

  5. #5
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    So you want to be able to control by what's after the question mark what was entered or clicked?

    Like if something load something, if some load some, if som load som not just one word or phrase.

  6. #6
    Join Date
    Feb 2009
    Posts
    303
    Thanks
    18
    Thanked 36 Times in 36 Posts

    Default

    What I'm using it for is so depending on the Request URI, jQuery adds a "selected" class to the specific A tag with that ID.

    But, in the portfolio, I have each client page like this: portfolio?client=clientname. And the URL CONTAINS portfolio, but isn't EXACTLY portfolio, and so the PHP rules it out, and the "selected" class isn't applied to a#portfolio.

    Is there a sign (like == in the IF) to determine if the variable contains a string of text?

    Thanks,
    X96
    Alex Blackie, X96 Design
    My Website
    I specialize in: HTML5, CSS3, PHP, Ruby on Rails, MySQL, MongoDB, Linux Server Administration

  7. #7
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Quote Originally Posted by X96 Web Design View Post
    Is there a sign (like == in the IF) to determine if the variable contains a string of text?
    No sign to do this, but you can use strpos() to determine if a string contains a certain string.

    Are you using Wordpress or some other CMS? If so, there is usually a way to determine the current page.

  8. #8
    Join Date
    Feb 2009
    Posts
    303
    Thanks
    18
    Thanked 36 Times in 36 Posts

    Default

    No CMS, just my website.

    I decided just to manually add the selected class, instead of use PHP. But thanks for your help!

    // X96 \\
    Last edited by X96 Web Design; 08-31-2009 at 03:08 AM.
    Alex Blackie, X96 Design
    My Website
    I specialize in: HTML5, CSS3, PHP, Ruby on Rails, MySQL, MongoDB, Linux Server Administration

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •