2

The following code shows a multi-dimensional array, and a function that loops through the array to echo out a nested menu with links.

However, the function doesn't appear to be working as nothing is being echoed.

It was working but I have changed a few includes around, so could it be something to do with that?

<?php

$urls = array (
    0=>array (
        0=>"Home",
        1=>"http://uni.michaelnorris.co.uk/",
        2=>"Home",
        3=>"",
        4=>"",
        5=>"1"
    ),

    1=>array (
        0=>"Blog",
        1=>"http://uni.michaelnorris.co.uk/blog/",
        2=>"Blog",
        3=>"",
        4=>"",
        5=>"1"
    ),

    2=>array (
        0=>"Glossary",
        1=>"http://uni.michaelnorris.co.uk/",
        2=>"Glossary",
        3=>"",
        4=>"",
        5=>"1"
    ),

    3=>array (
        0=>"Resources",
        1=>"http://uni.michaelnorris.co.uk/blog/",
        2=>"Resources",
        3=>"",
        4=>"",
        5=>"1"
    ),

    4=>array (
        0=>"Staff",
        1=>"http://uni.michaelnorris.co.uk/",
        2=>"Staff",
        3=>"",
        4=>"",
        5=>"1"
    ),

    5=>array (
        0=>"Blog",
        1=>"http://uni.michaelnorris.co.uk/blog/",
        2=>"Blog",
        3=>"",
        4=>"",
        5=>"1"
    ),

    6=>array (
        0=>"Home",
        1=>"http://uni.michaelnorris.co.uk/",
        2=>"Home",
        3=>"",
        4=>"",
        5=>"1"
    ),

    7=>array (
        0=>"Blog",
        1=>"http://uni.michaelnorris.co.uk/blog/",
        2=>"Blog",
        3=>"",
        4=>"",
        5=>"1"
    )       
);

function showMenu()
{
    $top = count($urls);
    echo "<ul>";
    for ($i=0;$i<$top;$i++) {
        echo "<li><a href='".$urls[$i][1]."' title='".$urls[$i][2]."'>".$urls[$i][0]."</a><li>";
    }
    echo "</ul>";
}

?>

<?php showMenu(); ?>
4
  • 3
    Are you getting an error? Is it not outputting correctly? My crystal ball is being repaired currently. I don't see anything inherently wrong with your code. Commented Oct 25, 2011 at 19:14
  • Wow.... shouldn't that be filtered? Only code, no text. Commented Oct 25, 2011 at 19:16
  • What do you expect to happen? What is happening now? Commented Oct 25, 2011 at 19:16
  • error_reporting(E_ALL); => Notice: Undefined variable: urls in x.php on line 81 Commented Oct 25, 2011 at 19:18

6 Answers 6

7

After taking a step back, I noticed your problem. Replace showMenu with this:

function showMenu()
{
    global $urls;
    $top = count($urls);
    echo "<ul>";
    for ($i=0;$i<$top;$i++) {
        echo "<li><a href='".$urls[$i][1]."' title='".$urls[$i][2]."'>".$urls[$i][0]."</a></li>";
    }
    echo "</ul>";
}

You had declared $urls outside the scope of the function. In the future, please tell us what's wrong with your code in the question, instead of making us guess.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. That has sorted it, can you please explain why this has fixed it?
@Mike: global $urls; will redeclare $urls in the local scope of the function, based on the existing global one.
Oh and fix your <li> to a </li> at the end of the echo line as well, credit to @KurtFunai for that one.
@djaqeel: Read my comment directly above yours. I'll edit the answer though to include this change. And he is calling showMenu in his code already, so I will not include that in my answer.
3

URLS is defined outside the scope of the function showMenu, so it doesn't know that function exists. You can use the global method to bring it in:

function showMenu() {
  global $urls;
  [...]
}

Or you can pass it as a variable:

$urls = array();

function showMenu($urls) {
  [...]
}

showMenu($urls);

Or you could just define $urls within the function itself.

Also, you might be interested to know that you don't have to explicitly number the array. You could more easily type it as:

$urls = array(
  array()
  ,array()
  ,array()
);

Comments

2

You are trying to access the $urls variable from inside the function, which is not possible. You must pass the array into the function or letting the function know that is a global variable. Here's a modified version of your code that will work:

function showMenu($urls)
{
    $top = count($urls);
    echo "<ul>";
    for ($i=0;$i<$top;$i++) {
        echo "<li><a href='".$urls[$i][1]."' title='".$urls[$i][2]."'>".$urls[$i][0]."</a></li>";
    }
    echo "</ul>";
}

?>

<?php showMenu($urls); ?>

I also fixed the missing /in the closing <li>

1 Comment

Thank you, I hadn't even noticed the missing /.
0

You are also missing the slash on your closing list element:

echo "<li><a href='".$urls[$i][1]."' title='".$urls[$i][2]."'>".$urls[$i][0]."</a><li>";

Should be:

echo "<li><a href='".$urls[$i][1]."' title='".$urls[$i][2]."'>".$urls[$i][0]."</a></li>";

Comments

0

You're declaring your array outside your function, so it will not have any scope within your function. Either declare the array as a global, or pass the array to the function. I would prefer the latter.

3 Comments

Why would you prefer the latter? Are there performance gains? Or is it just a personal thing, is there any benefit to parsing it to the function?
"Global variables in software are generally a bad idea. They serve to magically teleport information from one place in a program to another in a way that is very hard to follow. Thus they cause subtle bugs, make maintenance difficult, and kill kittens." -tjhunt.blogspot.com/2009/04/php-global-variables-are-not.html
Use them when they're needed and called for, but not just because it will help you do what you're trying to do when there is a more..accepted/standard way to do it. Coding your php functions like this will get you into a bad habit.
0

You forgot to put

global $urls;

at the top of your showMenu function.

function showMenu()
{ 
    global $urls;
    $top = count($urls);
    ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.