...

View Full Version : display code based on url segments



howie2009
05-20-2011, 11:39 PM
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



?>

Horologe
05-21-2011, 12:07 AM
Dude, don't reinvent the wheel.

I highly suggest Codeignitor.com

howie2009
05-21-2011, 12:13 AM
Thanks will do! It will probably go way over my head tho. Starting out

Horologe
05-21-2011, 07:21 AM
Nah there are about 20 tuts on youtube on how to use it, and on their forums there is some cool stuff. check the vids on the main page and you can get a basic understanding on how much faster it is.

howie2009
05-21-2011, 10:15 AM
Thanks. Hopefully a php guru can advise on the below php code (re PHP 5).

I came across this code when googling that appears to isolate url segments



<?php


$uri = $_SERVER['REQUEST_URI'];
$segment = explode("/", $uri);
$uri_3 = $segment[3];
echo '$segment[3]'; //is this syntax correct?
?>

If the url in the browser is say www.mysite.com/animals/dogs/labrador I need the output of my php attempt to be labrador.
Thanks

dastii
05-21-2011, 01:33 PM
echo '$segment[3]';


must be this:



echo $segment[3];


Now put that code in a index.php file and call it like this:

yoursite.com/index.php/segment1/segment2/segment3

to make your url look like yoursite.com/segment1/segment2/segment3 you need a .htaccess file.

create a .htaccess file with the following content next to your index.php file



RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /index.php/$1 [L]

howie2009
05-21-2011, 05:01 PM
Thanks!

kbluhm
05-21-2011, 06:42 PM
In an include file somewhere near the top of your script, it may be handy to define some constants:


/* - - - - - - - - - -
* Parse our segments from the URL
*/

list( $segments ) = explode( '?', $_SERVER['REQUEST_URI'] );
$segments = preg_split( '/\/+/', $segments );

/* - - - - - - - - - -
* Define segment constants
* Up to 9... any more than that is a crazy URL!
* Case insensitive: SEGMENT_1, segment_1, Segment_1, etc
*/

foreach ( range( 1, 9 ) as $index )
{
define( 'SEGMENT_' . $index , isset( $segments[ $index ] ) ? $segments[ $index ] : NULL, TRUE );
}

Then in your code, the segments are now available as constant values to be used anywhere, regardless of scope (inside functions, etc):


switch ( segment_1 )
{

case '':
// index page
break;

case 'contant':
// contact form
break;

case 'about':
// about us
break;

default:
// error
break;

}

...or:


if ( segment_3 == 'blackcat' )
{
include 'nice_weekend.php';
}
else
{
include 'nice_day.php';
}

...etc.

tangoforce
05-21-2011, 08:38 PM
@howie: There are [php] tags you can use on this site instead of [code]. In case you missed it, there is a sticky topic asking us all to use them so there is a link in my sig if you need more info ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum