Jump to content

php url segments


howster2011

Recommended Posts

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



?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.