Re: [PHP] 2 dimensional array processing

2005-01-11 Thread Brent Baisley
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


Re: [PHP] 2 dimensional array processing

2005-01-11 Thread Benjamin Edwards
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 [EMAIL PROTECTED] 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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] 2 dimensional array processing

2005-01-11 Thread Brent Baisley
A, 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 [EMAIL PROTECTED] 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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] 2 dimensional array processing

2005-01-11 Thread Richard Lynch
Brent Baisley wrote:
 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.

Which is not valid SQL, so won't work on most SQL engines.
[Though MySQL may be forgiving in this regard...]

$fieldValues = implode(', ', $fields);
would work on all standard SQL engines if ALL the fields were text (or
non-numeric).

Or, in MySQL, for integer/float as well, as MySQL lets you get away with
bogus (non-standard) apostrophes on numeric field values, so this will
work for MySQL even though it's morally wrong :-)

To make it really portable (standard SQL), you'd want to query your
database as to the type of each field and put apostrophes only around
non-numeric field values.

Or you could have some other way of keeping track of what's numeric and
what's not, of course.

 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.

It's not measurably faster.

Even apostrophes have *some* internal processing (for embedded ' and \) so
there is no real difference in speed.

Benchmark it on your own machine to confirm.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php