473,738 Members | 10,068 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Extending variable scope? (Related post at alt.php.mysql)

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 misunderstandin g $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($ar ray)){ //2 if $array is indeed an array
foreach($array as $key=>$value){ //3 foreach key/value pair
within the array
if(is_array($va lue)){ //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($pa rent)){ //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

Nov 3 '06 #1
5 2274
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 misunderstandin g $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($ar ray)){ //2 if $array is indeed an array
foreach($array as $key=>$value){ //3 foreach key/value pair
within the array
if(is_array($va lue)){ //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($pa rent)){ //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.
js*******@attgl obal.net
=============== ===
Nov 4 '06 #2

Jerry Stuckle wrote:
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 misunderstandin g $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($ar ray)){ //2 if $array is indeed an array
foreach($array as $key=>$value){ //3 foreach key/value pair
within the array
if(is_array($va lue)){ //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($pa rent)){ //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.
js*******@attgl obal.net
=============== ===
Thanks Jerry - but I'm still a long way off

Here's a snippet from the array:
Note: This is one of 4 children of a parent task which has ID = 11

[3] =>
Array ( [name] =TASK
[attrs] =>
Array ( [ID] =14
[NAME] =Task 2.4
[EXPAND] =true )
[children] =>
Array ( [0] =>
Array ( [name] =NOTES
[attrs] =Array ( )
[tagData] =Embedded devices, etc ) ) )

The statements echoed by the function look something like this:

INSERT INTO TASK (name) VALUES ('TASK') ON DUPLICATE KEY UPDATE
INSERT INTO (ID) VALUES ('14') ON DUPLICATE KEY UPDATE
INSERT INTO (ID) VALUES ('14') WHERE ID = 14
INSERT INTO (NAME) VALUES ('Task 2.4') ON DUPLICATE KEY UPDATE
INSERT INTO (ID) VALUES ('14') WHERE ID = 14
INSERT INTO (EXPAND) VALUES ('true') ON DUPLICATE KEY UPDATE
INSERT INTO (ID) VALUES ('14') WHERE ID = 14
INSERT INTO NOTES (name) VALUES ('NOTES') ON DUPLICATE KEY UPDATE
INSERT INTO NOTES (tagData) VALUES ('Embedded devices, etc') ON
DUPLICATE KEY UPDATE

and here's how I suppose it should look

INSERT INTO TASK (ID,NAME,EXPAND ) VALUES ('14','Task 2.4','true') ON
DUPLICATE KEY UPDATE
INSERT INTO TASK (PARENT) VALUES ('11') WHERE ID = '14'

Even if we ignore the NOTES bit for the now (which I guess should be a
field within the TASK table) you can see there's a lot wrong with my
function!

So, I can see that to do it this way I'll need to sort out my logic,
and also implode the keys and values, something like:

foreach ($array as $key =$value)
$array[$key] = "'".str_replace ("'", "\'",
stripslashes($v alue))."'";
$values = implode(", ", $value);

Again, any more nudges in the right direction greatly appreciated.

---
For continuity, here's the current incarnation of the function:

function array_to_mysql( $array) { //1 define the function
$table = null; //2 define a persistent variable
if(is_array($ar ray)){ //3 if $array is indeed an array
foreach($array as $key=>$value){ //4 foreach key/value pair within
the array
if(is_array($va lue)){ //5 either the current value is
itself an array
array_to_mysql( $value); //6 (so call this function again
to process that array);
} else { //7 or it isn't
if($key == 'name'){ //8 (in which case, if the key
paired with that value = 'name'
$table = $value; //9 then assign that value to $table)
} elseif //10 or else
($key == 'ID'){ //11 (if the key paired with that
value = 'ID'
$parent = $value;
$condition = "WHERE ID = $value";} //12 then assign that
value to $parent

echo "INSERT INTO $table ($key) VALUES ('$value') ON DUPLICATE KEY
UPDATE <BR>\n";
if(!is_null($pa rent)){ //14 Finally, if $parent is
assigned issue the following
echo "INSERT INTO $table (ID) VALUES ('$parent') $condition<BR>\ n";
}
}
}
}
}

Nov 4 '06 #3
strawberry wrote:
Jerry Stuckle wrote:
>>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 misunderstandin g $variable persistence. I posted a
similar enquiry over at alt.php.mysql, but I guess this is a more
appropriat e 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($ar ray)){ //2 if $array is indeed an array
foreach($array as $key=>$value){ //3 foreach key/value pair
within the array
if(is_array($va lue)){ //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($pa rent)){ //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.
js*******@att global.net
============= =====


Thanks Jerry - but I'm still a long way off

Here's a snippet from the array:
Note: This is one of 4 children of a parent task which has ID = 11

[3] =>
Array ( [name] =TASK
[attrs] =>
Array ( [ID] =14
[NAME] =Task 2.4
[EXPAND] =true )
[children] =>
Array ( [0] =>
Array ( [name] =NOTES
[attrs] =Array ( )
[tagData] =Embedded devices, etc ) ) )

The statements echoed by the function look something like this:

INSERT INTO TASK (name) VALUES ('TASK') ON DUPLICATE KEY UPDATE
INSERT INTO (ID) VALUES ('14') ON DUPLICATE KEY UPDATE
INSERT INTO (ID) VALUES ('14') WHERE ID = 14
INSERT INTO (NAME) VALUES ('Task 2.4') ON DUPLICATE KEY UPDATE
INSERT INTO (ID) VALUES ('14') WHERE ID = 14
INSERT INTO (EXPAND) VALUES ('true') ON DUPLICATE KEY UPDATE
INSERT INTO (ID) VALUES ('14') WHERE ID = 14
INSERT INTO NOTES (name) VALUES ('NOTES') ON DUPLICATE KEY UPDATE
INSERT INTO NOTES (tagData) VALUES ('Embedded devices, etc') ON
DUPLICATE KEY UPDATE

and here's how I suppose it should look

INSERT INTO TASK (ID,NAME,EXPAND ) VALUES ('14','Task 2.4','true') ON
DUPLICATE KEY UPDATE
INSERT INTO TASK (PARENT) VALUES ('11') WHERE ID = '14'

Even if we ignore the NOTES bit for the now (which I guess should be a
field within the TASK table) you can see there's a lot wrong with my
function!

So, I can see that to do it this way I'll need to sort out my logic,
and also implode the keys and values, something like:

foreach ($array as $key =$value)
$array[$key] = "'".str_replace ("'", "\'",
stripslashes($v alue))."'";
$values = implode(", ", $value);

Again, any more nudges in the right direction greatly appreciated.

---
For continuity, here's the current incarnation of the function:

function array_to_mysql( $array) { //1 define the function
$table = null; //2 define a persistent variable
if(is_array($ar ray)){ //3 if $array is indeed an array
foreach($array as $key=>$value){ //4 foreach key/value pair within
the array
if(is_array($va lue)){ //5 either the current value is
itself an array
array_to_mysql( $value); //6 (so call this function again
to process that array);
} else { //7 or it isn't
if($key == 'name'){ //8 (in which case, if the key
paired with that value = 'name'
$table = $value; //9 then assign that value to $table)
} elseif //10 or else
($key == 'ID'){ //11 (if the key paired with that
value = 'ID'
$parent = $value;
$condition = "WHERE ID = $value";} //12 then assign that
value to $parent

echo "INSERT INTO $table ($key) VALUES ('$value') ON DUPLICATE KEY
UPDATE <BR>\n";
if(!is_null($pa rent)){ //14 Finally, if $parent is
assigned issue the following
echo "INSERT INTO $table (ID) VALUES ('$parent') $condition<BR>\ n";
}
}
}
}
}
Hi, Strawberry,

Sorry, it took a while for me to get back to you. I wanted to have some
time to think about this.

Yes, I can see where you're getting confused. It's not an easy job to
solve, but it can be done.

First of all, since you're using the same ID in both cases, you only
need one insert statement:

INSERT INTO TASK (ID,NAME,EXPAND ,PARENT)
VALUES ('14','Task 2.4','true', 11)
ON DUPLICATE KEY UPDATE

If you don't have a parent, you could use 0 or null (depending on your
database schema) for PARENT.

Alternatively, you should be using and UPDATE for the second one:

INSERT INTO TASK (ID,NAME,EXPAND )
VALUES ('14','Task 2.4','true')
ON DUPLICATE KEY UPDATE
UPDATE TASK SET PARENT=11 WHERE ID = '14'

But your problem is a lot more difficult because of the way you have
your arrays. I think you would be a lot better off not trying to use a
recursive function here because your children are of different types.

Recursion works much better if the children are of the same type. For
instance, parts to a car:

Car =Array
Engine =Array
Piston
Crankshaft
Camshaft
Valve
Body =Array
Door
Hood
Bumper

And so on.

As it is, you have different types as your children. This setup is
going to be much more difficult to process. You'll probably be better
off processing each type of array separately, i.e. the TASK function
would look for attributes and children. The attributes function would
process attributes such as NAME, ID and expand, while the children
function would handle notes, attributes (calling the attributes function
above) and tagdata.

You may still have some recursions - that's fine. But you'll be
separating the various types of arrays into different functions, making
them easier to handle.

Returning values to the caller could be handled with references.

Another way would be to go to an object oriented method, with each type
of array being a class. Put the array elements into class variables and
when you're done you can process the class to create your SQL
statements. If you're familiar with OO techniques, I think this would
be the better way to go.

But this is going to be a bit complicated because of the different data
types you have.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Nov 6 '06 #4

Jerry Stuckle wrote:
strawberry wrote:
Jerry Stuckle wrote:
>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 misunderstandin g $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($ar ray)){ //2 if $array is indeed an array
foreach($array as $key=>$value){ //3 foreach key/value pair
within the array
if(is_array($va lue)){ //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($pa rent)){ //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.
js*******@attg lobal.net
============== ====

Thanks Jerry - but I'm still a long way off

Here's a snippet from the array:
Note: This is one of 4 children of a parent task which has ID = 11

[3] =>
Array ( [name] =TASK
[attrs] =>
Array ( [ID] =14
[NAME] =Task 2.4
[EXPAND] =true )
[children] =>
Array ( [0] =>
Array ( [name] =NOTES
[attrs] =Array ( )
[tagData] =Embedded devices, etc ) ) )

The statements echoed by the function look something like this:

INSERT INTO TASK (name) VALUES ('TASK') ON DUPLICATE KEY UPDATE
INSERT INTO (ID) VALUES ('14') ON DUPLICATE KEY UPDATE
INSERT INTO (ID) VALUES ('14') WHERE ID = 14
INSERT INTO (NAME) VALUES ('Task 2.4') ON DUPLICATE KEY UPDATE
INSERT INTO (ID) VALUES ('14') WHERE ID = 14
INSERT INTO (EXPAND) VALUES ('true') ON DUPLICATE KEY UPDATE
INSERT INTO (ID) VALUES ('14') WHERE ID = 14
INSERT INTO NOTES (name) VALUES ('NOTES') ON DUPLICATE KEY UPDATE
INSERT INTO NOTES (tagData) VALUES ('Embedded devices, etc') ON
DUPLICATE KEY UPDATE

and here's how I suppose it should look

INSERT INTO TASK (ID,NAME,EXPAND ) VALUES ('14','Task 2.4','true') ON
DUPLICATE KEY UPDATE
INSERT INTO TASK (PARENT) VALUES ('11') WHERE ID = '14'

Even if we ignore the NOTES bit for the now (which I guess should be a
field within the TASK table) you can see there's a lot wrong with my
function!

So, I can see that to do it this way I'll need to sort out my logic,
and also implode the keys and values, something like:

foreach ($array as $key =$value)
$array[$key] = "'".str_replace ("'", "\'",
stripslashes($v alue))."'";
$values = implode(", ", $value);

Again, any more nudges in the right direction greatly appreciated.

---
For continuity, here's the current incarnation of the function:

function array_to_mysql( $array) { //1 define the function
$table = null; //2 define a persistent variable
if(is_array($ar ray)){ //3 if $array is indeed an array
foreach($array as $key=>$value){ //4 foreach key/value pair within
the array
if(is_array($va lue)){ //5 either the current value is
itself an array
array_to_mysql( $value); //6 (so call this function again
to process that array);
} else { //7 or it isn't
if($key == 'name'){ //8 (in which case, if the key
paired with that value = 'name'
$table = $value; //9 then assign that value to $table)
} elseif //10 or else
($key == 'ID'){ //11 (if the key paired with that
value = 'ID'
$parent = $value;
$condition = "WHERE ID = $value";} //12 then assign that
value to $parent

echo "INSERT INTO $table ($key) VALUES ('$value') ON DUPLICATE KEY
UPDATE <BR>\n";
if(!is_null($pa rent)){ //14 Finally, if $parent is
assigned issue the following
echo "INSERT INTO $table (ID) VALUES ('$parent') $condition<BR>\ n";
}
}
}
}
}

Hi, Strawberry,

Sorry, it took a while for me to get back to you. I wanted to have some
time to think about this.

Yes, I can see where you're getting confused. It's not an easy job to
solve, but it can be done.

First of all, since you're using the same ID in both cases, you only
need one insert statement:

INSERT INTO TASK (ID,NAME,EXPAND ,PARENT)
VALUES ('14','Task 2.4','true', 11)
ON DUPLICATE KEY UPDATE

If you don't have a parent, you could use 0 or null (depending on your
database schema) for PARENT.

Alternatively, you should be using and UPDATE for the second one:

INSERT INTO TASK (ID,NAME,EXPAND )
VALUES ('14','Task 2.4','true')
ON DUPLICATE KEY UPDATE
UPDATE TASK SET PARENT=11 WHERE ID = '14'

But your problem is a lot more difficult because of the way you have
your arrays. I think you would be a lot better off not trying to use a
recursive function here because your children are of different types.

Recursion works much better if the children are of the same type. For
instance, parts to a car:

Car =Array
Engine =Array
Piston
Crankshaft
Camshaft
Valve
Body =Array
Door
Hood
Bumper

And so on.

As it is, you have different types as your children. This setup is
going to be much more difficult to process. You'll probably be better
off processing each type of array separately, i.e. the TASK function
would look for attributes and children. The attributes function would
process attributes such as NAME, ID and expand, while the children
function would handle notes, attributes (calling the attributes function
above) and tagdata.

You may still have some recursions - that's fine. But you'll be
separating the various types of arrays into different functions, making
them easier to handle.

Returning values to the caller could be handled with references.

Another way would be to go to an object oriented method, with each type
of array being a class. Put the array elements into class variables and
when you're done you can process the class to create your SQL
statements. If you're familiar with OO techniques, I think this would
be the better way to go.

But this is going to be a bit complicated because of the different data
types you have.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Thanks Jerry, plenty of food for thought.

I'm reluctant to take an OO approach simply because my experience with
programming generally, and this method in particular, is so limited.

I know that the structure of the array is somewhat inconsistent.
Unfortunately, that's not an aspect I have much control over, but
that's why I took an approach that 'simply' said 'for every occurrence
of the key 'name', let its value be that of a table and let every
subsequent key be a field within that table - until, of course, you
find another occurrence of the key 'name'. Oh, and record the fact that
nested arrays are children of the parent array.'

Although in reality I'm really only interested in a small part of the
whole array, I thought it would be simpler to have a script that
processed the whole thing with ambivalence. The XML from which the
array is generated is still in development. There may, in due course,
be more objects added to it, some of which I too would be interested in
including in my db. This way, I'd only have to add the missing tables -
I could even code for the creation of a table if one was missing.

As I mentioned, an earlier version of this function was posted over at
alt.php.mysql where Paul's been giving me some useful pointers. I know
that you both frequent both NGs so I didn't worry about it, but perhaps
I should consider closing one or other of the threads and consolidating
everything in one place - probably here, as the problem is more
specifically a PHP one rather than MySQL.

Nov 7 '06 #5

strawberry wrote:
Jerry Stuckle wrote:
strawberry wrote:
Jerry Stuckle wrote:
>
>>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 misunderstandin g $variable persistence. I posted a
>>>similar enquiry over at alt.php.mysql, but I guess this is a more
>>>appropriat e 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($ar ray)){ //2 if $array is indeed an array
>> foreach($array as $key=>$value){ //3 foreach key/value pair
>>>within the array
>> if(is_array($va lue)){ //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($pa rent)){ //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.
>>js*******@att global.net
>>============= =====
>
>
Thanks Jerry - but I'm still a long way off
>
Here's a snippet from the array:
Note: This is one of 4 children of a parent task which has ID = 11
>
[3] =>
Array ( [name] =TASK
[attrs] =>
Array ( [ID] =14
[NAME] =Task 2.4
[EXPAND] =true )
[children] =>
Array ( [0] =>
Array ( [name] =NOTES
[attrs] =Array ( )
[tagData] =Embedded devices, etc ) ) )
>
The statements echoed by the function look something like this:
>
INSERT INTO TASK (name) VALUES ('TASK') ON DUPLICATE KEY UPDATE
INSERT INTO (ID) VALUES ('14') ON DUPLICATE KEY UPDATE
INSERT INTO (ID) VALUES ('14') WHERE ID = 14
INSERT INTO (NAME) VALUES ('Task 2.4') ON DUPLICATE KEY UPDATE
INSERT INTO (ID) VALUES ('14') WHERE ID = 14
INSERT INTO (EXPAND) VALUES ('true') ON DUPLICATE KEY UPDATE
INSERT INTO (ID) VALUES ('14') WHERE ID = 14
INSERT INTO NOTES (name) VALUES ('NOTES') ON DUPLICATE KEY UPDATE
INSERT INTO NOTES (tagData) VALUES ('Embedded devices, etc') ON
DUPLICATE KEY UPDATE
>
and here's how I suppose it should look
>
INSERT INTO TASK (ID,NAME,EXPAND ) VALUES ('14','Task 2.4','true') ON
DUPLICATE KEY UPDATE
INSERT INTO TASK (PARENT) VALUES ('11') WHERE ID = '14'
>
Even if we ignore the NOTES bit for the now (which I guess should be a
field within the TASK table) you can see there's a lot wrong with my
function!
>
So, I can see that to do it this way I'll need to sort out my logic,
and also implode the keys and values, something like:
>
foreach ($array as $key =$value)
$array[$key] = "'".str_replace ("'", "\'",
stripslashes($v alue))."'";
$values = implode(", ", $value);
>
Again, any more nudges in the right direction greatly appreciated.
>
---
For continuity, here's the current incarnation of the function:
>
function array_to_mysql( $array) { //1 define the function
$table = null; //2 define a persistent variable
if(is_array($ar ray)){ //3 if $array is indeed an array
foreach($array as $key=>$value){ //4 foreach key/value pair within
the array
if(is_array($va lue)){ //5 either the current value is
itself an array
array_to_mysql( $value); //6 (so call this function again
to process that array);
} else { //7 or it isn't
if($key == 'name'){ //8 (in which case, if the key
paired with that value = 'name'
$table = $value; //9 then assign that value to $table)
} elseif //10 or else
($key == 'ID'){ //11 (if the key paired with that
value = 'ID'
$parent = $value;
$condition = "WHERE ID = $value";} //12 then assign that
value to $parent
>
echo "INSERT INTO $table ($key) VALUES ('$value') ON DUPLICATE KEY
UPDATE <BR>\n";
if(!is_null($pa rent)){ //14 Finally, if $parent is
assigned issue the following
echo "INSERT INTO $table (ID) VALUES ('$parent') $condition<BR>\ n";
}
}
}
}
}
>
Hi, Strawberry,

Sorry, it took a while for me to get back to you. I wanted to have some
time to think about this.

Yes, I can see where you're getting confused. It's not an easy job to
solve, but it can be done.

First of all, since you're using the same ID in both cases, you only
need one insert statement:

INSERT INTO TASK (ID,NAME,EXPAND ,PARENT)
VALUES ('14','Task 2.4','true', 11)
ON DUPLICATE KEY UPDATE

If you don't have a parent, you could use 0 or null (depending on your
database schema) for PARENT.

Alternatively, you should be using and UPDATE for the second one:

INSERT INTO TASK (ID,NAME,EXPAND )
VALUES ('14','Task 2.4','true')
ON DUPLICATE KEY UPDATE
UPDATE TASK SET PARENT=11 WHERE ID = '14'

But your problem is a lot more difficult because of the way you have
your arrays. I think you would be a lot better off not trying to use a
recursive function here because your children are of different types.

Recursion works much better if the children are of the same type. For
instance, parts to a car:

Car =Array
Engine =Array
Piston
Crankshaft
Camshaft
Valve
Body =Array
Door
Hood
Bumper

And so on.

As it is, you have different types as your children. This setup is
going to be much more difficult to process. You'll probably be better
off processing each type of array separately, i.e. the TASK function
would look for attributes and children. The attributes function would
process attributes such as NAME, ID and expand, while the children
function would handle notes, attributes (calling the attributes function
above) and tagdata.

You may still have some recursions - that's fine. But you'll be
separating the various types of arrays into different functions, making
them easier to handle.

Returning values to the caller could be handled with references.

Another way would be to go to an object oriented method, with each type
of array being a class. Put the array elements into class variables and
when you're done you can process the class to create your SQL
statements. If you're familiar with OO techniques, I think this would
be the better way to go.

But this is going to be a bit complicated because of the different data
types you have.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===

Thanks Jerry, plenty of food for thought.

I'm reluctant to take an OO approach simply because my experience with
programming generally, and this method in particular, is so limited.

I know that the structure of the array is somewhat inconsistent.
Unfortunately, that's not an aspect I have much control over, but
that's why I took an approach that 'simply' said 'for every occurrence
of the key 'name', let its value be that of a table and let every
subsequent key be a field within that table - until, of course, you
find another occurrence of the key 'name'. Oh, and record the fact that
nested arrays are children of the parent array.'

Although in reality I'm really only interested in a small part of the
whole array, I thought it would be simpler to have a script that
processed the whole thing with ambivalence. The XML from which the
array is generated is still in development. There may, in due course,
be more objects added to it, some of which I too would be interested in
including in my db. This way, I'd only have to add the missing tables -
I could even code for the creation of a table if one was missing.

As I mentioned, an earlier version of this function was posted over at
alt.php.mysql where Paul's been giving me some useful pointers. I know
that you both frequent both NGs so I didn't worry about it, but perhaps
I should consider closing one or other of the threads and consolidating
everything in one place - probably here, as the problem is more
specifically a PHP one rather than MySQL.
Well, I think this very nearly works...

function array_to_mysql( $array) { // define the function
static $table = null; // define a persistent
variable
static $parent = null; // and another
static $id = null; // and another
if(is_array($ar ray)){ // if $array is indeed an
array
foreach($array as $key=>$value){// foreach key/value pair
within the array
if(is_array($va lue)){ // either the current value
is itself an array
if ($key == 'children'){// (so see if it's a
child...
$parent = $id; // ...and if so, record the id of
its parent
} // and then
array_to_mysql( $value); // so call this function
again to process that array);
} else { // or it isn't
if($key == 'name') { // If $key = 'name' then its
associated
$table = $value; // value is the name of a table within
my db
} elseif // otherwise
($key == 'ID'){ // If $key = 'ID' then its associated
value
$id = $value; // is a primary key (and, possibly, a
parent) within my db
if (!is_null($pare nt)){
echo "INSERT INTO $table (ID,PARENT) VALUES
('$id','$parent ') UPDATE ON DUPLICATE KEY<BR>\n";
}
} else {
echo "INSERT INTO $table (ID,$key) VALUES
('$id','$value' ) UPDATE ON DUPLICATE KEY<BR>\n";

}
}
}
}
}

it doesn't implode anything so I've got to issue a separate INSERT
statement each time and there's a flaw whereby it inappropriately
assigns a value of '1' to the $parent variable so creating a whole
bunch of meaningless INSERT statements but, having come this far, this
must simple enough to straighten out :-) I'm going to replace the
echoes with real statements now and see what happens...

Jerry & Paul, thanks for all your help.

Nov 8 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

75
3875
by: David MacQuigg | last post by:
Seems like we need a simple way to extend Python syntax that doesn't break existing syntax or clash with any other syntax in Python, is easy to type, easy to read, and is clearly distinct from the "base" syntax. Seems like we could put the @ symbol to good use in these situations. Examples: print @(separator = None) x, y, z @x,y:x*x+y*y -- anonymous function
4
2788
by: Alicia Haumann | last post by:
I accidentally sent this to webmaster@python.org, so this could be a duplicate if "webmaster" forwards it to this list. :{ Hi, there. Thanks for any help that can be offered. I've been working with Python for a year or more now, but only doing simple extending in C/C++. I'm now attempting some embedding and several questions have come to mind. BTW - I'm running Windows 2000 with Python23 and VisualC++ developers
134
7889
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that means that if I misspell a variable name, my program will mysteriously fail to work with no error message. If you don't declare variables, you can inadvertently re-use an variable used in an enclosing context when you don't intend to, or
166
8642
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
4
14904
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is strongly name which contains the class where the static variable is defined. This library can be referenced by multiple projects. I am fairly sure the static variable does not survive across the application boundry but does it within the application...
13
2571
by: eman1000 | last post by:
I was recently looking at the prototype library (http://prototype.conio.net/) and I noticed the author used the following syntax: Object.extend(MyObj.prototype, { my_meth1: function(){}, my_meth2: function(){} }); to define new methods on the MyObj prototype object. Object.extend
6
14218
by: Jody Gelowitz | last post by:
I have run into an issue with variable scope within an XSLT document that is translated in VS.NET 2.0. Under VS.NET 1.1 (XslTransform), this code works fine. However, when using VS.NET 2.0 (XslCompiledTransform), the exact same XSLT transformation fails with the error: System.Xml.Xsl.XslLoadException: The variable or parameter 'lastrecordwaskit' was duplicated within the same scope. An error occurred at (174,12). at...
112
5457
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions that may print some messages. foo(...) { if (!silent)
11
2926
by: whirlwindkevin | last post by:
I saw a program source code in which a variable is defined in a header file and that header file is included in 2 different C files.When i compile and link the files no error is being thrown.How is this possible.I thought it will throw "Variable redefinition Error". Giving the source code for reference.Please help me out on this... a.h ----- int g; void fun(void);
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.