Forum Moderators: coopster

Message Too Old, No Replies

Dynamic Navigation

Are there wildcard options?

         

King of Bling

12:44 pm on Nov 5, 2004 (gmt 0)

10+ Year Member



Good day,

I am trying to construct a dynamic navigation that expands when a visitor selects a sub-directory. The site has 6 sub-directories, plus the root. Is there a way to use a wildcard or something, to indicate all contents of a directory?

For example (this does not work):

<?if($_SERVER['PHP_SELF']=="/cars/*"){?>

Anyone know of a solution? What about the root level, is there an option for that too?

Thanks in advance!
KOB

mincklerstraat

1:13 pm on Nov 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, "if's" statements don't accept wildcards. You have to think of it this way:

'if' is a control structure that evaluates the stuff that's in the parentheses directly following, and executes the following line, or the following lines inside curly braces, if the result of that statement is TRUE (or 1, or anything that's not 0 basically).

So what you're evaluating here is the logical statement:

($_SERVER['PHP_SELF']=="/cars/*")

This depends on '==', which is one of the many operators [be2.php.net], more specifically, a comparison operator [be2.php.net].
'==' just sees if the two values on each side of it are the same (same value, not necessarily same type (ie., integer, float, string, boolean (true or false) - but we won't get into that here).

It's not a matching sort of operator that can take wildcards.

What you'll need to do is use string functions that can in some way operate like matching operators. The easiest is

strpos()
[be2.php.net], which will just return the value of where the string you're looking for (the 'needle') is inside of the bigger string you're looking in (the 'haystack'). A potential problem with this is that it returns the number 0 if the string you're looking for is right at the beginning of the haystack string - since the beginning position is '0' - but the value 0 (and not the type) is equivalent to FALSE, which is what you get if the string isn't there at all.

You need to use the 'Identical' operator, '===', which checks to see if both values are identical - i.e., aren't only of just the same value, but also the same type (string, int, float, boolean).

So what you get putting this together is:

if(strpos('$_SERVER['PHP_SELF'], '/cars/')) === 0)

You want to check that it's at the beginning of the string (0), but than it's not FALSE (what's returned if the string isn't there at all).

You can also use

preg_match()
, but that's a bit more advanced -- you'll definitely want to use it since it's often looked upon as a programmer's 'swiss army knife', but give yourself some time in the more basic php stuff before you try to sink your teeth into it.

N.b.: 'try before you buy' - be sure to test this out before using.