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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sdibb
Forum Newbie
Posts: 5
Joined: Mon Sep 08, 2003 12:34 pm
Location: Orem, Utah, USA

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

Post 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
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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.
sdibb
Forum Newbie
Posts: 5
Joined: Mon Sep 08, 2003 12:34 pm
Location: Orem, Utah, USA

Post 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
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Post Reply