PHP If...Else Statements - beginner's question

Discussion in 'PHP' started by a-ha, Nov 20, 2008.

  1. #1
    Hi, I'm developing new website using drupal, and since I'm new to php, I'm having a problem which I believe is simple but can't find a solution.
    This is the code that gets site-name and places it between <h1></h1>:

    <?php if ($site_name) { ?><h1 class='site-name'><a href="<?php print $base_path ?>" title="<?php print t('Home') ?>"><?php print $site_name ?></a><?php } ?></h1>

    I don't want to have site-name in h1 so I replaced that h1 with div:

    <?php if ($site_name) { ?><div id="site-name"><a href="<?php print $base_path ?>" title="<?php print t('Home') ?>"><?php print $site_name ?></a><?php } ?></div>

    And it works great.
    But, I want site-name to be in h1 on my frontpage, like it was on beginning. So, I would like site-name in h1 if it is frontpage, else I would like it in div. I have both codes but don't know how to put them in conditional statement properly.
     
    a-ha, Nov 20, 2008 IP
  2. happpy

    happpy Well-Known Member

    Messages:
    926
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    120
    #2
    assuming your frontpage has the uri "/" this will work:

    <?
    if($_SERVER[REQUEST_URI]=="/") { if ($site_name) { ?><h1 class='site-name'><a href="<?php print $base_path ?>" title="<?php print t('Home') ?>"><?php print $site_name ?></a><?php } ?></h1> <? } else { if ($site_name) { ?><div id="site-name"><a href="<?php print $base_path ?>" title="<?php print t('Home') ?>"><?php print $site_name ?></a><?php 
    } ?></div><? } ?>
    Code (markup):
     
    happpy, Nov 20, 2008 IP
    Colbyt likes this.
  3. a-ha

    a-ha Peon

    Messages:
    120
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, exactly what I wanted. :)
     
    a-ha, Nov 20, 2008 IP