[thelist] PHP & CSS (Two Part Question)

Steve Lewis slewis at macrovista.net
Wed May 1 19:25:01 CDT 2002

Burhan Khalid wrote:

> Have one array, with one element that holds the filename, and have it
> pointed to the navigation trail portion of it. Example :
>     $navigation = array("index.php" => "Home Page");
>         I would like to be able to access this array using
> $navigation[0], and be
> able to access both the index.php and the Home Page part of it. It is my

You have the right hunch.  'index.php' and '0' are BOTH valid keys (or
indices) in the array which point to the value you wanted of "Home Page"
and I am not sure that this will work for you, but my first suggestion
is to drop the numerical index if you can.  Iterate over the array using
the current($navigation) and next($navigation) functions and write out
your breadcrumb as you go.  You seem to indicate you need to have the
numerical key, but why?

You can iterate over the array--and have the numerical index(0), the
value ("Home Page"), and the key ("index.php") available to you--using
something like the following:

    // for each navigation element
    while(list($value,$key) = each($navigation));
        // insert whatever actions you need to do here
        echo "<a href='$key'>$value</a><br />";

If, for some reason you need to have both the numeric (implied) and
string (explicit) keys than use the array_keys function.  [ see also
http://www.php.net/manual/en/function.array-keys.php ]  This requires
that you are using PHP 4 and you had better be.

    // define the navigation
    $navigation = array ("index.php" => "Home Page", "contact.php" =>
"Contact Us");

    // build an array to better define our keys
    $keys = array_keys($navigation);

    // now for each navigation element
    while(list($num_key,$str_key) = each($keys));
        // insert whatever actions you need to do here
        echo "<a href='$str_key'>($num_key) $navigation[$num_key]</a><br

    Warning:  I have not written any PHP in probably 14 months; mea
culpa if this throws errors.

--Steve Lewis

More information about the thelist mailing list