|
Posted by Brent Baisley on 10/04/39 11:05
Ahhhh, I was wondering why your variable was named insert.
Anyway, that's easy too.
foreach($insert as $table => $fields) {
$fieldList = array_keys($fields);
$fieldNames = implode(',', $fieldList);
$fieldValues = '"'.implode('","', $fields).'"';
echo 'insert into '.$table.' ('.$fieldNames.') values
('.$fieldValues.')';
}
That puts double quotes around the values. I always use single quotes
around my text pieces since PHP doesn't parse what's inside them, thus
it's supposed to be faster.
On Jan 11, 2005, at 10:42 AM, Benjamin Edwards wrote:
> Ok - looking at what you did I cant see any diference apart from
> turning a serious updated into a single update. This is usefull if I
> want to do an update but I actualy want an insert (the update was just
> used for this example).
>
> I actualy want to do an insert so i'me thinking the following would
> work:-
>
> foreach ($insert as $table => $fields) {
> foreach($fields as $field => $value) {
> $columns[] = $field;
> $values[] = "'".$value."'";
> }
>
> echo
> "insert into $table ( ".implode( ', ', $columns ).
> " values ( ".implode( ', ', $values )." ) ";
> unset($columns);
> unset($values);
> }
>
> Ben
>
> >>> Brent Baisley <brent@landover.com> 01/11/05 03:02pm >>>
> You've created a 2x2 array, not a 1x4, which might be confusing you:
> tab2 ["fields1"] ["fields5"]
> tab1 ["fields2"] ["fields7"]
>
> You almost have it, you just need to set your loop up to handle
> unlimited field/value pairs in your array.
>
> One option is to structure your loop like this:
> foreach ($insert as $table => $fields) {
> foreach($fields as $field => $value) {
> $fieldValuePairs[] = "$field = $value";
> }
> $fieldValueStr = implode(',', $fieldValuePairs);
> echo "update $table set $fieldValueStr";
> unset($fieldValuePairs);
> }
>
> On Jan 11, 2005, at 6:22 AM, Benjamin Edwards wrote:
>
> > If I create the following 2 dimensional associative array:-
> >
> > $insert["tab2"]["fields1"] = "value1";
> > $insert["tab1"]["fields2"] = "value2";
> > $insert["tab2"]["fields5"] = "value3";
> > $insert["tab1"]["fields7"] = "value4";
> >
> > how do I do 2 levels of nested loop with the first level looping
> > through the first level of the array and the second nesting through
> > the second. something like:-
> >
> > foreach( $insert as $table => $fields ) {
> > foreatch( $fields as $field => $value ) {
> > echo "update $table set $field = $value";
> > }
> > }
> >
> > Regards,
> > Ben
> >
> >
> >
> >
> >
> >
> --
> Brent Baisley
> Systems Architect
> Landover Associates, Inc.
> Search & Advisory Services for Advanced Technology Environments
> p: 212.759.6400/800.759.0577
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email
> ______________________________________________________________________
>
>
> ______________________________________________________________________
> This email and any files transmitted with it are confidential. It is
> for the intended recipient only. If you have received the email in
> error please notify the author by replying to this email. If you are
> not the intended recipient, you must not disclose, distribute, copy,
> print, or rely on this email. Any views expressed by an individual
> within this email which do not constitute or record professional
> advice relating to the RNLI, do not necessarily reflect the views of
> the organisation.
>
> Registered Charity Number 209603
>
> <image.tiff>
>
> <image.tiff>
--
Brent Baisley
Systems Architect
Landover Associates, Inc.
Search & Advisory Services for Advanced Technology Environments
p: 212.759.6400/800.759.0577
Navigation:
[Reply to this message]
|