• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

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
0
savetheorcas
Asked:
savetheorcas
  • 2
  • 2
1 Solution
 
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"];

?
0
 
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.
0
 
xberryCommented:
So, where is your foreach statement actually, I can't see it anywhere in your code.
0
 
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);
}

0
 
ysreCommented:
edit: You'd start this beast like   print breadcrumbs ("", 0, $mymenu);
Works beautifully with indentation here.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now