PHP Conditional Statement Help

Discussion in 'PHP' started by TechMaster, Sep 9, 2009.

  1. #1
    Hello,

    I need help in PHP conditional statement. I am not PHP programmer. I have a site which is coded in PHP. Footer of the site is common for all the 200+ pages. To call footer in page below code has been used....
    
    <? 
    include('footer.php'); 
    ?>
    Code (markup):
    Now, what i want is different link on different pages. For example....

    if url is http://website.com/buy_short_jeans.php then link in footer should be a link like <a href="http://website.com/buy_short_jeans.php">Buy Short Jeans</a>

    What i have figured out is if-elseif can be used for that. See below code...

    <?php
    $url;
    
    if ($url == "http://website.com/buy_short_jeans.php") { 
    php coding that makes hyperlink
    }
    elseif ($url == "http://website.com/buy_short_skirts.php") {
    php coding that makes hyperlink
    } 
    
    else {
    hyperlink for main domain
    }
    ?>
    Code (markup):
    can you plz help me to finish doing that???
     
    TechMaster, Sep 9, 2009 IP
  2. kjb

    kjb Greenhorn

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #2
    Well firstly, that variable $url is doing nothing. If this were me, I'd use something unique to the page from the database to perform this - that way, you type the code in once and it works for every page like a set tag or even the url itself.

    But if I'm understanding you right,

    Put this:

    $url = http://website.com/buy_short_jeans.php

    in the body of your page and then:

    echo $url in the footer include.

    The variable is defined in the page and the footer will read the variable from the page when it's browsed.

    I haven't tested this, but in theory it should work.
     
    kjb, Sep 9, 2009 IP
  3. kjb

    kjb Greenhorn

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    Tested this and it works. I've also developed it a little for you:

    Page 1
    Page 2
    Footer

    Page 1:

    
    $url = $_SERVER['PHP_SELF'];
    $title = 'page 1';
    
    include('footer.php');
    
    Code (markup):
    Page 2:

    
    $url = $_SERVER['PHP_SELF'];
    $title = 'page 2';
    
    include('footer.php');
    
    Code (markup):
    Footer:

    echo '<a href="'.$url.'">'.$title.'</a>';
    Code (markup):
    If you browse Page 1, the link will read Page 1 and link to page 1, if on page 2 it will read page 2 and link to page 2.

    And mostly automated too. Enjoy.
     
    kjb, Sep 9, 2009 IP
  4. TechMaster

    TechMaster Peon

    Messages:
    225
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I did that in this way....

    <a href="<? $url = $_SERVER['SERVER_NAME'];
    
    $file = $_SERVER['REQUEST_URI'];
    
    echo "http://".$url.$file;
    
    ?>
    
     " title="
     
     <?
     if ( $url.$file == "www.website.com")
     {
     
     echo "Buy short jeans skirts from website.com";
     
     }
     elseif ( $url.$file == "www.website.com/buy_short_jeans.php") {
        echo "Buy short jeans online";
    }
    elseif ( $url.$file == "www.website.com/buy_short_skirts.php") {
        echo "Buy short skirts online";
    }
    
    else {
    echo "Buy jeans";
    }
     ?>
     
     ">
    
    <?
    
    if ($url.$file == "www.website.com/buy_short_jeans.php"){
    
    echo "Buy Short Jeans";
    }
     elseif ( $url.$file == "www.website.com/buy_short_skirts.php") {
        echo "Buy short skirts";
    }
    else {
    echo "Buy short";
    }
    ?>
    
    Code (markup):
     
    TechMaster, Sep 9, 2009 IP
  5. TechMaster

    TechMaster Peon

    Messages:
    225
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    i included the code in footer.php

    kjb: Thank for the help.


    Is there a way to fetch title of the page?
     
    Last edited: Sep 9, 2009
    TechMaster, Sep 9, 2009 IP
  6. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You could fetch the contents of the current page using file_get_contents(), and then do a regular experession to get the text inside the <title> tags.

    Or you could do something inside your header.php which just assigns the title to a variable $title or something, and then use it in your footer.
     
    wd_2k6, Sep 9, 2009 IP