|
Posted by Jerry Stuckle on 11/04/06 04:30
strawberry wrote:
> In the function below, I'd like to extend the scope of the $table
> variable such that, once assigned it would become available to other
> parts of the function. I thought 'global $table;' would solve this but
> it's clear that I'm misunderstanding $variable persistence. I posted a
> similar enquiry over at alt.php.mysql, but I guess this is a more
> appropriate forum because the problems I'm having relate to PHP.
>
> Any help appreciated.
>
> $function array_to_mysql($array) { //1 define the function
> if(is_array($array)){ //2 if $array is indeed an array
> foreach($array as $key=>$value){ //3 foreach key/value pair
> within the array
> if(is_array($value)){ //4 either the current value
> is itself an array
> array_to_mysql($value); //5 (so call this function again
> to process that array)
> } else { //6 or it isn't
> if($key == 'name'){ //7 (in which case, if the key
> paired with that value = 'name'
> $table = $value; //8 then assign that value to
> $table)
> } elseif //9 or else
> ($key == 'ID'){ //10 (if the key paired with
> that value = 'ID'
> $parent = $value;}else{ //11 then assign that value to
> $parent
> echo "INSERT INTO $table ($key) VALUES ('$value') ON DUPLICATE KEY
> UPDATE <BR>\n";}
> if(!is_null($parent)){ //13 Finally, if $parent is
> assigned issue the following
> echo "INSERT INTO $table (ID) VALUES ('$parent') ON DUPLICATE KEY
> UPDATE<BR>\n";
> }
> }
> }
> }
> }
>
> Apologies if the formatting gets stuffed up in the posting. TIA
>
Just define $table at the start of our function, i.e.
function array_to_mysql($array) {
$table = null;
....
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|