Looping through left joined tables |
[eluser]jshultz[/eluser]
I've got a small problem: I've got a model that's doing a left join on two tables. A projects table and a tasks table. It's supposed to echo out the project, then echo out all the tasks associated with that project. Instead,it's repeating the project name for each task row. Here's the code: http://stikked.com/view/25802955 do you think a second foreach loop would work? inside the first foreach loop? VIEW: Code: <?php $currentProjectID = NULL; ?> Code: /* Get all Projects and Tasks by Project ID */
[eluser]kgill[/eluser]
When you use joins in a one to many relationship you get repeating columns, that's the nature of a database and how it's supposed to work. You're one step away from the solution though, you're already comparing if the currentProjectID is not equal to the p_id, but you aren't storing the p_id in currentprojectid so each time it loops it's always true and will always print.
[eluser]sophistry[/eluser]
hi jshultz... this is how i've approached joined output. maybe it will help you: Code: // this is a very simple sub-summary function that will give you nested arrays
[eluser]sophistry[/eluser]
oh yeah, and you might look at GROUP_CONCAT if you have mysql 4.1+ - that would let you do something similar in SQL: http://www.mysqlperformanceblog.com/2006...extension/ EDIT: be sure to read the comments at the bottom of that page: there is a limitation of 1024 characters when using group_concat
[eluser]jshultz[/eluser]
Sweet! Thank you for all your help. This has been very informative. |
Welcome Guest, Not a member yet? Register Sign In |