Page 1 of 2
Pushing Query Results into an Array
Posted: Sat Jan 06, 2007 11:32 pm
by maxd
I need to build an array based on the results of a MySQL query. The table from which I'm calling my data looks like this:
Code: Select all
| id | parent | child1 | child2 | child3 |
--------------------------------------------------
1 | Home | | | |
2 | About | Foo1 | | |
3 | About | Foo2 | | |
4 | About | Foo3 | | |
5 | Resources | Bar1 | | |
6 | Resources | Bar2 | | |
7 | Resources | Bar3 | foo1 | |
8 | Resources | Bar4 | foo2 | |
9 | Services | Zot1 | | |
10 | Services | Zot2 | | |
11 | Services | Zot3 | zam1 | zoo1 |
12 | Services | Zot3 | zam1 | zoo2 |
The array I need to build looks like this:
Code: Select all
$menu=array();
$menu["home"]="#";
$menu["about"]=array();
$menu["about"]["foo1"]="#";
$menu["about"]["foo2"]="#";
$menu["about"]["foo3"]="#";
$menu["resources"]["Bar1"]="#";
$menu["resources"]["Bar2"]="#";
$menu["resources"]["Bar3"]=array();
$menu["resources"]["Bar3"]["foo1"]="#";
$menu["resources"]["Bar4"]=array();
$menu["resources"]["Bar4"]["foo2"]="#";
and so forth.
The challenge I'm having is successfully detecting when an object is the "last" object in the structure (prompting '= "#"'), and when an object has "child" objects (prompting '=array()').
I've tried:
Code: Select all
while($array = mysql_fetch_array($navResult)){
if(($array['parent'])&&(($array['child1'])==NULL)){
$menu[$array['parent']] = "#";
}elseif(($array['child1'])&&(($array['child2'])==NULL)){
$menu[$array['parent']] = array();
$menu[$array['parent']][$array['child1']] = "#";
}elseif(($array['child2'])&&(($array['child3'])==NULL)){
$menu[$array['parent']] = array();
$menu[$array['parent']][$array['child1']] = array();
$menu[$array['parent']][$array['child1']][$array['child2']] = "#";
}
}
and pretty much every other way I can think of, but it doesn't seem to like my method, and the result is that it only picks up seemingly random objects and loads them into the array. (I'm sure they're not really random, but I can't make any sense of it). For example, the code above creates the following array output:
Code: Select all
Array
(
[HOME] => #
[ABOUT US] => Array
(
[Foo3] => #
)
[RESOURCES] => Array
(
[Bar4] => Array
(
[foo2] => #
)
)
[SERVICES] => Array
(
[Zot2] => #
)
)
Any ideas?
thanks. max
Posted: Sat Jan 06, 2007 11:55 pm
by feyd
I know I normally just talk about the solution, but this time it was a bit more complicated, so I've illustrated it.
This should give you a pretty good idea.
Code: Select all
<?php
$test = array(
array('id'=>'1','parent'=>'Home','child1'=>null,'child2'=>null,'child3'=>null,),
array('id'=>'2','parent'=>'About','child1'=>'Foo1','child2'=>null,'child3'=>null,),
array('id'=>'3','parent'=>'About','child1'=>'Foo2','child2'=>null,'child3'=>null,),
array('id'=>'4','parent'=>'About','child1'=>'Foo3','child2'=>null,'child3'=>null,),
array('id'=>'5','parent'=>'Resources','child1'=>'Bar1','child2'=>null,'child3'=>null,),
array('id'=>'6','parent'=>'Resources','child1'=>'Bar2','child2'=>null,'child3'=>null,),
array('id'=>'7','parent'=>'Resources','child1'=>'Bar3','child2'=>'foo1','child3'=>null,),
array('id'=>'8','parent'=>'Resources','child1'=>'Bar4','child2'=>'foo2','child3'=>null,),
array('id'=>'9','parent'=>'Services','child1'=>'Zot1','child2'=>null,'child3'=>null,),
array('id'=>'10','parent'=>'Services','child1'=>'Zot2','child2'=>null,'child3'=>null,),
array('id'=>'11','parent'=>'Services','child1'=>'Zot3','child2'=>'zam1','child3'=>'zoo1',),
array('id'=>'12','parent'=>'Services','child1'=>'Zot3','child2'=>'zam1','child3'=>'zoo2',),
);
$menu = array();
foreach($test as $row)
{
unset($row['id']);
$row = array_values($row);
$row = array_filter($row);
$t =& $menu;
for($i = 0, $j = count($row)-1; $i < $j; ++$i)
{
$t[$row[$i]] = array();
$t =& $t[$row[$i]];
}
$t[$row[$i]] = '#';
}
print_r($menu);
?>
outputs
Code: Select all
Array
(
[Home] => #
[About] => Array
(
[Foo3] => #
)
[Resources] => Array
(
[Bar4] => Array
(
[foo2] => #
)
)
[Services] => Array
(
[Zot3] => Array
(
[zam1] => Array
(
[zoo2] => #
)
)
)
)
Posted: Sat Jan 06, 2007 11:56 pm
by dibyendrah
I think querying IS NULL for child1, child2, child3 and making an array might help you.
Posted: Sun Jan 07, 2007 2:22 am
by maxd
Wow.
I'm very appreciative of the thorough accounting from feyd (who is my hero). Full of syntax of which I have absolutely no understanding.

