Re: [PHP] Unexpected values in an associative array

2007-08-08 Thread Ken Tozier


On Aug 8, 2007, at 12:43 AM, Robert Cummings wrote:


On Tue, 2007-08-07 at 23:28 -0500, Richard Lynch wrote:

On Tue, July 31, 2007 11:06 am, Instruct ICC wrote:

What is $value and what is this supposed to do:

case'integer':
$value  
+= 0;



This is a silly hack in place of:

$value = (int) $value;


And it's flawed since a string containing a float having 0 added to it
will still be float :)


Good points. Thanks.

Ken

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



RE: [PHP] Unexpected values in an associative array

2007-08-07 Thread Richard Lynch
On Tue, July 31, 2007 11:06 am, Instruct ICC wrote:
 What is $value and what is this supposed to do:
  case'integer':
  $value  
 += 0;


This is a silly hack in place of:

$value = (int) $value;

break;

  case'float':
  $value  
 += 0.0;

$value = (float) $value;

Adding 0 won't hurt an int, but adding 0.0 can only increase the error
margin of float.

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

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



RE: [PHP] Unexpected values in an associative array

2007-08-07 Thread Robert Cummings
On Tue, 2007-08-07 at 23:28 -0500, Richard Lynch wrote:
 On Tue, July 31, 2007 11:06 am, Instruct ICC wrote:
  What is $value and what is this supposed to do:
 case'integer':
 $value  
  += 0;
 
 
 This is a silly hack in place of:
 
 $value = (int) $value;

And it's flawed since a string containing a float having 0 added to it
will still be float :)

Cheers,
Rob.
-- 
...
SwarmBuy.com - http://www.swarmbuy.com

Leveraging the buying power of the masses!
...

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



Re: [PHP] Unexpected values in an associative array

2007-08-01 Thread Travis D
On 7/31/07, Ken Tozier [EMAIL PROTECTED] wrote:

 ...

   // set fetch prefs
 $this-db-setAttribute(PDO:: FETCH_ASSOC,
 true);   // also tried 1
  ...

Is that the way to do it?


Hmm.. Maybe I sent you in the wrong direction - I can't find any docs on
using setAttribute to set the fetch mode.  Anyway, setAttribute always works
like this:
setAttribute(attribute_to_set,value_to_set_to);

I was expecting something like:
setAttribute(PDO::FETCH_MODE, PDO::FETCH_ASSOC);

I don't think that works though, can't find anything in docs relating to
attribute called FETCH_MODE.  Anyway I dug in some code and this is what you
can do:

foreach ($pdo-query($query, PDO::FETCH_ASSOC)) {}

Query can take a second parameter.

Another option:
PDOStatement-setFetchMode(PDO::FETCH_ASSOC).. so your original code goes
from:

foreach ($pdo-query($query) as $row) {}

To:

$statement = $pdo-query($query);
$statement-setFetchMode(PDO::FETCH_ASSOC);
foreach( $statement as $row) {}

There must be a way to set it with setAttribute for the connection though,
instead of just on a per-statement basis...

Travis Doherty


[PHP] Unexpected values in an associative array

2007-07-31 Thread Ken Tozier

Hi

I think this is probably just a misunderstanding on my part, but I'm  
creating associative arrays with string keys from MySQL query results  
and when I put a value in the array, I get the expected key  
association along with an index key that has a different value.


For example: If I have a table Foo, do a select, perform coersions  
on the results, and place the coerced value in an array with a key, a  
second uncoerced value is also placed in the array with an index key.  
I know all associative arrays have both key and index accessors, but  
I would think that the values should be the same.


Here's the full function.
(Note: The line where values are added to the array is: $fields 
[$key] = $value; after the coersions switch statement)


