Help making this function work with this array

I'm trying to modify this function so that it works with my array.

<code>
function breadcrumbs( $id, $pages )
            {
              $bcl = array( );
              $pageid = $id;
              while( strlen( $pageid ) > 0 )
              {
                  $bcl[] = $pageid;
                  $pageid = $pages[ $pageid ]['parent'];
              }
              for( $i = count( $bcl ) - 1; $i >= 0; $i-- )
              {
                  $page = $pages[$bcl[$i]];
                  if ( $i > 0 )
                  {
                        echo( "<a href=\"" );
                        echo( $page['url'] );
                        echo( "\">" );
                  }
                  echo( $page['title'] );
                  if ( $i > 0 )
                  {
                  echo( "</a> | " );
                  }
              }
         }
</code>

The code, as it is, is supposed to work with an array like:

$pages = array(
            home => array( id=>"home", parent=>"", title=>"Home",
                        url=>"showpage.php?id=home" ),
            users => array( id=>"users", parent=>"home", title=>"Users",
                        url=>"showpage.php?id=users" ),
            jack => array( id=>"jack", parent=>"users", title=>"Jack",
                        url=>"showpage.php?id=jack" )
            );

I want to setup my array like this:

$pagesets = array(
        // menu set #1
     'main' => array(
          'start' => array( pid=>"start", parent=>"", title=>"Home", url=>"showpage.php?id=install" ),
          'step2' => array( pid=>"step1", parent=>"start", title=>"Gettign Started", url=>"showpage.php?id=step2" ),
          'info' => array( pid=>"info", parent=>"", title=>"Information", url=>"showpage.php?id=info" ),
     ),
    'admin => array(
          'adminhome' => array( pid=>"adminhome", parent=>"", title=>"Control Panel", url=>"showpage.php?id=admin" ),
          'profile' => array( pid=>"profile", parent=>"adminhome", title=>"Manage My Profile", url=>"showpage.php?id=profile" ),
     )
     );

I've tried this, but it dosn't work.

<code - doesn't work>
function breadcrumbs( $id, $pages )
            {
              $bcl = array( );
              $pageid = $id;
              while( strlen( $pageid ) > 0 )
              {
                  $bcl[] = $pageid;
                  $pageid = $pages[ $pageid ]['parent'];
              }
              for( $i = count( $bcl ) - 1; $i >= 0; $i-- )
              {
                  $page = $pages[$bcl[$i]];
                  if ( $i > 0 )
                  {
                        $pg .= ( "<a href=\"" );
                        $pg .= ( $page['url'] );
                        $pg .= ( "\">" );
                  }
                  $pg .= ( $page['title'] );
                  if ( $i > 0 )
                  {
                  $pg .= ( "</a> | " );
                  }
              }
              return $pg;
         }
</code>

Thanks
savetheorcasAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

xberryCommented:
In your arrays you should set both, key and values, with quotation marks, supposed the items are strings, shouldn't you ?

sample:   "start" => array( "pid"=>"start", "parent"=>"", "title"=>"Home", "url"=>"showpage.php?id=install" ),

. . .

Also, you packed your array threedimensional but only call it two dimensional as far as I an see:

$pageid = $pages[ $pageid ]['parent'];  should be something like:

$pageid = $pages[$pageid]["start"]["parent"];

?
savetheorcasAuthor Commented:
Yes, I tried that. Actually, I always do this. I took them out because in the book where I got the example from, there were no quotes. Either way, though it's not working. Like I said, I modified the array a bit so I think the problem I have is with one of the foreach statements.
xberryCommented:
So, where is your foreach statement actually, I can't see it anywhere in your code.
ysreCommented:
Whatever happened to while (list ($key, $value) = each($arrayref)) ? ;)

In some esoteric case you'd have to reset ($arrayref); before doing the while loop to ensure you get to walk the whole array :)

Besides, wouldn't 'parent' be eg. "main" or "admin" for "start" or resp. "adminhome" ?
I don't quite get why you do multidimensional arrays for this.
Afterall it should be something like this:
$mymenu = array(
  // our fake top in the menu listing => notshown usually
  "top" => array ("parent" => "", "title" => "", "url" => "base.url"),

  "main" => array ("parent" => "top", "title" => "Some title", "url" => "someu.rl"),
  // my users in main
  "users" => array ("parent" => "main", "title" => "My users", "url" => "someotheru.rl"),
  "jack" => array ("parent" => "users", "title" => "My user jack", "url" => "jacksu.rl"),

  // now my prefs in main
  "preferences" => array ("parent" => "main", "title" => "Main preferences", "url" => "prefsu.rl"),
  "videoprefs" => array("parent" => "preferences", "title" => "main video preferences", "url" => "videoprefsu.rl"),

  // my login / register stuff
  "login" => array("parent" => "top", "title" => "Go login here", "url" => "login.url"),
  "registernewuser" => array ("parent" => "top", "title" => "register new!", "url" => "registerme.url")
);



Of course you'd have to change the breadcrumbs function for this:

function breadcrumbs( $start_id, $depth, $pages )  // recursive function
{
    $cur_layer = array();

    $html_pg = "";

    if ($depth > 10) return "";   // fail-safe bailout.. just to make sure we dont run forever..

    // find current layer of menu content
    if ((isset($pages[$start_id])) and ($start_id != ""))  // show entry except for the first nonshown top in our hierarchy
    {
       $cur_layer = $pages[$start_id];
       $depth_indicator = "";
       for ($i = 0; $i < $depth; $i++) { $depth_indicator .= "&nbsp;"; }
       $html_pg .= "$depth_indicator<a href=\"" .$cur_layer['url']. "\">" .$cur_layer['title']. "</a><br>\n";
    }

    // now walk the whole array to find out if there is stuff linked to us (aka childs)
    reset ($pages);
    while (list ($k, $v) = each ($pages))
    {
      if ($pages[$k]['parent'] == $start_id)
      {
         $html_pg .= breadcrumbs ($k, $depth+1, $pages);
      }
    }
   
   return ($html_pg);
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ysreCommented:
edit: You'd start this beast like   print breadcrumbs ("", 0, $mymenu);
Works beautifully with indentation here.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.