I picked up the code, and ran it, and it does indeed render the result described. Unfortunately, it's not the result I need.

I probably haven't explained the problem properly. I'm not sure I know how.
The end array output should look like this:
Code: Select all
Array
(
[home] => #
[about] => Array
(
[foo1] => #
[foo2] => #
[foo3] => #
)
[resources] => Array
(
[Bar1] => #
[Bar2] => #
[Bar3] => Array
(
[foo1] => #
)
[Bar4] => Array
(
[foo2] => #
)
)
[services] => Array
(
[Zot1] => #
[Zot2] => #
[Zot3] => Array
(
[zam1] => Array
(
[zoo1] => #
[zoo2] => #
)
)
)
)
If I write the array by hand, like so:
Code: Select all
$menu=array();
$menu["home"]="#";
$menu["about"]=array();
$menu["about"]["foo1"]="#";
$menu["about"]["foo2"]="#";
$menu["about"]["foo3"]="#";
$menu["resources"]["Bar1"]="#";
$menu["resources"]["Bar2"]="#";
$menu["resources"]["Bar3"]=array();
$menu["resources"]["Bar3"]["foo1"]="#";
$menu["resources"]["Bar4"]=array();
$menu["resources"]["Bar4"]["foo2"]="#";
$menu["services"]["Zot1"]="#";
$menu["services"]["Zot2"]="#";
$menu["services"]["Zot3"]=array();
$menu["services"]["Zot3"]["zam1"]=array();
$menu["services"]["Zot3"]["zam1"]["zoo1"]="#"
$menu["services"]["Zot3"]["zam1"]["zoo2"]="#"
and run the recursive function against it, like so:
Code: Select all
function display_menu($m) {
foreach ($m as $section => $link) {
if (!is_array($link))
echo "<ul><li><a href='{$link}'>{$section}</a></li></ul>";
else {
echo "<ul><li>{$section}";
display_menu($link);
echo '</li></ul>';
} // end of else
} // end of foreach loop
} // end of function
display_menu($menu);
I get exactly the display I desire:
* home
* about
o foo1
o foo2
o foo3
* resources
o Bar1
o Bar2
o Bar3
+ foo1
o Bar4
+ foo2
* services
o Zot1
o Zot2
o Zot3
+ zam1
# zoo1
# zoo2
The trick is, I would like to build the array dynamically, from a MySQL query, so that I can have the navigation and sitemap dynamically updated based on modifications to the site structure.
So, the solution I'm looking for is a way to query the table I described in my first post, and push the results into an array exactly like the one I have in this post. Perhaps it's impossible. If feyd can't figure it out, I'll know I'm lost. Or at least that I never should have gone down this road.

