Grow your CSS skills. Land your dream job.

Show Specific Banners for Specific Pages

  • # April 11, 2012 at 8:06 am

    Hello guys!

    I am in a need of a little PHP script for a static website I’m making. I’m not using any CMS, just a few includes for the footer, header, menu etc.

    On every single page there is a featured image/banner, and I’d like to use a little snippet in those specific pages to include the image based in the current page.

    Here is how it’s outlined:
    For instance, I have three images. When the ‘about.php’ is the current page, I want to display image number one, and when ‘contact.php’ is the current page, I want to display image number two. The third image is for the rest of all the pages. I was wondering if it’s possible to say something like (I have absolutely no PHP experience)


    < ?php
    if ($currentpage == 'about.php') {
    print("")
    },
    if ($currentpage == 'contact.php') {
    print("")
    },
    else {
    print("")
    }
    ?>

    I don’t know how to get the current page thingy. I’m using the following script to highlight the current menu item, and I was wondering if it’s possible to just customize that little bunch of code so it’ll work with the images.


    < ?php
    function activestate($pagename, $currentpage) {
    if ($pagename == $currentpage) {
    echo "current";
    }
    }
    ?>


    Thanks in advance.

    Best regards,
    Kralle

    # April 11, 2012 at 1:43 pm

    basename($_SERVER)

    … should do what you’re looking for. I’m not really sure if this is safe (regarding SQL injections), but as you are using a static web page it won’t be a problem.

    # April 11, 2012 at 5:51 pm

    How would I implement

    basename($_SERVER)

    in the image-code – or even better, how would you write the final code? I really appreciate your help.

    Thanks in advance,
    Kralle

    # April 11, 2012 at 8:22 pm

    Dude.. Try this..


    < ?php
    $currentpage = substr($_SERVER["SCRIPT_NAME"], strrpos($_SERVER["SCRIPT_NAME"],"/")+1);

    if ($currentpage == 'page_1.php') {
    echo '';
    }
    elseif ($currentpage == 'page_2.php') {
    echo '';
    }
    elseif ($currentpage == 'page_3.php') {
    echo '';
    }
    else {
    echo '';
    }
    ?>
    # April 24, 2012 at 11:24 am

    You, sir, are a legend. Thanks a bunch man. It works perfectly!

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".