Page 1 of 1

Need help with split string...

Posted: Mon Jan 24, 2005 11:54 am
by 123dhs321
I need to switch a style based on which directory your in.

I have one more question if you have time. By the way I’m a beginner at php. :)

This works with this url ….com/rideshare/:
<div id="<?php if ($_SERVER["REQUEST_URI"] == '/rideshare/') { echo 'ridesharetitle'; } else { echo 'hometitle'; }?>"</div>

Within the /rideshare/ folder I have dynamic premalinks gernerated by WP.
ex: ….com/rideshare/this-title/

How can I get the code
<div id="<?php if ($_SERVER["REQUEST_URI"] == '/rideshare/') { echo 'ridesharetitle'; } else { echo 'hometitle'; }?>"</div>
to include all the dynamic directories under the /rideshare/ directory? Hence, directories such as ….com/rideshare/this-title/ or ….com/rideshare/generated-title/ or ….com/rideshare/could-be-anything/

Site in development is here:
http://www.panictour.com/rideshare/

I recieved advise like this:

The logic for the last scenario is pretty much the same. You just need some function to split the string REQUEST_URI and place the substrings in an array. Something like:
<?php
list($directory, $subdirectory) = split('/', $_SERVER["REQUEST_URI"]);
echo '<div class="' . $directory . ' id="' . $subdirectory . '">';
?>

Thing is I dont know how to do this. Any help would be appreciated. I've been stuck on this for weeks now!

Thanks,
-Ryan

Posted: Mon Jan 24, 2005 12:16 pm
by feyd

Code: Select all

<div id="<?php if (strpos($_SERVER&#1111;"REQUEST_URI"],'/rideshare/') !== false) &#123; echo 'ridesharetitle'; &#125; else &#123; echo 'hometitle'; &#125;?>"</div>
is one way..

Posted: Mon Jan 24, 2005 12:34 pm
by 123dhs321
Works perfectly!


How would the code be accross many pages...

ie:

<div id="<?php if ($_SERVER["REQUEST_URI"] == '/rideshare/') { echo 'ridesharetitle'; } else ?php if ($_SERVER["REQUEST_URI"] == '/tourdates/') { echo 'tourdatestitle'; } else ?php if ($_SERVER["REQUEST_URI"] == '/ticketexchange/') { echo 'ticketexchangetitle'; } else { echo 'hometitle'; }?>"</div>

I know that may be wrong but you know what I'm saying... Thanks for the help so far.

-Ryan

Posted: Mon Jan 24, 2005 12:37 pm
by feyd

Code: Select all

if()
&#123;
...
&#125;
elseif(...)
&#123;
...
&#125;
elseif(...)
&#123;
...
&#125;
elseif(...)
&#123;
...
&#125;
else
&#123;
...
&#125;

Posted: Mon Jan 24, 2005 12:40 pm
by magicrobotmonkey
or more efficiently:

Code: Select all

switch($_SERVER&#1111;"REQUEST_URI"])
&#123;
    case "rideshare":
        echo "ridesharetitle";
    break;

    case "tourdates":
        echo "tourdatestitle";
    break;
&#125;

Posted: Mon Jan 24, 2005 12:49 pm
by feyd
missing some logic for what he's doing... ;)

Posted: Mon Jan 24, 2005 12:55 pm
by 123dhs321
Is this right:

<?php if (strpos($_SERVER["REQUEST_URI"],'/rideshare/') !== false) { echo 'ridesharetitle'; } elseif (strpos($_SERVER["REQUEST_URI"],'/tourdates/') !== false) { echo 'tourdatestitle'; } elseif (strpos($_SERVER["REQUEST_URI"],'/ticketexchange/') !== false) { echo 'ticketexchangetitle'; } else { echo 'hometitle'; }?>



It doesnt seem to work. This syntax is hard. Alot harder than asp!

Thanks for the help so far.

-ryan

Posted: Mon Jan 24, 2005 1:07 pm
by feyd
magicrobotmonkey illustrated an example of another way to do it.. there are many ways to do it in php.

Posted: Mon Jan 24, 2005 1:16 pm
by 123dhs321
I thought that magicrobotmonkey was missing the logic?

If not, how do I impliement that into my current code?

Posted: Mon Jan 24, 2005 2:14 pm
by magicrobotmonkey
oh right- woops. To do it my way, you would first have to pull the relevent bit out of the URI. Which if you could do it would be a lot better then a bunch of strpos()'s.

Posted: Mon Jan 24, 2005 2:15 pm
by 123dhs321
Can you give me an example based on my code? I'm really new to PHP.