Posted: Sun Jan 07, 2007 4:56 am
by onion2k
My advice would be to redesign the database using a system that stores the id of the parent, and then recurse through the results. In your current design you're storing a lot of redundant data (empty children), and as soon as your client decides they need a 4th child category you'll end up rewriting the whole lot.
http://www.phpriot.com/d/articles/php/a ... index.html
Posted: Sun Jan 07, 2007 9:05 am
by feyd
Oops, I didn't notice it eating the previous entries. Simple enough to fix:
Code: Select all
<?php
$test = array(
array('id'=>'1','parent'=>'Home','child1'=>null,'child2'=>null,'child3'=>null,),
array('id'=>'2','parent'=>'About','child1'=>'Foo1','child2'=>null,'child3'=>null,),
array('id'=>'3','parent'=>'About','child1'=>'Foo2','child2'=>null,'child3'=>null,),
array('id'=>'4','parent'=>'About','child1'=>'Foo3','child2'=>null,'child3'=>null,),
array('id'=>'5','parent'=>'Resources','child1'=>'Bar1','child2'=>null,'child3'=>null,),
array('id'=>'6','parent'=>'Resources','child1'=>'Bar2','child2'=>null,'child3'=>null,),
array('id'=>'7','parent'=>'Resources','child1'=>'Bar3','child2'=>'foo1','child3'=>null,),
array('id'=>'8','parent'=>'Resources','child1'=>'Bar4','child2'=>'foo2','child3'=>null,),
array('id'=>'9','parent'=>'Services','child1'=>'Zot1','child2'=>null,'child3'=>null,),
array('id'=>'10','parent'=>'Services','child1'=>'Zot2','child2'=>null,'child3'=>null,),
array('id'=>'11','parent'=>'Services','child1'=>'Zot3','child2'=>'zam1','child3'=>'zoo1',),
array('id'=>'12','parent'=>'Services','child1'=>'Zot3','child2'=>'zam1','child3'=>'zoo2',),
);
$menu = array();
foreach($test as $row)
{
unset($row['id']);
$row = array_values($row);
$row = array_filter($row);
$t =& $menu;
for($i = 0, $j = count($row)-1; $i < $j; ++$i)
{
if (!array_key_exists($row[$i], $t))
{
$t[$row[$i]] = array();
}
$t =& $t[$row[$i]];
}
$t[$row[$i]] = '#';
}
print_r($menu);
?>
will output
Code: Select all
Array
(
[Home] => #
[About] => Array
(
[Foo1] => #
[Foo2] => #
[Foo3] => #
)
[Resources] => Array
(
[Bar1] => #
[Bar2] => #
[Bar3] => Array
(
[foo1] => #
)
[Bar4] => Array
(
[foo2] => #
)
)
[Services] => Array
(
[Zot1] => #
[Zot2] => #
[Zot3] => Array
(
[zam1] => Array
(
[zoo1] => #
[zoo2] => #
)
)
)
)
Posted: Sun Jan 07, 2007 5:02 pm
by maxd
I decided to follow onion2k's advice. I have been fighting the nagging feeling that my DB structure was limiting, so the tutorial (
http://www.phpriot.com/d/articles/php/a ... index.html) onion2k directed me to was exactly what I'd been looking for, and I read it carefully and followed all the directions...
Right up to Part III, the real world web application tutorial, which the author evidently never wrote!
So I've got this great DB, great function which successfully creates a great array, and I have no idea how to recurse through it in PHP to actually display the menu.
Here's what the final array looks like:
Code: Select all
Array
(
[0] => stdClass Object
(
[id] => 0
[children] => Array
(
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
[7] => 7
)
)
[1] => stdClass Object
(
[id] => 1
[parent_id] => 0
[title] => Home
[nleft] => 1
[nright] => 2
[nlevel] => 1
[children] => Array
(
)
)
[2] => stdClass Object
(
[id] => 2
[parent_id] => 0
[title] => About Us
[nleft] => 3
[nright] => 16
[nlevel] => 1
[children] => Array
(
[8] => 8
[9] => 9
[10] => 10
[11] => 11
[12] => 12
[13] => 13
)
)
[3] => stdClass Object
(
[id] => 3
[parent_id] => 0
[title] => Why Donation
[nleft] => 17
[nright] => 26
[nlevel] => 1
[children] => Array
(
[14] => 14
[15] => 15
[16] => 16
[17] => 17
)
)
[4] => stdClass Object
(
[id] => 4
[parent_id] => 0
[title] => Resources
[nleft] => 27
[nright] => 48
[nlevel] => 1
[children] => Array
(
[18] => 18
[19] => 19
[20] => 20
[21] => 21
)
)
[5] => stdClass Object
(
[id] => 5
[parent_id] => 0
[title] => Our Stories
[nleft] => 49
[nright] => 54
[nlevel] => 1
[children] => Array
(
[28] => 28
[29] => 29
)
)
[6] => stdClass Object
(
[id] => 6
[parent_id] => 0
[title] => News Room
[nleft] => 55
[nright] => 62
[nlevel] => 1
[children] => Array
(
[30] => 30
[31] => 31
[32] => 32
)
)
[7] => stdClass Object
(
[id] => 7
[parent_id] => 0
[title] => Community
[nleft] => 63
[nright] => 72
[nlevel] => 1
[children] => Array
(
[33] => 33
[34] => 34
[35] => 35
[36] => 36
)
)
[8] => stdClass Object
(
[id] => 8
[parent_id] => 2
[title] => Overview
[nleft] => 4
[nright] => 5
[nlevel] => 2
[children] => Array
(
)
)
[9] => stdClass Object
(
[id] => 9
[parent_id] => 2
[title] => Mission and Values
[nleft] => 6
[nright] => 7
[nlevel] => 2
[children] => Array
(
)
)
[10] => stdClass Object
(
[id] => 10
[parent_id] => 2
[title] => Donation Process
[nleft] => 8
[nright] => 9
[nlevel] => 2
[children] => Array
(
)
)
[11] => stdClass Object
(
[id] => 11
[parent_id] => 2
[title] => History
[nleft] => 10
[nright] => 11
[nlevel] => 2
[children] => Array
(
)
)
[12] => stdClass Object
(
[id] => 12
[parent_id] => 2
[title] => Board of Directors
[nleft] => 12
[nright] => 13
[nlevel] => 2
[children] => Array
(
)
)
[13] => stdClass Object
(
[id] => 13
[parent_id] => 2
[title] => Funding
[nleft] => 14
[nright] => 15
[nlevel] => 2
[children] => Array
(
)
)
[14] => stdClass Object
(
[id] => 14
[parent_id] => 3
[title] => The Difference it Makes
[nleft] => 18
[nright] => 19
[nlevel] => 2
[children] => Array
(
)
)
[15] => stdClass Object
(
[id] => 15
[parent_id] => 3
[title] => Donation Myths/Facts
[nleft] => 20
[nright] => 21
[nlevel] => 2
[children] => Array
(
)
)
[16] => stdClass Object
(
[id] => 16
[parent_id] => 3
[title] => Religious/Racial Considerations
[nleft] => 22
[nright] => 23
[nlevel] => 2
[children] => Array
(
)
)
[17] => stdClass Object
(
[id] => 17
[parent_id] => 3
[title] => Donation FAQ
[nleft] => 24
[nright] => 25
[nlevel] => 2
[children] => Array
(
)
)
[18] => stdClass Object
(
[id] => 18
[parent_id] => 4
[title] => Document Library
[nleft] => 28
[nright] => 29
[nlevel] => 2
[children] => Array
(
)
)
[19] => stdClass Object
(
[id] => 19
[parent_id] => 4
[title] => Glossary of Terms
[nleft] => 30
[nright] => 31
[nlevel] => 2
[children] => Array
(
)
)
[20] => stdClass Object
(
[id] => 20
[parent_id] => 4
[title] => Family Resources
[nleft] => 32
[nright] => 39
[nlevel] => 2
[children] => Array
(
[22] => 22
[23] => 23
[24] => 24
)
)
[21] => stdClass Object
(
[id] => 21
[parent_id] => 4
[title] => Professional Resources
[nleft] => 40
[nright] => 47
[nlevel] => 2
[children] => Array
(
[25] => 25
[26] => 26
[27] => 27
)
)
[22] => stdClass Object
(
[id] => 22
[parent_id] => 20
[title] => Contacting Donor Families
[nleft] => 33
[nright] => 34
[nlevel] => 3
[children] => Array
(
)
)
[23] => stdClass Object
(
[id] => 23
[parent_id] => 20
[title] => Contacting Transplant Recipients
[nleft] => 35
[nright] => 36
[nlevel] => 3
[children] => Array
(
)
)
[24] => stdClass Object
(
[id] => 24
[parent_id] => 20
[title] => Support Resources
[nleft] => 37
[nright] => 38
[nlevel] => 3
[children] => Array
(
)
)
[25] => stdClass Object
(
[id] => 25
[parent_id] => 21
[title] => Partners
[nleft] => 41
[nright] => 42
[nlevel] => 3
[children] => Array
(
)
)
[26] => stdClass Object
(
[id] => 26
[parent_id] => 21
[title] => Coroner's Corner
[nleft] => 43
[nright] => 44
[nlevel] => 3
[children] => Array
(
)
)
[27] => stdClass Object
(
[id] => 27
[parent_id] => 21
[title] => Funeral Services
[nleft] => 45
[nright] => 46
[nlevel] => 3
[children] => Array
(
)
)
[28] => stdClass Object
(
[id] => 28
[parent_id] => 5
[title] => Donor Families
[nleft] => 50
[nright] => 51
[nlevel] => 2
[children] => Array
(
)
)
[29] => stdClass Object
(
[id] => 29
[parent_id] => 5
[title] => Transplant Recipients
[nleft] => 52
[nright] => 53
[nlevel] => 2
[children] => Array
(
)
)
[30] => stdClass Object
(
[id] => 30
[parent_id] => 6
[title] => News Releases
[nleft] => 56
[nright] => 57
[nlevel] => 2
[children] => Array
(
)
)
[31] => stdClass Object
(
[id] => 31
[parent_id] => 6
[title] => In the News
[nleft] => 58
[nright] => 59
[nlevel] => 2
[children] => Array
(
)
)
[32] => stdClass Object
(
[id] => 32
[parent_id] => 6
[title] => Media Center
[nleft] => 60
[nright] => 61
[nlevel] => 2
[children] => Array
(
)
)
[33] => stdClass Object
(
[id] => 33
[parent_id] => 7
[title] => Programs
[nleft] => 64
[nright] => 65
[nlevel] => 2
[children] => Array
(
)
)
[34] => stdClass Object
(
[id] => 34
[parent_id] => 7
[title] => Community Calendar
[nleft] => 66
[nright] => 67
[nlevel] => 2
[children] => Array
(
)
)
[35] => stdClass Object
(
[id] => 35
[parent_id] => 7
[title] => Volunteer
[nleft] => 68
[nright] => 69
[nlevel] => 2
[children] => Array
(
)
)
[36] => stdClass Object
(
[id] => 36
[parent_id] => 7
[title] => Newsletters
[nleft] => 70
[nright] => 71
[nlevel] => 2
[children] => Array
(
)
)
)
Anyone who can coach me on turning this into a practical menu will earn my undying gratitude.
Even pointing me to a good tutorial on extracting and displaying elements from a multidimensional array would be good. The most I seem able to do is get it to regurgitate all of its guts.
30 hours into this menu, now. I just know it's the best way in the long run. But it's painful.
Posted: Sun Jan 07, 2007 5:13 pm
by feyd
If memory serves, you sort by the left or right edge information. You can do this in the database query.
Posted: Sun Jan 07, 2007 5:36 pm
by maxd
So what you're saying is, I don't need the confounded multidimensional array (from which I am utterly incapable of extracting specific data) at all? I can just run a query against my new table, ORDER BY nleft, nright, nlevel (or some combination thereof), and output straight in PHP with some case-based formatting?
I'll give it a shot. I wonder why the guy wrote the 12 page article that put everything in an array. I sure wish he'd have written the final part, in which he was supposed to bring it all together for us sad pedestrians. Perhaps he got hit by a bus or something.
Thanks for all your work on this, feyd. I'll post a final synopsis when (if) I ever get it working. This seems like the kind of thing that should have a satisfactory and widely available solution, given its obvious utility in creating dynamic hierarchical menu systems, sitemaps, etc. There are a lot of people doing recursive menus with multiple sql queries built into the function, but my goal was to avoid the looping db queries. Even though my current implementation wouldn't really load the server, it just seems unneccessary.
Thanks again.
max
Posted: Sun Jan 07, 2007 8:12 pm
by maxd
OK. I got this working. It's a pretty good N-Level Nested Tree, I think. I see no reason why this couldn't be utilized for almost any size hierarchical system. Especially if you pick up the functions from the article onion2k recommended (
http://www.phpriot.com/d/articles/php/a ... index.html). One of the built in functions rebuilds the database table with the appropriate nleft and nright values, which is essential if this is to be easily scalable.
Here's what the DB looks like (the tutorial actually has the schema to automatically build it):
Code: Select all
| id | parent_id | title | nleft | nright | nlevel
--------------------------------------------------
1 | 0 | Home | 1 | 2 | 1
2 | 0 | Section1 | 3 | 10 | 1
3 | 0 | Section2 | 11 | 18 | 1
4 | 0 | Section3 | 19 | 20 | 1
5 | 0 | Section4 | 21 | 22 | 1
6 | 0 | Section5 | 23 | 24 | 1
7 | 2 | SubSec 1 | 4 | 5 | 2
8 | 2 | SubSec 2 | 6 | 7 | 2
9 | 2 | SubSec 3 | 8 | 9 | 2
10 | 3 | SubSec 4 | 12 | 13 | 2
11 | 10 | SubSub 1 | 14 | 15 | 3
12 | 10 | SubSub 2 | 16 | 17 | 3
As per feyd's recommendation, I just query the db directly, rather than going through the functions in the tutorial, but that's because I'm incompetent at getting the data back out of the array.
Code: Select all
// NAVIGATION QUERY
// Perform MySQL query for site navigation
// set up the queries
$navQuery = sprintf("
SELECT * FROM nested_tree ORDER BY nleft,nlevel;
");
// Perform Queries
$navResult = mysql_query($navQuery);
$navRows = mysql_num_rows($navResult);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$navResult) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $navQuery;
die($message);
}
// END NAV QUERY
Then, I can just output and iterate through with PHP:
Code: Select all
// Output Nav results
$navNlevel = "1";
echo "<ul>\n";
while ($row = mysql_fetch_assoc($navResult)) {
if ($navNlevel<$row['nlevel']){
echo "<ul>\n";
} elseif ($navNlevel>$row['nlevel']){
$levelDrop = ($navNlevel-$row['nlevel']); //set counter for level shift
for($i = 0; $i < $levelDrop; $i++) // loop through to close lists
{
echo("</ul>\n</li>\n");
}
}
echo "<li>" . $row['title'];
if (($row['nleft']+1)==$row['nright']){
echo "</li>\n";
} else { echo "\n"; }
$navNlevel = $row['nlevel']; //set LEVEL for processing above
}
if ($navNlevel>1){
$levelDrop = ($navNlevel-1); //set counter for level shift
for($i = 0; $i < $levelDrop; $i++) // loop through to close lists
{
echo("</ul>\n</li>\n");
}
}
echo "</ul>\n";
?>
The nice things about it are, it's dynamic, easily scaled (add a section/parent/level to the db, run the db update function from the tutorial, and you've got new navigation) and only uses one MySQL query, instead of repeated queries for each level.
Seems good to me. Anything I missed?
Thanks feyd and onion2k for your help.
Posted: Mon Jan 08, 2007 12:08 am
by Kieran Huggins

