Wednesday 15 September 2010

breadcrumbs - Creating a "guide bar" like this, above a web page by php+mysql? -


I want to create a "guide bar" (I do not know its exact name). I have a table named cat that contains the following data:

 catid catname parent 1 news faucet 2 game 1 health 3 4 tennis 2 5 football 2 6 soccer 5 

My page gets: $ catid = 6 , and I want to create:

football>

First get your data from the database and create an array

  $ q = mysql_query ("select" Catid, cat's name, cat to parents "); $ Pages = array (); ($ R = mysql_fetch_assoc ($ q)) {$ pages [$ r ['catid']] = Arrays ('catname' = & gt; $ r ['catname'], 'parent' => $ r [ ' parent ID']); }  

Use a recursive function to create breadcrumbs:

  function build_breadcrumbs ($ pages, $ catid) {if (not_null ($ catid) || Free ($ catid) return; Return build_breadcrumbs ($ pages, $ pages [$ catid] ['guardian']). $ Pages [$ catid] ['Cat's name']. '& Gt; '; }  

The function returns the breadcrumb of the parent + breadcrumbs current. Use the function like this:

  $ catid = 6; $ Breadcrumb = trim (build_breadcrumb ($ page, $ catered), '& gt;'); Echoes breadcrumbs;  

Or, if you prefer more arrays, then you can create a function like this:

  function build_breadcrumbs ($ pages, $ catid ) {If (is_null ($ catid)} empty ($ cated) returns return (); Returns array_majores (build_broadcrambs ($ page, $ page [$ catid] ['parent']), array ($ page [$ catid] ['name'])); }  

and use it with implode:

  $ catid = 6; $ Breadcrumbs = intestine ('' gt; ', build_breadcrumbs ($ pages, $ catid)); Echoes breadcrumbs;  

This gives you the option to define the separator outside the function, hope that helps.


No comments:

Post a Comment