howster2011 Posted May 20, 2011 Share Posted May 20, 2011 Hi Guys, ExpressionEngine cms uses url segment variables: http://expressionengine.com/user_guide/templates/globals/url_segments.html so just wondering how to do the following with raw php: If i have 2 urls for demo purposes: www.mysite.com/animals/dogs/terrier www.mysite.com/animals/cats/blackcat where segment1 = animals segment2 = dogs and segment3 = terrier I'd like to be able to use raw php in my templates such that: if segment1= animals show this html/php code else show this other html/php code or if segment3= blackcat show this html/php code else show this other html/php code and so on for upto 5 segments or more. I'm phrasing it very basically out of my own PHP newbie ignorance but hopefully someone will know the code I'm after. The segments will relate to the url in the browser as opposed to some fixed url like the samples above. Thanks Googling revealed 2 examples which I fused together so syntax probably totally wrong to try and explain. (may help} <?php $uri = $_SERVER['REQUEST_URI']; $segment = explode("/", $uri); $uri_3 = $segment[3]; if ($segment[3]=="blackcat") echo "Have a nice weekend!"; //i want to echo a chunk of html code though else echo "Have a nice day!"; //i want to echo a chunk of html code though if ($segment[1]=="animals") echo "Have a nicer weekend!"; //i want to echo a chunk of html code though else echo "Have a nicer day!"; //i want to echo a chunk of html code though ?> Quote Link to comment https://forums.phpfreaks.com/topic/237013-php-url-segments/ Share on other sites More sharing options...
howster2011 Posted May 20, 2011 Author Share Posted May 20, 2011 In relation to the code above I'd like to be able to output say: <div class="textColor"> <h1>title</h1> <p> Hello phpfreaks</p> <h2> some title</h2> <p> another paragraph</p> </div> in each echo THANKS Quote Link to comment https://forums.phpfreaks.com/topic/237013-php-url-segments/#findComment-1218274 Share on other sites More sharing options...
wildteen88 Posted May 21, 2011 Share Posted May 21, 2011 To output a chunk of html you can do either of the following Using single quotes (make sure you escape any ' within your string using \') echo '<div class="textColor"> <h1>title</h1> <p> Hello phpfreaks</p> <h2> some title</h2> <p> another paragraph</p> </div>'; Or double quotes (make sure you escape any " within your string using \") echo "<div class=\"textColor\"> <h1>title</h1> <p> Hello phpfreaks</p> <h2> some title</h2> <p> another paragraph</p> </div>"; Or using heredoc syntax echo <<<HTML <div class="textColor"> <h1>title</h1> <p> Hello phpfreaks</p> <h2> some title</h2> <p> another paragraph</p> </div> HTML; Or you can go in/out of PHP <?php // your code here ?> <div class="textColor"> <h1>title</h1> <p> Hello phpfreaks</p> <h2> some title</h2> <p> another paragraph</p> </div> <?php // some more code here ?> Quote Link to comment https://forums.phpfreaks.com/topic/237013-php-url-segments/#findComment-1218358 Share on other sites More sharing options...
howster2011 Posted May 21, 2011 Author Share Posted May 21, 2011 Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/237013-php-url-segments/#findComment-1218395 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.