You are here: Recursive function problem with variable « PHP Programming Language « IT news, forums, messages
Recursive function problem with variable

Posted by Alkimake on 08/04/06 11:54

here is the code that i am using for recursion to view the tree of my
database records.

recurs(48);
$i =0;
function recurs($ownerid) {
global $i;
$sqlOne="SELECT * FROM sub_cat WHERE oID=".$ownerid;
$queryOne = mysql_query($sqlOne);
$i++;
$k=0;
if (mysql_num_rows($queryOne)) {
do {
$sqlTwo="SELECT * FROM sub_cat WHERE
oID=".mysql_result($queryOne,$k,"sID");
$queryTwo = mysql_query($sqlTwo);
if (mysql_num_rows($queryTwo)) {
echo $i.".".mysql_result($queryOne,$k,"sName")."<br />";
recurs(mysql_result($queryOne,$k,"sID"));
} else {
echo $i.".".mysql_result($queryOne,$k,"sName")."<br />";
$i--;
return false;
}
$k++;
} while ($k = mysql_num_rows($queryOne));
} else {
return false;
}
}

this code works but with a little problem.

As you see i have a db table that uses normal id as "sID" and owner id
as "oID". One of records sID can be oID of the other records. It is so
easy if i can explain with my poor english :)

So the function starts to render and prints the first levels first
record. Than looks if the record has a child with $queryTwo. if it has
calls the function again with a new $ownerid.

when the control structure do...while finishes the process of first
record of first level, than it passes $k=1, so the second record. but
when it tries to process second record i can see $querOne is changed
and becomes the last level of the first record. (It is sooo hard to
tell :) ) so it exports something like this.
(l) = level
(r) = record

(l)0r(0) = OK
(l)1r(0) = OK // there is just one record
(l)2r(0) = OK // there is just one record too.
(l)1r(0) = FAIL // it says unable to jump row 1 of queryOne -> because
$queryOne is change.

As you see the main problem i can not use the query as a local or
private object in the function. Everytime i call the function $queryOne
changes globally.

Any suggestions,

If you have better idea to create tree view of my records than the
recursion, i am open to hear it. Thanks.

 

Navigation:

[Reply to this 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

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