Jump to content

Archived

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

FrostiE

Parent fetching

Recommended Posts

Ok, got another problem, should be my last :-D.

Anyways, what im wanting to do, if find all parents of a single entry, basically, for a bread crumb. Now I want them seperated by a "," sign or something like that, so I can explode them into a array at a later stage when I fetch them from the database.

The code here is similar to the code I was helped with earlier, but just a little changed

[code]function listparents ($id) {
    include("db.inc");
         $res = mysql_query ("SELECT id, parent FROM proj
                              WHERE id = '$parent'
                              ORDER BY id");
         while (list($id, $parent) = mysql_fetch_row($res)) {
         $parents = $parents.",".$id;
         listparents($parent);
                        
         }
        
}[/code]

if I say use
[code]listparents ($row['id']);
[/code]

and add a echo line to the bottom of the listparents function, it give me exactly what I want, exept with a , at the beginning. The only problem is a dont know how to get the final result being say : 15, 19, 32, 45 back to the original function, to add it to the database.

Any help is really appreciated!

Share this post


Link to post
Share on other sites
No need to store derivable data in the db. You can get the breadcrumbs with

[code]include 'db.inc.php';

function breadcrumbs ($id, &$p) {

         $res = mysql_query ("SELECT id, name, parent FROM proj
                              WHERE id = '$id'");
         list($id, $name, $parent) = mysql_fetch_row($res);
         array_unshift ($p, $name);
         if ($parent > 0) breadcrumbs($parent, $p);

}

$id = 10;
$parents = array();
breadcrumbs (10, $parents);

echo join (' » ', $parents);[/code]

(BTW, name your include files as ".php" so they can't be browsed.)

Share this post


Link to post
Share on other sites

×

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.