Jump to content

[SOLVED] 2d array need help to get values


soulbro

Recommended Posts

Hi all, I hope someone can help as im stuck on a problem past 2 days now..

Using the tutorial on this site http://www.sitepoint.com/print/hierarchical-data-database/, I created a new column called 'parent' for each page in my site, and each page now has a 'parent'.

Using a recursive function, I access the database and create a two-dimensional array to find the path back to the root node of any particular page like so:

 

              function get_path($parent_column)

                      {

                      $sql = "select id, display_name, page_name, parent from page_admin where id ='$parent_column'";

                      $rs = mssql_query($sql);// or die(echo("Error on page retrieve_contents.php on line 125"));

 

                      $row = mssql_fetch_array($rs);

                      $path = array();

                     

                      if ($row['parent']!='')

                        {

                          //creating a 2 dimensional array

                              $path[] = array($row['parent'], $row['display_name'], $row['page_name'], $row['id']);

                              //$path = array($row['parent'],$row['display_name'],$row['page_name'],$row['id'];

                              $path = array_merge(get_path($row['parent']), $path);

                             

                            }

                          return $path;

                      }

 

Now I am stuck on how to extract the information needed from the array to create a breadcrumb trail. I want to create a kind of trail with links like : - Home -> About Us -> More Info etc etc

 

The array $path looks like

Array ( [0] => Array ( [0] => 0 [1] => Home [2] => index [3] => 1 ) [1] => Array ( [0] => 1 [1] => About us [2] => aboutus [3] => 2 ) )

 

But I can't understand how to pick out the individual items to create the breadcrumb trail. If anyone can help, would be really appreciated.

 

Link to comment
https://forums.phpfreaks.com/topic/139979-solved-2d-array-need-help-to-get-values/
Share on other sites

Sorry I gave the "more info" path as an example, although the code I supplied was for the About Us page, however your code works automatically for any page (you genius!!) Thank you premiso.

 

I edited slightly so it uses the 3rd column of the array to create links on the names like so:

 

            foreach ($array as $path) {

                          $bc[] = "<a href=".$path[2].".php>".$path[1]."</a>";

                      }

                      $bc = implode(" -> ", $bc);

                      echo $bc;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.