Log in

View Full Version : Resolved IF statement contains....



X96 Web Design
08-28-2009, 06:47 AM
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

bluewalrus
08-28-2009, 08:07 AM
Like if url is http://www.site.com/directory?TEXTTOTEST

and have if url contains texttotest do something?

JShor
08-28-2009, 03:55 PM
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

$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:)

X96 Web Design
08-28-2009, 05:51 PM
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?


<?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 \\

bluewalrus
08-28-2009, 06:01 PM
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.

X96 Web Design
08-28-2009, 06:09 PM
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

JasonDFR
08-29-2009, 07:16 AM
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.

X96 Web Design
08-31-2009, 03:03 AM
No CMS, just my website.

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

// X96 \\