RE: [PHP-DB] Calling functions recursively: recursive PHP functions
Thanks John. Your comment helped me through. I used a static variable for the array, array_merge_recursive() function to accumulate the foreign keys and $this->kgforeignkeys as a recursive function in a loop. This all works ok! function kgforeignkeys($tablename = "" ) { static $totalkgArr; if ( $this->connection && $tablename != "" ) { $keylist = ""; $sql = "SELECT conname, pg_catalog.pg_get_constraintdef(oid) as condef FROM pg_catalog.pg_constraint r WHERE r.conrelid = (SELECT c.oid FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE pg_catalog.pg_table_is_visible(c.oid) AND c.relname ~ '^" . $tablename . "$' ) AND r.contype = 'f'"; $keylist = pg_query ($this->connection, $sql); $num_rows = pg_num_rows($keylist); for ($i=0; $i < $num_rows; $i++) { $r = pg_fetch_row($keylist); // echo "Field: $r[0], $r[1] "; $phrase = split("\(|\)", $r[1]); // echo "Phrase: $phrase[0], $phrase[1], $phrase[2], $phrase[3], $phrase[4] "; $kgArr[$i][0][0] = $tablename; $word1 = split(",", $phrase[1]); for ($j=1; $j <= count($word1); $j++) { $kgArr[$i][0][$j] = trim($word1[$j - 1]); } $kgArr[$i][1][0] = trim(Substr($phrase[2], strrpos($phrase[2], " "))); $word2 = split(",", $phrase[3]); for ($j=1; $j <= count($word2); $j++) { $kgArr[$i][1][$j] = trim($word2[$j - 1]); } } pg_free_result ($keylist); $totalkgArr = array_merge_recursive($totalkgArr, $kgArr ); for ($h=0; $h < $num_rows; $h++) { $totalkgArr = $this->kgforeignkeys( $kgArr[$h][1][0] ); } return $totalkgArr; } else { echo 'Failed to obtain the foreign keys in ' . $tablename . ''; return $totalkgArr; } } On Mon, 2003-01-27 at 19:57, John W. Holmes wrote: > [snip] > > > function Test() > > { > > static $count = 0; > > > > $count++; > > echo $count; > > if ($count < 10) { > > Test (); > > } > > $count--; > > } > > ?> > > > > ** > > * > > Interesting but I am still lost as I need some help with > > methods/functions within objects: > > In the above example do I need to access Test() by: > > > > if ($count < 10) { > > this->Test (); > > Yes, without the space: $this->Test(); > > ---John W. Holmes... > > PHP Architect - A monthly magazine for PHP Professionals. Get your copy > today. http://www.phparch.com/ > > > -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Calling functions recursively
There are some cases where I see recursive functions as the only option. Picture this function to retrieve data from a global array: $data = array(array('somedata'=>'somevalue','somedata2'=>'somevalue2'),array('somedata'=>'somevalue','somedata2'=>'somevalue2'),array('somedata'=>'somevalue','somedata2'=>'somevalue2')); function getdata($key){ return $GLOBALS['data'][$key]; } But what if some entries in the array should be aliases of others? Using a recursive function, it's simple: $data = array(array('somedata'=>'somevalue','somedata2'=>'somevalue2'),'ALIAS0',array('somedata'=>'somevalue','somedata2'=>'somevalue2'),array('somedata'=>'somevalue','somedata2'=>'somevalue2')); function getdata($key){ if(!is_array($GLOBALS['data'][$key])){ preg_match('/^ALIAS([0-9]+)$/',$GLOBALS['data'][$key],$matches); return getdata($matches[1]); }else{ return $GLOBALS['data'][$key]; } } The array could be fetched inside of the if block, but that would make for longer code, harder to change code, and aliases of aliases would be impossible to have. Roberto Plomp wrote: If you mean that it would be more straight forward, hence neater and more comprehensive and more maintenance and stack friendly to just call the function in a loop, I agree. On the other hand ... well, there are arguments for the recursive approach for a number of algorithms. Personally I prefer not to use recursive functions, have never really seen the absolute necessity to do so. Roberto - Original Message - From: "Jason Wong" <[EMAIL PROTECTED]> Subject: Re: [PHP-DB] Calling functions recursively On Saturday 25 January 2003 14:49, Kevin Gordon wrote: Can functions / methods be called recursively? If so how can this be done? Comments much appreciated. google > "php recursive functions" -- Jason Wong -> Gremlins Associates -> www.gremlins.biz Open Source Software Systems Integrators * Web Design & Hosting * Internet & Intranet Applications Development * /* QOTD: "I've just learned about his illness. Let's hope it's nothing trivial." */ -- The above message is encrypted with double rot13 encoding. Any unauthorized attempt to decrypt it will be prosecuted to the full extent of the law.
RE: [PHP-DB] Calling functions recursively: recursive PHP functions
[snip] > function Test() > { > static $count = 0; > > $count++; > echo $count; > if ($count < 10) { > Test (); > } > $count--; > } > ?> > ** > * > Interesting but I am still lost as I need some help with > methods/functions within objects: > In the above example do I need to access Test() by: > > if ($count < 10) { > this->Test (); Yes, without the space: $this->Test(); ---John W. Holmes... PHP Architect - A monthly magazine for PHP Professionals. Get your copy today. http://www.phparch.com/ -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Calling functions recursively: recursive PHP functions
Roberto & Jason thank you for your comments. Please note the question at the end of this email. I searched on "recursive PHP functions" and found: [thelist] recursive PHP functions Rob Wilson thelist at lists.evolt.org Mon Sep 16 11:00:01 2002 * Previous message: [thelist] recursive PHP functions * Next message: [thelist] pop up window for image without MSIE image resize * Return to the message index sorted by: [ date ] [ thread ] [ subject ] [ author ] That should read 10) { return $n; } echo "N =" . $n . ""; $n++; return recursive ($n); } echo "The result:".recursive(1).""; ?> With the important line being the : return recursive ( $n ); This allows you to unwind the recursion correctly. HTH Rob * Under www.zend.com/manual chapter 8 "Variable scope" a mention of recursion: Static variables also provide one way to deal with recursive functions. A recursive function is one which calls itself. Care must be taken when writing a recursive function because it is possible to make it recurse indefinitely. You must make sure you have an adequate way of terminating the recursion. The following simple function recursively counts to 10, using the static variable $count to know when to stop: *** Interesting but I am still lost as I need some help with methods/functions within objects: In the above example do I need to access Test() by: if ($count < 10) { this->Test (); Comments please. Kevin Gordon -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Calling functions recursively
If you mean that it would be more straight forward, hence neater and more comprehensive and more maintenance and stack friendly to just call the function in a loop, I agree. On the other hand ... well, there are arguments for the recursive approach for a number of algorithms. Personally I prefer not to use recursive functions, have never really seen the absolute necessity to do so. Roberto - Original Message - From: "Jason Wong" <[EMAIL PROTECTED]> Subject: Re: [PHP-DB] Calling functions recursively > On Saturday 25 January 2003 14:49, Kevin Gordon wrote: > > Can functions / methods be called recursively? If so how can this be > > done? Comments much appreciated. > > google > "php recursive functions" > > -- > Jason Wong -> Gremlins Associates -> www.gremlins.biz > Open Source Software Systems Integrators > * Web Design & Hosting * Internet & Intranet Applications Development * > > > /* > QOTD: > "I've just learned about his illness. Let's hope it's nothing > trivial." > */ -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Calling functions recursively
On Saturday 25 January 2003 14:49, Kevin Gordon wrote: > Can functions / methods be called recursively? If so how can this be > done? Comments much appreciated. google > "php recursive functions" -- Jason Wong -> Gremlins Associates -> www.gremlins.biz Open Source Software Systems Integrators * Web Design & Hosting * Internet & Intranet Applications Development * /* QOTD: "I've just learned about his illness. Let's hope it's nothing trivial." */ -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Calling functions recursively
Kevin, Have fun and be sure to guarantee the prevent never ending looping. Roberto Counting to ten: $aCounter\n"; $aCounter++; RecursiveFunction($aCounter); } } ?> - Original Message - From: "Kevin Gordon" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Saturday, January 25, 2003 7:49 AM Subject: [PHP-DB] Calling functions recursively > Can functions / methods be called recursively? If so how can this be > done? Comments much appreciated. > Kevin Gordon > > > > > -- > PHP Database Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php