talk about complicated! wow.... all those columns just for nav? And there doesn't even seem to be URL's anywhere....

Posted: Mon Jan 08, 2007 2:24 am
by onion2k
Kieran Huggins wrote:
talk about complicated! wow.... all those columns just for nav? And there doesn't even seem to be URL's anywhere....

Yes it's complicated, but it means you can pull out any branch of the tree
with a single query ... no recursion needed unlike a single parent id tree. For large trees that's an absolute godsend because it's considerably quicker. I really wouldn't write a tree system any other way these days.
Posted: Mon Jan 08, 2007 11:41 am
by maxd
I actually didn't include all the fields I have in the DB, only the relevant ones. So there are even more! I include a field to store the page content, title and meta tags, etc. You certainly could include a URL field easily.
But if you think about it, there are really only 3 fields you have to think about (if you take advantage of the functions included in the tutorial onion2k referenced). "title", "parent_id" and "nlevel". The function automatically populates the nleft and nright fields for you. And if you're good with arrays, the functions in the tutorial include about every way possible to grab info.
In fact, if anyone can post code for pulling and displaying data from the array I showed above, I'm sure it's more efficient than my convoluted if/elseif spaghetti. I'd love to see it, and thus improve myself.
Posted: Mon Jan 08, 2007 11:50 am
by matthijs
maxd wrote:In fact, if anyone can post code for pulling and displaying data from the array I showed above, I'm sure it's more efficient than my convoluted if/elseif spaghetti. I'd love to see it, and thus improve myself.
Did you try the code from phpriot? Seems like that class has most functions you'd want.
he also published an appendix (sort of third article)
http://www.phpriot.com/d/articles/php/a ... index.html
Posted: Mon Jan 08, 2007 12:11 pm
by Kieran Huggins
Meh.
Saving db queries is overrated, in my opinion. Database connections are expensive, simple queries: not so much.
Besides, if it's a really big hit you should cache the result.