cvs: php4 /pear/DB common.php
| From: | Stig Bakken | Date: | Sat, 29 Dec 2001 06:36:57 +0000 |
| Subject: | cvs: php4 /pear/DB common.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-1749@lists.php.net to get a copy of this message | ||
ssb Sat Dec 29 01:36:57 2001 EDT
Modified files:
/php4/pear/DB common.php
Log:
* added grouping parameter to getAssoc
Index: php4/pear/DB/common.php
diff -u php4/pear/DB/common.php:1.75 php4/pear/DB/common.php:1.76
--- php4/pear/DB/common.php:1.75 Tue Dec 25 03:49:27 2001
+++ php4/pear/DB/common.php Sat Dec 29 01:36:57 2001
@@ -17,7 +17,7 @@
// | |
// +----------------------------------------------------------------------+
//
-// $Id: common.php,v 1.75 2001/12/25 08:49:27 ssb Exp $
+// $Id: common.php,v 1.76 2001/12/29 06:36:57 ssb Exp $
//
// Base class for DB implementations.
//
@@ -866,18 +866,34 @@
* '1' => 'one',
* '2' => 'two',
* '3' => 'three',
- * )
+ * )
*
* ...while the call getAssoc('SELECT id,text,date FROM mytable') returns:
* array(
* '1' => array('one', '944679408'),
* '2' => array('two', '944679408'),
* '3' => array('three', '944679408')
- * )
+ * )
+ *
+ * If the more than one row occurs with the same value in the
+ * first column, the last row overwrites all previous ones by
+ * default. Use the $group parameter if you don't want to
+ * overwrite like this. Example:
+ *
+ * getAssoc('SELECT category,id,name FROM mytable', false, null,
+ DB_FETCHMODE_ASSOC, true) returns:
+ * array(
+ * '1' => array(array('id' => '4', 'name'
=> 'number four'),
+ * array('id' => '6', 'name' =>
'number six')
+ * ),
+ * '9' => array(array('id' => '4', 'name'
=> 'number four'),
+ * array('id' => '6', 'name' =>
'number six')
+ * )
+ * )
*
* Keep in mind that database functions in PHP usually return string
* values for results regardless of the database's internal type.
-
+ *
* @param string $query the SQL query
*
* @param boolean $force_array used only when the query returns
@@ -887,13 +903,20 @@
* @param array $params array if supplied, prepare/execute will be used
* with this array as execute parameters
*
+ * @param boolean $group if true, the values of the returned array
+ * is wrapped in another array. If the same
+ * key value (in the first column) repeats
+ * itself, the values will be appended to
+ * this array instead of overwriting the
+ * existing values.
+ *
* @access public
*
* @return array associative array with results from the query.
*/
function &getAssoc($query, $force_array = false, $params = array(),
- $fetchmode = DB_FETCHMODE_ORDERED)
+ $fetchmode = DB_FETCHMODE_ORDERED, $group = false)
{
settype($params, "array");
if (sizeof($params) > 0) {
@@ -928,14 +951,22 @@
reset($row);
$key = current($row);
unset($row[key($row)]);
- $results[$key] = $row;
+ if ($group) {
+ $results[$key][] = $row;
+ } else {
+ $results[$key] = $row;
+ }
}
} else {
while (is_array($row = $res->fetchRow(DB_FETCHMODE_ORDERED))) {
- reset($row);
- // we copy the row of data into a new array
- // to get indices running from 0 again
- $results[$row[0]] = array_slice($row, 1);
+ // we shift away the first element to get
+ // indices running from 0 again
+ $key = array_shift($row);
+ if ($group) {
+ $results[$key][] = $row;
+ } else {
+ $results[$key] = $row;
+ }
}
}
if (DB::isError($row)) {
@@ -944,7 +975,11 @@
} else {
// return scalar values
while (is_array($row = $res->fetchRow(DB_FETCHMODE_ORDERED))) {
- $results[$row[0]] = $row[1];
+ if ($group) {
+ $results[$row[0]][] = $row[1];
+ } else {
+ $results[$row[0]] = $row[1];
+ }
}
if (DB::isError($row)) {
$results = $row;