Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder howie2009's Avatar
    Join Date
    May 2009
    Location
    Ireland
    Posts
    202
    Thanks
    37
    Thanked 0 Times in 0 Posts

    display code based on url segments

    Hi Guys,

    ExpressionEngine cms uses url segment variables:
    http://expressionengine.com/user_gui..._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}
    Code:
    <?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
    
    
    
    ?>
    Last edited by howie2009; 05-20-2011 at 11:07 PM.

  • #2
    New Coder
    Join Date
    May 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Dude, don't reinvent the wheel.

    I highly suggest Codeignitor.com

  • #3
    Regular Coder howie2009's Avatar
    Join Date
    May 2009
    Location
    Ireland
    Posts
    202
    Thanks
    37
    Thanked 0 Times in 0 Posts
    Thanks will do! It will probably go way over my head tho. Starting out

  • #4
    New Coder
    Join Date
    May 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Regular Coder howie2009's Avatar
    Join Date
    May 2009
    Location
    Ireland
    Posts
    202
    Thanks
    37
    Thanked 0 Times in 0 Posts
    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


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

  • #6
    New to the CF scene
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post
    Code:
    echo '$segment[3]';
    must be this:

    Code:
    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

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

  • Users who have thanked dastii for this post:

    howie2009 (05-21-2011)

  • #7
    Regular Coder howie2009's Avatar
    Join Date
    May 2009
    Location
    Ireland
    Posts
    202
    Thanks
    37
    Thanked 0 Times in 0 Posts
    Thanks!

  • #8
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    In an include file somewhere near the top of your script, it may be handy to define some constants:
    PHP Code:
    /* - - - - - - - - - -
     * 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 ( range1) as $index )
    {
        
    define'SEGMENT_' $index , isset( $segments$index ] ) ? $segments$index ] : NULLTRUE );

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

        case 
    '':
            
    // index page
            
    break;

        case 
    'contant':
            
    // contact form
            
    break;

        case 
    'about':
            
    // about us
            
    break;

        default:
            
    // error
            
    break;


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

    ...etc.
    Last edited by kbluhm; 05-21-2011 at 06:43 PM.

  • #9
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,091
    Thanks
    51
    Thanked 506 Times in 493 Posts
    @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
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •