Reply to Re: [PHP] Problem with array

Your name:

Reply:


Posted by Richard Lynch on 05/01/05 22:04

On Sun, May 1, 2005 10:21 am, Murray @ PlanetThoughtful said:
> <?
>
> 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;
>
> }

The $arrtree in the function above has *NOTHING* to do with the $arrtree
in the function below.

Not connected at all, really.

> function buildProjectTree($contid, $level=1){
>
> if ($contid<>''){
>
> global $arrtree;

//Start with an empty tree every time this function is called:
$arrtree = array();

> $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?

There is *NOTHING* in the code given here that makes it "right" to have
$arrtree be a GLOBAL variable in your function.

That means you are probably doing that wrong too.

Try it with just the line I added, and no "global $arrtree;" line.

--
Like Music?
http://l-i-e.com/artists.htm

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация