Page 1 of 1

Efficient loop through PEAR's $db->getAll($sql)

Posted: Mon Sep 08, 2003 12:34 pm
by sdibb
Okay, so I'm finally using PEAR's DB classes, and they are great, but I'm not sure the best way to loop through this info.

Using this:

Code: Select all

$array=$db->getAll($sql)
is a great alternative to

Code: Select all

$sql="SELECT blah";
$rs=mysql_query($sql);
$array=mysql_fetch_array($rs);
except, that the $db class dumps the info into a two-dimensional (or is it two layered?) array, which makes it hard to pull it out and loop through it.

So far, this is the best I've come up with:

Code: Select all

// Set arrays from the first array:
	for($i=0; $i<count($array); $i++) &#123;
		$array_content&#1111;$i]=$array&#1111;$i];
	&#125;

// And then do it a second time, for the info in that array:

	for($j=0; $j<count($array_content); $j++) &#123;
		echo $array_content&#1111;$j]&#1111;'id'];
	&#125;
There's got to be an easier way to pull info from get*() rather than just looping through the returned array twice. If not, I'll go back to my old while loop. Any suggestions?

Too bad the PEAR docs don't let you add notes. :T

PEAR Doc: http://pear.php.net/manual/en/package.d ... -fetch.php

Steve

Posted: Mon Sep 08, 2003 12:46 pm
by patrikG
I am using the following code in my db-wrapper

Code: Select all

<?php
function arrayComplete($SqlString=false,$resourceId=false)
//returns all relevant records in database in 
//a one-dimensional associative array.
	{
	$parent=$this->arrays($SqlString,$resourceId);
	while($children=$this->arrays($SqlString,$resourceId))
		{
		$parent=array_merge_recursive($parent,$children);
		}
	return $parent;
	}
?>
This basically grabs one array from the db, assigns it to $parent, then loops the SQL-query and uses array_merge_recursive to merge the arrays the way they should be. Returns a one-dimensional associative array.

You will need a db-array function. Mine looks this:

Code: Select all

<?php
function arrays($SqlString=false,$resourceId=false)
//simply returns the typical associative array you get with 
//mysql_fetch_array
 {
	if(!empty($SqlString))
		{
		$this->query($SqlString);
		$arrays = mysql_fetch_assoc($this->query_id);
	 	}
	elseif(!empty($resourceId))
		{
		$arrays = mysql_fetch_assoc($resourceId);
		}
	elseif(empty($SqlString) && empty($resourceId))
		{
		$arrays = mysql_fetch_assoc($this->query_id);
		}
  return $arrays;
 }
?>
P.S.: My experience with PEAR is, unfortunately, just like yours. Very bulky, too complicated and the results are at best mediocre. For some reason I tend to look at http://www.phpclasses.org or PEAR and then don't like them. I usually ended up writing my own. But it's good to have had a look at those examples.

Posted: Mon Sep 08, 2003 1:17 pm
by sdibb
P.S.: My experience with PEAR is, unfortunately, just like yours. Very bulky, too complicated and the results are at best mediocre. For some reason I tend to look at http://www.phpclasses.org or PEAR and then don't like them. I usually ended up writing my own. But it's good to have had a look at those examples.
So far I'm finding them saving me some troubles .. to an extent. For example, I like using DB so I can connect to any database, but the other functions aren't as well documented with examples.

I'll have to check out the other classes. Thanks.

Steve

Posted: Mon Sep 08, 2003 6:24 pm
by McGruff