Hi All,

 

I have 2 function, 1 which calls another, that I am attempting to use in a
page with, currently, some problems.

 

In essence, I have a hierarchical recordset table called tbl_content (contid
and parid, where parid contains the contid of the record to which the
current record belongs). A record without a parid value indicates that the
record is a project level record. I have another table with messages that
have been attached to records in tbl_content, called tbl_content_messages.
Tbl_content_messages contains a contid field indicating the record from
tbl_content to which each message has been attached.

 

I'm trying to create a summary page at the project level (ie all records in
tbl_content that do not have a parid value) displaying the number of
messages in each project (ie, attached to any record in tbl_content that is
part of the hierarchy chain underneath the relevant project record).

 

To do this, for each project I'm trying to determine the list of child
contid values so that I can do a count(*) in tbl_content_messages using the
IN operator, so that only messages attached to records in that project's
hierarchy chain are counted.

 

To generate that child list in the summary page, I'm using:

 

<?

            $leaflist = listProjectChildren($d->contid); // $d->contid
contains the contid of the project being summarised

?>

 

The two functions I'm calling are:

 

<?

 

function listProjectChildren($contid, $list=''){

            if ($contid<>''){

                        $arrtree = array();

                        $arrtree = buildProjectTree($contid, $level=1);

                        for ($i=0;$i < count($arrtree);$i++){

            $list .= "'{$arrtree[$i][2]}',";

                         }

            }

    $list = substr($list, 0,-1);

    unset($arrtree);

            return $list;

}

 

function buildProjectTree($contid, $level=1){

            if ($contid<>''){

                        global $arrtree;

                        $sql = "SELECT contid, parid, title FROM tbl_content
WHERE parid='$contid'";

                        $rs = mysql_query($sql);

                        while ($d= mysql_fetch_object($rs)){

                                    $ind = count($arrtree);

 

                                    $arrtree[$ind][0] = $d->title;

                                    $arrtree[$ind][1] = $level;

                                    $arrtree[$ind][2] = $d->contid;

                                    $arrtree[$ind][3] = $d->parid;

                                    buildProjectTree($d->contid, $level+1);

 

                        }

                        mysql_free_result($rs);

            }

            return $arrtree;

}

?>

 

My problem is that when I'm iterating through all of the project level
records, it appears that the content of $arrtree is never reset. New entries
are simply placed on the end, causing the "for." loop that builds the
returned $list variable in listProjectChildren() to progressively return
larger and larger lists of contid values. As you can see, I try to UNSET()
the array prior to returning the $list variable, but this doesn't seem to
help.

 

Can anyone help me figure out what I'm doing wrong?

 

Many thanks in advance!

 

Murray

 

Reply via email to