Joe H
Joe H

Reputation: 827

php passing function argument value to select other string and give that a value

I've been racking my brains trying to think of a way round this. I have a header file which is called in every document on my site, and I need to dynamically select from each document which anchor is given a set value. as follows:

function headur($page) {echo'...
    <ul>
      <li><a'.$h.' href="/">Home</a>
      <li><a'.$s.' href="/services">Services</a>
      <li><a'.$p.' href="/portfolio">Portfolio</a>
      <li><a'.$a.' href="/about">About</a>
      <li><a'.$c.' href="/contact">Contact</a>
    </ul>...
  ;}

then in my document I call headur($a); and from that argument value - $a - use that as the selector to give the matching internal string a specific value - class="selected" i.e. this particular function call would output the html like this:

    <ul>
      <li><a href="/">Home</a>
      <li><a href="/services">Services</a>
      <li><a href="/portfolio">Portfolio</a>
      <li><a class="selected" href="/about">About</a>
      <li><a href="/contact">Contact</a>
    </ul>

The whole point is that it's being dynamically selected from the document that calls the function, without me having to manually create a big old if statement to match everything up. i.e. if ($page == $a) {$a = ' class="selected"';} for each one, which is what I have been doing. I guess it's the php equivalent of doing .addClass(); but on an external file.

Thanks in advance.

Upvotes: 0

Views: 334

Answers (4)

mineichen
mineichen

Reputation: 510

A solution could be to load your template into a DOMDocument and set the Link as active which matches the current URL:

$dom = new DOMDocument();
$dom->loadXML('<ul><li><a href="/yourLink">Your Link</a></li></ul>');

$xpath = new DOMXPath($dom);
$url = $_SERVER['REQUEST_URI'];

foreach ($xpath->query(sprintf('//a[@href="%s"]', $uri)) as $current) {
    $current->setAttribute('class', 'selected');
} 

echo $dom->saveXML();

Code is not tested! But it should give you an idea... This way you can write your template-File without any PHP-Code at all in it...

Upvotes: 0

slashingweapon
slashingweapon

Reputation: 11317

It is very typical to create an array, and iterate through the array:

$linkMenu = array(
    '/' => 'Home',
    '/services' => 'Services',
    '/portfolio' => 'Portfolio',
    '/about' => 'About',
    '/contact' => 'Contact',
);

function linkList($links, $page=null) {
    echo '<ul>';
    foreach($links as $url=>$name) {
        $selected = ($url == $page) ? " class='selected' " : '';
        echo "<li><a $selected href='$url'>$name</a></li>\n";
    }
    echo '</ul>';
}

Now, you can create the link menu for the home page with:

linkList($linkMenu, '/');

Upvotes: 0

DanL
DanL

Reputation: 11

Just store your class definitions in an array and search it by the index. Using numeric keys for the array would be simplest, but not very descriptive while reading back over your code. To use numeric keys just do something like this:

$pages = array("", "services", "portfolio", "about", "contact");
$where = array_search($a, $pages); // $a is the param passed to the function
$pages[$where] .= "\" class=\"selected\"";

I did your pages in order, so $h would be replaced by $pages[0], $s would be replaced by $pages[1] and so on. If you want me to write you some code so you can use textual keys in the array to make it easier, I can do that as well, but I personally think this works fine without the extra code.

Upvotes: 1

Mark Steudel
Mark Steudel

Reputation: 1682

Well you could do something like this:

<ul>
    <li><a class="<?php echo $_SERVER['PHP_SELF'] == '/' ? 'selected' : '' ?>" href="/">Home</a>
    <li><a class="<?php echo $_SERVER['PHP_SELF'] == '/services' ? 'selected' : '' ?>" href="/services">Services</a>
    <li><a class="<?php echo $_SERVER['PHP_SELF'] == '/portfolio' ? 'selected' : '' ?>" href="/portfolio">Portfolio</a>
    <li><a class="<?php echo $_SERVER['PHP_SELF'] == '/about' ? 'selected' : '' ?>"href="/about">About</a>
    <li><a class="<?php echo $_SERVER['PHP_SELF'] == '/contact' ? 'selected' : '' ?>" href="/contact">Contact</a>
</ul>

Upvotes: 0

Related Questions