Jump to content


Parent fetching

  • Please log in to reply
1 reply to this topic

#1 FrostiE

  • Members
  • PipPip
  • Member
  • 18 posts

Posted 11 March 2006 - 08:10 PM

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

function listparents ($id) {
         $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;

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

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!

#2 Barand

  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 11 March 2006 - 08:49 PM

No need to store derivable data in the db. You can get the breadcrumbs with

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);

(BTW, name your include files as ".php" so they can't be browsed.)
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received


|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users