function query_database($inQuery)
{
$query  = $inQuery;
$coersions  = null;
$object_key = null;
$group_by_key   = null;

if (is_array($inQuery))
{
$query  = $inQuery['query'];
$coersions  = $inQuery['coersions'];
$object_key = $inQuery['object_key'];
$group_by_key   = $inQuery['group_by_key'];
}

try
{
// determine query type
if (strpos($query, 'insert') === false)
{
$rows   = array();
$rowCounter = 0;

foreach ($this-db-query($query) as $row)
{
$fields = array();
$recordKey  = $rowCounter;

foreach ($row as $key = $value)
{
// remember this key if it matches the 
user specified object key
if (($object_key != null)  ($key == 
$object_key))
$recordKey  = 
$value;

// perform user specified coersions
if ($coersions != null)
{
switch ($coersions[$key])
{
case'integer':
$value  
+= 0;
break;

case'float':
$value  
+= 0.0;
break;

case'datetime':
$value  
= new date_object($value);
break;

case'base64_decode':
$value  
= base64_decode($value);
break;

case'hex_decode':
$value 
 = $this-hex_decode($value);
}
}

$fields[$key]   
= $value;
}

// perform grouping if requested
if ($group_by_key == null)
$rows[$recordKey]   = 
$fields;
else
{
$groupKey   = 
$fields[$group_by_key];

if ($rows[$groupKey] == null)
$rows[$groupKey]= 
array();

  

Re: [PHP] Unexpected values in an associative array[Solved]

2007-07-31 Thread Ken Tozier
Turns out that objects returned from SQL queries contain two parts  
for every field, one with a string key and one with an index key.  
Adding an is_numeric test on the keys allows you to filter out the  
numeric keys if you want to. For example:


foreach ($row as $key = $value)
{
if (!is_numeric($key))
{
/* do stuff here */
}
}

Ken


On Jul 31, 2007, at 10:35 AM, Ken Tozier wrote:


Hi

I think this is probably just a misunderstanding on my part, but  
I'm creating associative arrays with string keys from MySQL query  
results and when I put a value in the array, I get the expected key  
association along with an index key that has a different value.


For example: If I have a table Foo, do a select, perform  
coersions on the results, and place the coerced value in an array  
with a key, a second uncoerced value is also placed in the array  
with an index key. I know all associative arrays have both key and  
index accessors, but I would think that the values should be the same.


Here's the full function.
(Note: The line where values are added to the array is: $fields 
[$key] = $value; after the coersions switch statement)


function query_database($inQuery)
{
$query  = $inQuery;
$coersions  = null;
$object_key = null;
$group_by_key   = null;

if (is_array($inQuery))
{
$query  = $inQuery['query'];
$coersions  = $inQuery['coersions'];
$object_key = $inQuery['object_key'];
$group_by_key   = $inQuery['group_by_key'];
}

try
{
// determine query type
if (strpos($query, 'insert') === false)
{
$rows   = array();
$rowCounter = 0;

foreach ($this-db-query($query) as $row)
{
$fields = array();
$recordKey  = $rowCounter;

foreach ($row as $key = $value)
{
// remember this key if it matches the 
user specified object key
if (($object_key != null)  ($key == 
$object_key))
$recordKey  = 
$value;

// perform user specified coersions
if ($coersions != null)
{
switch ($coersions[$key])
{
case'integer':
$value  
+= 0;
break;

case'float':
$value  
+= 0.0;
break;

case'datetime':
$value  
= new date_object($value);
break;

case'base64_decode':
$value  
= base64_decode($value);
break;

case'hex_decode':
$value 
 = $this-hex_decode($value);
}
}

$fields[$key]   
= $value;
}

// perform grouping if requested
if ($group_by_key == null)
$rows[$recordKey]   = 

Re: [PHP] Unexpected values in an associative array

2007-07-31 Thread Jim Lucas

Ken Tozier wrote:

foreach ($this-db-query($query) as $row)


Well, this is what I was going to say, is that probably query is using *_fetch_array() instead of 
*_fetch_assoc() or *_fetch_row()



--
Jim Lucas

   Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them.

Twelfth Night, Act II, Scene V
by William Shakespeare

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



Re: [PHP] Unexpected values in an associative array[Solved]

2007-07-31 Thread Robin Vickery
On 31/07/07, Ken Tozier [EMAIL PROTECTED] wrote:
 Turns out that objects returned from SQL queries contain two parts
 for every field, one with a string key and one with an index key.
 Adding an is_numeric test on the keys allows you to filter out the
 numeric keys if you want to. For example:

Or don't get numeric keys in the first place:

foreach ($this-db-query($query, PDO::FETCH_ASSOC) as $row)

-robin

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



Re: [PHP] Unexpected values in an associative array[Solved]

2007-07-31 Thread Zoltán Németh
2007. 07. 31, kedd keltezéssel 11.29-kor Ken Tozier ezt írta:
 Turns out that objects returned from SQL queries contain two parts  
 for every field, one with a string key and one with an index key.  
 Adding an is_numeric test on the keys allows you to filter out the  
 numeric keys if you want to. For example:

thats the documented behaviour of mysql_fetch_array. but you do have
mysql_fetch_assoc and mysql_fetch_row at hand, which return an array
with one method of indexing only
see:
http://hu.php.net/mysql_fetch_array

I don't know what your $this-db-query($query) uses, but you could
look there...

greets
Zoltán Németh

 
 foreach ($row as $key = $value)
 {
   if (!is_numeric($key))
   {
   /* do stuff here */
   }
 }
 
 Ken
 
 
 On Jul 31, 2007, at 10:35 AM, Ken Tozier wrote:
 
  Hi
 
  I think this is probably just a misunderstanding on my part, but  
  I'm creating associative arrays with string keys from MySQL query  
  results and when I put a value in the array, I get the expected key  
  association along with an index key that has a different value.
 
  For example: If I have a table Foo, do a select, perform  
  coersions on the results, and place the coerced value in an array  
  with a key, a second uncoerced value is also placed in the array  
  with an index key. I know all associative arrays have both key and  
  index accessors, but I would think that the values should be the same.
 
  Here's the full function.
  (Note: The line where values are added to the array is: $fields 
  [$key] = $value; after the coersions switch statement)
 
  function query_database($inQuery)
  {
  $query  = $inQuery;
  $coersions  = null;
  $object_key = null;
  $group_by_key   = null;
  
  if (is_array($inQuery))
  {
  $query  = $inQuery['query'];
  $coersions  = $inQuery['coersions'];
  $object_key = $inQuery['object_key'];
  $group_by_key   = $inQuery['group_by_key'];
  }
  
  try
  {
  // determine query type
  if (strpos($query, 'insert') === false)
  {
  $rows   = array();
  $rowCounter = 0;
  
  foreach ($this-db-query($query) as $row)
  {
  $fields = array();
  $recordKey  = $rowCounter;
  
  foreach ($row as $key = $value)
  {
  // remember this key if it matches the 
  user specified object key
  if (($object_key != null)  ($key == 
  $object_key))
  $recordKey  = 
  $value;
  
  // perform user specified coersions
  if ($coersions != null)
  {
  switch ($coersions[$key])
  {
  case'integer':
  $value  
  += 0;
  break;
  
  case'float':
  $value  
  += 0.0;
  break;
  
  case'datetime':
  $value  
  = new date_object($value);
  break;
  
  case'base64_decode':
  $value  
  = base64_decode($value);
  break;
  
  case'hex_decode':
  $value  
  = $this-hex_decode($value);
  }
  }
  
  

Re: [PHP] Unexpected values in an associative array[Solved]

2007-07-31 Thread Ken Tozier


On Jul 31, 2007, at 11:40 AM, Robin Vickery wrote:


Or don't get numeric keys in the first place:

foreach ($this-db-query($query, PDO::FETCH_ASSOC) as $row)


Robin: Bingo! That did the trick.

I knew my solution was hokey but I haven't used PDO before this  
project so wasn't aware of what it did behind the scenes.


Thanks everyone for the quick replies.

Ken

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



RE: [PHP] Unexpected values in an associative array

2007-07-31 Thread Instruct ICC

From: Ken Tozier [EMAIL PROTECTED]
Hi

I think this is probably just a misunderstanding on my part, but I'm  
creating associative arrays with string keys from MySQL query results  and 
when I put a value in the array, I get the expected key  association along 
with an index key that has a different value.


And here's some sample output

array(6) {
  [task_id]=
  int(22)
  [0]=
  string(2) 22
  [container_id]=
  int(3784)
  [1]=
  string(4) 3784
  [name]=
  string(12) 108-6972.XTG
  [2]=
  string(24) 3130382D363937322E585447
}

Why is that? Is there any way to fix this without coercing the same  value 
twice? Once for the key accessor and once for the index accessor?


Thanks In advance

Ken


What is $coersions or $inQuery['coersions']?  Maybe a var_dump or a print_r 
on that for the list?

What is $value and what is this supposed to do:

case'integer':
$value  
+= 0;
break;

case'float':
$value  
+= 0.0;


If $value is a string, you should quote your 0 and 0.0.  What add 0 or 0.0 
to an int or float respectively?


Do a print_r on $row before your foreach ($row as $key = $value) and echo 
your $key and $value as the first line in after that for loop.  Then echo 
$coersions[$key].  Does $coersions[$key] have what you want for switch 
($coersions[$key]) at this point?  I assume you expect $coersions to contain 
datatypes, but does it?


Perhaps start at the end considering the structure of the output array you 
want, then consider how to populate it to have that structure.


What's the extra [] about in $rows[$groupKey][] = $fields;
Maybe that's where your duplicates are coming from although I don't see 
duplicates in your output.


_
http://liveearth.msn.com

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



Re: [PHP] Unexpected values in an associative array[Solved]

2007-07-31 Thread Robert Cummings
On Tue, 2007-07-31 at 11:29 -0400, Ken Tozier wrote:
 Turns out that objects returned from SQL queries contain two parts  
 for every field, one with a string key and one with an index key.  
 Adding an is_numeric test on the keys allows you to filter out the  
 numeric keys if you want to. For example:
 
 foreach ($row as $key = $value)
 {
   if (!is_numeric($key))
   {
   /* do stuff here */
   }
 }

BAH, use the correct function. You're using mysql_fetch_array() when you
SHOULD be using mysql_fetch_assoc().

Cheers,
Rob.
-- 
...
SwarmBuy.com - http://www.swarmbuy.com

Leveraging the buying power of the masses!
...

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



Re: [PHP] Unexpected values in an associative array

2007-07-31 Thread Ken Tozier


On Jul 31, 2007, at 12:06 PM, Instruct ICC wrote:


array(6) {
  [task_id]=
  int(22)
  [0]=
  string(2) 22
  [container_id]=
  int(3784)
  [1]=
  string(4) 3784
  [name]=
  string(12) 108-6972.XTG
  [2]=
  string(24) 3130382D363937322E585447
}

What is $coersions or $inQuery['coersions']?  Maybe a var_dump or a  
print_r on that for the list?


For the above record, $coersions is an associative array of the form

array('task_id'= 'integer', 'container_id'= 'integer',  
'name'='hex_decode');


I found that I was doing a lot of coersions with results of SQL  
queries to turn them into real integers, floats or decode from base64/ 
hex before I could use them so rolling all that ugly stuff into the  
query makes it so users of the function don't have to do coersions  
any more. There may be a better way to perform coersions from string  
types returned by PDO queries and their real types but I'm a PDO noob  
so don't know if one.



What is $value and what is this supposed to do:

case'integer':
$value  
+= 0;
break;

case'float':
$value  
+= 0.0;


If $value is a string, you should quote your 0 and 0.0.  What add 0  
or 0.0 to an int or float respectively?


The point of these is to convert the strings to ints, floats, decode  
them etc...


What's the extra [] about in $rows[$groupKey][] = $fields;
Maybe that's where your duplicates are coming from although I don't  
see duplicates in your output.


Another thing I found the need for was to group rows in a select  
result by specific keys. For example say a query gets all ads in a  
publication, and you want to physically group them by page. That's  
what that group function does.


Ken

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