| Home | Register | FAQ | Members List | Search | Today's Posts | Mark Forums Read |
| DesignersTalk > [PHP] loops in loops & multi-dimensional arrays |
|
LinkBack | Thread Tools | Search this Thread |
|
|
#1 (permalink) |
|
www.nakedintruder.com
|
[PHP] loops in loops & multi-dimensional arrays
I've been trying all night at this and I'm still buggered.. here's what's going on simplified a bit, I've changed the group id to letters to make it easier to see what I'm after too. The relevant part of the table of my database is set out like; Code:
group id = foreign key to the group that item is part of from another table. I want to output this like so;
Without absolutely hammering the database as best I can...I've managed to get the output I want, but it's hitting the database too much, at the moment it's
If I only had 1 group and 1 item, this'd connect once then hit the database 4 times I've been trying with multi-dimensional arrays but having no luck and half-ideas I'm having don't look like they're going to hit the DB any less. I was also writing over arrays so in my most recent attempt have tried eval(); Code:
Anyone had to do this before and can put me on the right track? I'm making a right mess as you can see! Cheers |
|
|
|
|
|
#2 (permalink) |
|
vague™
Join Date: Mar 2004
Location: Glasgow
Posts: 5,488
|
Haha, what the fuck is that - a brain dump? It sounds like this could be massively simplified on the sql side, as nothing should require four database accesses for "1 group and 1 item", but you're making it too much of an effort for me to understand what you actually want Post the relevant db schemas and tell us in concise english exactly what you want the sql to do and i'll do it Last edited by i_am_cam : 30-05-2005 at 07:35. |
|
|
|
#4 (permalink) |
|
www.nakedintruder.com
|
This is the db structure (with irrelevant parts removed) ![]() The page I want to build is a list of all items in the database, headed by the group each item belongs to, It's not going well haha! We can see here that; group 1 contains 1 item, item 1 group 2 contains 2 items, item 2 and item 3. The output I'm after is;
|
|
|
|
#5 (permalink) |
|
Barney army!
Join Date: Mar 2003
Location: London
Posts: 696
|
OK, the first step is to read the results into an array. Then convert this into a new multidimensional array where each key is the group and each value is an array of items. So, for example: PHP Code:
I've not tested it so I might have made the odd mistake but thats the general idea. Luke Redpath .::. Software Engineer .::. Reevoo - Real Reviews From Real Customers
|
|
|
|
#7 (permalink) |
|
www.nakedintruder.com
|
haha, ace! it works you beauty! I owe you big time for this mate, I was well buggered as you could probably tell, brilliant!! thanks a lot! Using a while loop saved on the db loads, I was using for loops and having to find out the number of rows first and all sorts, messy. Do you mind breaking down what this part is doing so I understand it better? PHP Code:
PHP Code:
Last edited by nakedintruder : 30-05-2005 at 08:33. |
|
|
|
#9 (permalink) |
|
Barney army!
Join Date: Mar 2003
Location: London
Posts: 696
|
Ok, I'll do my best. PHP Code:
OK, obviously this loops through the resultset, returning an associative array for each row in the resultset. PHP Code:
This is where the real action happens. It creates a new key in the $combinedResults array from the group name, then treats the resulting key's value as an array itself, using the [] operator to add a new value to this array, the item name. After this loop has run, if you were to do a print_r on the $combinedResults array, you'd have an array that looked a bit like this: Code:
So as you can see from this, we have an array of groups, and each group is an array of items, which makes the loop to build your list work. By extra columns, I assume you mean how do you store more than just the item name within each group array? That's quite easy too. Do something like this: PHP Code:
Now you can use these in the list building loop like this (as an example): PHP Code:
Your array would now look like this: Code:
Hope that helps! Luke Redpath .::. Software Engineer .::. Reevoo - Real Reviews From Real Customers
|
|
|
|
#11 (permalink) |
|
Barney army!
Join Date: Mar 2003
Location: London
Posts: 696
|
In fact, if you want access to each column in the row automatically, you could just do this: PHP Code:
This would lead to the group_name column being duplicated (as both the top level array key and as a value in the array it contains but this is useful if you want access to all of your columns in one go). Luke Redpath .::. Software Engineer .::. Reevoo - Real Reviews From Real Customers
|
|
![]() |