Better way of doing this ?

Discussion in 'PHP' started by hasbehas, Mar 16, 2013.

  1. #1
    Hi all,

    I have this piece of code that prints a word/title for each page. Its alot longer than this :)

    <?
    if(preg_match("/aboutus\.html/",$_SERVER['REQUEST_URI'])) {
    print 'About us & contact';
    }
    if(preg_match("/beaches\.html/",$_SERVER['REQUEST_URI'])) {
    print 'All Beaches';
    }
    if(preg_match("/cafes\.html/",$_SERVER['REQUEST_URI'])) {
    print 'Cafes & Bars';
    }
    if(preg_match("/hotels\.html/",$_SERVER['REQUEST_URI'])) {
    print 'Accommodation';
    }
    else {
    print 'Home';
    }
    ?>
    PHP:
    Is there better or faster or easier way to do this ?
    You can see I am no expert with this :D
     
    Solved! View solution.
    hasbehas, Mar 16, 2013 IP
  2. D3Tek

    D3Tek Active Member

    Messages:
    164
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    50
    #2
    You could pull the titles from the database.

    Or you could try having an array of data.
     
    D3Tek, Mar 16, 2013 IP
  3. hasbehas

    hasbehas Well-Known Member

    Messages:
    726
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    190
    #3
    no DB I am afraid. Array sounds a good idea. you know how to ?
     
    hasbehas, Mar 16, 2013 IP
  4. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #4
    why not make use of arrays?

    
     
    $array = array('aboutus' => 'About us',
                   'beaches' => 'Beaches');
                   
    foreach ($array AS $page=>$title)
    {
        if (substr($_SERVER['REQUEST_URI'], 0, strlen($page) == $page) 
        {   
            echo $title  . ' found';
        }
    }
    
    Code (markup):

    something like this, but the rest you may figure out..
     
    EricBruggema, Mar 16, 2013 IP
  5. #5
    
    $titles = [
        'aboutus' => 'About Us',
        'beaches' => 'Beaches',
        // ...
    ];
     
    $file = basename($_SERVER['REQUEST_URI'], '.html');
     
    if (isset($titles[$file]))
    {
        echo $titles[$file];
    }
    else
    {
        echo 'Home';
    }
    
    PHP:
     
    nico_swd, Mar 16, 2013 IP
    hasbehas likes this.
  6. hasbehas

    hasbehas Well-Known Member

    Messages:
    726
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    190
    #6
    This worked..

    $titles = array(
        'aboutus' => 'About Us',
        'beaches' => 'Beaches',
        // ...
    );
     
    $file = basename($_SERVER['REQUEST_URI'], '.html');
     
    if (isset($titles[$file]))
    {
        echo $titles[$file];
    }
    else
    {
        echo 'Home';
    }
    
    PHP:
    Thanks alot..
     
    hasbehas, Mar 16, 2013 IP
  7. hasbehas

    hasbehas Well-Known Member

    Messages:
    726
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    190
    #7
    One last question though..
    If I wanted to make multiple selections say
    /guide/
    /guide/aboutus.html
    to
    Guide > About us

    $titles = array(
        'aboutus' => 'About Us',
        'guide' => 'Guide',
        'beaches' => 'Beaches',
        'cafes' => 'Cafes',
        'my-cafe' => 'My Cafe',
        // ...
    );
     
    $file = basename($_SERVER['REQUEST_URI'], '.html');
     
    if (isset($titles[$file]))
    {
        echo $titles[$file];
    }
    else
    {
        echo 'Home';
    }
    
    PHP:
    Guide > About us
    Cafes
    Cafes > My Cafe

    is it possible ?
     
    hasbehas, Mar 16, 2013 IP