Page 1 of 1

Grabbing and matching filename from URI (using a function)

Posted: Sun Mar 15, 2009 11:59 am
by katebp
Hi, I'm new to PHP (previously used ASP, Coldfusion and JavaScript for work) and am having difficulty with the function below.

Code: Select all

<?php
            $urlsection = trim(basename($_SERVER["REQUEST_URI"],".php"));
            echo "[urlsection: " . trim($urlsection) . "]";
            
            function checksection($section) {
                echo "[section: " . $section . "]";
                if ($section == $urlsection) {
                    echo " class='lit'";
                }
            }
         ?>
            <ul>
                <li><a href="ourmission.php"><?php checksection("ourmission")?>Our Mission</a></li>
                <li><a href="contribute.php"><?php checksection("contribute")?>Contribute</a></li>
                <li><a href="projects.php"><?php checksection("projects")?>Projects</a></li>
                <li><a href="news.php"><?php checksection("ourmission")?>News</a></li>
                <li><a href="events.php">Events</a></li>
                <li><a href="contactus.php">Contact us</a></li>
            </ul>
At the moment the function is outside of the a tag, I will move this just as soon as I can work out why it's not working. I am outputting the urlsection and section variables just so I can check they're the same. They are. :(

My URL looks like: http://localhost/ashalondon/ourmission.php

All help gratefully received! Thanks, K

Re: Grabbing and matching filename from URI (using a function)

Posted: Mon Mar 16, 2009 12:29 am
by omniuni
Hi K,

Please clarify what you actually expect the function to DO. I took a guess...

Code: Select all

<?php
    
    function lightSection($thisSection){
        if($_GET['sec'] == $thisSection) echo 'class="lit"';
        }
 
?>
 
         <ul>
                <li><a href="?sec=ourmission" <?php lightSection("ourmission")?>>Our Mission</a></li>
                <li><a href="?sec=contribute" <?php lightSection("contribute")?>>Contribute</a></li>
                <li><a href="?sec=projects" <?php lightSection("projects")?>>Projects</a></li>
                <li><a href="?sec=news" <?php lightSection("news")?>>News</a></li>
                <li><a href="events.php">Events</a></li>
                <li><a href="contactus.php">Contact us</a></li>
            </ul>
Does that help, by chance?