?
Solved

Help making this function work with this array

Posted on 2006-04-06
7
Medium Priority
?
198 Views
Last Modified: 2006-11-18
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
Comment
Question by:savetheorcas
  • 2
  • 2
5 Comments
 
LVL 14

Expert Comment

by:xberry
ID: 16398795
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
 

Author Comment

by:savetheorcas
ID: 16403452
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
 
LVL 14

Expert Comment

by:xberry
ID: 16406763
So, where is your foreach statement actually, I can't see it anywhere in your code.
0
 
LVL 4

Accepted Solution

by:
ysre earned 2000 total points
ID: 16409928
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
 
LVL 4

Expert Comment

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

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month16 days, 18 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question