serious problems with recursive routines

I’m not sure since when the following does not work any more. It used to work.

Situation: I have an organisation structure and I want to generate a security try on it. I look at a record and look who is the owner. Then I go a level deeper etc. This way I get a structure like this:

01 main
0101 dept 1
010101 subdept 1
0102 dept 2
010201 subdept 2a
010202 subdept 2b

etc.

First problem:

I got an error about a non existing property on the field $herkomst on the line:

function maketree($oeh, $orgcode, $herkomst)

I solved it by renaming it to

function maketree($oeh, $orgcode, $herkomstcd)

There is a field called herkomst on the form so I guess this is causing problems now.

Second problem
The variable is not used in the sql statement:


		$orgcode = $rs->fields[0];
		echo 'orgcode 1'.$orgcode;
		$TheOeh = $oeh . GetOeh($cnt); 
		echo 'orgcode 2'.$orgcode;
		$update_sql = "UPDATE organogram set stree='".$TheOeh."' where orgcode='".$orgcode."' and herkomst='".$herkomstcd."'";
		echo 'orgcode 3'.$orgcode;
                sc_exec_sql($update_sql);
 	       echo 'orgcode 4'.$orgcode;

The trace shows that $orgcode has the correct value, BUT the trace on the executed SQL shows ANOTHER VALUE, the previous one.
I guess that there’s some internal pointer failure. I solved it by re-assigning another variable (???) So this works:


		$orgcode = $rs->fields[0];
		echo 'orgcode 1'.$orgcode;
		$TheOeh = $oeh . GetOeh($cnt); 
		echo 'orgcode 2'.$orgcode;
		$orgcodecd = $orgcode;
		$update_sql = "UPDATE organogram set stree='".$TheOeh."' where orgcode='".$orgcodecd."' and herkomst='".$herkomstcd."'";
		echo 'orgcode 3'.$orgcode;
                sc_exec_sql($update_sql);
	        echo 'orgcode 4'.$orgcode;
 

Third problem
Now the third problem occurs:
If I use a select in a recursive routine then the rs variable is NO LONGER LOCAL
This means that my script is only running on the first organisation and then quits:


function maketree($oeh, $orgcode, $herkomstcd)
{
// Update record
   static  $cnt, $TheOeh;
   echo $oeh;
   $check_sql = "SELECT orgcode from organogram where orgcodeboven='".$orgcode."' and herkomst='".$herkomstcd."' order by orgcode";
   sc_select(rs, $check_sql);
  
   if (false == {rs})     // Error while accessing database
   {
    sc_error_message('Error while accessing database.');
   }
   else 
   {
	$cnt=1;
    while(!$rs->EOF)
     {
		$orgcode = $rs->fields[0];
		echo 'orgcode 1'.$orgcode;
		$TheOeh = $oeh . GetOeh($cnt); 
		echo 'orgcode 2'.$orgcode;
		$orgcodecd = $orgcode;
		$update_sql = "UPDATE organogram set stree='".$TheOeh."' where orgcode='".$orgcodecd."' and herkomst='".$herkomstcd."'";
		echo 'orgcode 3'.$orgcode;
                sc_exec_sql($update_sql);
	        echo 'orgcode 4'.$orgcode;
 
  		maketree($TheOeh, $orgcodecd, $herkomstcd); 
		
		$cnt++;
		$rs->MoveNext();
     }
     $rs->Close();
   } 	
}

The maketree procedure is called recursive and overwrites the rs variable.

This used to work fine and something in one of the latest (?) versions has broken this code. Please correct this asap.

Ah, afterburner: I tried to add the $rs variable to the static list, but I get an error about reassinging of $this. But I made a typo first and typed $rv in the static list. *** The routine worked!!! *** Odd, but true, only after seeing the typo I corrected it. Got the errormessage. Reverted back … did not help, so I’m still stuck. This is very fishy.

And oh-help: I changed the header to function

maketree($oeh, $orgcodecd, $herkomstcd) and changed the var’s. THis is a great solution, all hangs and I need to restart Apache on the server. Mmmmmmmmm…

Hello,

Issue reported to our bugs team.

regards,
Bernhard Bernsmann

I got it working by changing the query by a fixed resultset. I also removed the static variables.

Hi Albert

Same problem for me using sc_exec_sql.
What you mean by “changing the query by a fixed resultset”?

There are two ways of getting a resultset, one as an array and one by row. I used the array variant. My result set is fortunately not that big.

Ok… in this case sc_lookup instead a sc_select from the main recordset and a while loop checking the array using isset({rs[index][0]})

Tkx

[QUOTE=gbravi;18917]Ok… in this case sc_lookup instead a sc_select from the main recordset and a while loop checking the array using isset({rs[index][0]})

Tkx[/QUOTE]

Yes, that’s what I did.