SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help me reduce down some conditional code

    This is my current code..

    Code:
    <?php 
    $this_page = basename($_SERVER['SCRIPT_NAME']);
    
    if ($this_page == 'home.php' || $this_page == 'sitemap.php') { ?>
    <?php } else { ?>
    <h1><?php page_title(); ?></h1>
    <?php } ?>
    How can I write this more concisely?

    Basically I'm saying:

    If the page we're on is home.php or sitemap.php then do nothing. Otherwise output the page title in a <h1> element.

    In an ideal world i'd be able to write

    Code:
    <?php 
    $this_page = basename($_SERVER['SCRIPT_NAME']);
    
    if ($this_page == 'home.php' || 'sitemap.php') { ?>
    <?php } else { ?>
    <h1><?php page_title(); ?></h1>
    <?php } ?>
    but that doesn't work.

    Anything I can do?

    Guy.

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    if(false === in_array(basename($_SERVER['SCRIPT_FILENAME']), array('home.php''sitemap.php')))
    {
        
    printf('<h1>%s</h1>'page_title());
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php $this_page basename($_SERVER['SCRIPT_NAME']); ?>
    <?php 
    if ($this_page == 'home.php' || $this_page == 'sitemap.php'): ?>

    <?php else: ?>
    <h1><?php page_title(); ?></h1>
    <?php endif; ?>
    It would look even better if you used MVC or something similar and assigned variables in the controller instead of templates:

    PHP Code:
    <?php if ($this->this_page == 'home.php' || $this->this_page == 'sitemap.php'): ?>

    <?php else: ?>
    <h1><?php page_title(); ?></h1>
    <?php endif; ?>

  4. #4
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks both!

    Anthony, your solution works right out of the box aside from printing the page name followed by the <h1> element.

    e.g.

    The page title<h1></h1>

    Why would it do that instead of

    <h1>The page title</h1>

    ?

    Risoknop, I will have to look into MVC a bit more. I'll admit I'm usually a front end designer. Just need a little bit of PHP here and there to achieve something. A decent framework would suit me down to the ground. A jQuery for PHP type thing. But I wouldn't know where to start.

    Thanks again guys.

  5. #5
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are many frameworks for PHP similar to jQuery for JavaScript: Symfony, Zend Framework, CakePHP, Kohana, Yii and probably more.

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I would guess page_title() function actually writes the title to the page itself (not good practice at all!).

    Unless the function supports an additional parameter, you could workaround it with...
    PHP Code:
    <?php
    if(false === in_array(basename($_SERVER['SCRIPT_FILENAME']), array('home.php''sitemap.php')))
    {
        
    ob_start();
        
    page_title();
        
    $page_title ob_get_contents();
        
    ob_end_clean();
        
    printf('<h1>%s</h1>'$page_title);
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @risknop -- thank's, i'll take a look.

    @anthony -- yes the function does write the title of the page to the page. It uses some XSLT to grab the title from an XML file which indexes all the pages in the site. I've no idea of any other way to write the page name there under these conditions unfortunately!

    Various other XSLT files create things like breadcrumb menus, sidebars, metadata and whatnot from the same XML file.

    Your workaround works perfectly. I just need to decode it all now to understand what it's doing!

  8. #8
    SitePoint Addict Beaumont's Avatar
    Join Date
    Mar 2005
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Speaking of concise code:

    PHP Code:

    // The following

    if(false === in_array(basename($_SERVER['SCRIPT_FILENAME']), array('home.php''sitemap.php')))
    {


    // Could instead be written

    if( ! in_array(basename($_SERVER['SCRIPT_FILENAME']), array('home.php''sitemap.php')))



Tags for this Thread

Bookmarks

Posting Permissions

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