Running a Query with in a foreach loop |
[eluser]Phil Sturgeon[/eluser]
Look at the code a little closer. Firstly, he needs a list of all projects. Then he needs a list of all projects a user is involved with. Finally he needs a list of all tasks for that project. You COULD join the two but it makes the looping a P.I.T.A and think about it, its not going to be that much data. If a user is involved with 10,000 projects then the slow performance will be an acceptable punishment for being such an idiot. the only performance issue here would be the full list of projects for each user, but that is why the cache was invented. :-)
[eluser]jshultz[/eluser]
Actually, I don't need a list of all projects. Only projects that the user has created with and without tasks.
[eluser]kgill[/eluser]
[quote author="Phil Sturgeon" date="1249960433"] You COULD join the two but it makes the looping a P.I.T.A and think about it, its not going to be that much data. If a user is involved with 10,000 projects then the slow performance will be an acceptable punishment for being such an idiot. the only performance issue here would be the full list of projects for each user, but that is why the cache was invented. :-)[/quote] Which is why I prefaced it with in this case it's fine and explained why ![]()
[eluser]jshultz[/eluser]
Ok, here's an update on what I've been doing. I decided to rework my code just a bit and I have this block now: Code: <div><table> My problem is that it's echoing at every task (there's 3 each with a different projectid). My concern is that if I change Code: ($tasks->projectid === $projects->id)
[eluser]Phil Sturgeon[/eluser]
$tasks and $projects are your arrays, not your single object variables containing the data. Let's play spot the difference. Code: <div><table> That make sense? You want to name your variables something more meaningful than row, after all by the time they are in the view this should be nothing to do with the database so something more general would apply. Did you work out how to get tasks into the projects array in the end? It doesn't look like it from this code. $projects and $tasks should not be set at the same level, tasks should be part of $projects, right? Post your controller method up here.
[eluser]Johan André[/eluser]
[quote author="Phil Sturgeon" date="1249960433"]If a user is involved with 10,000 projects then the slow performance will be an acceptable punishment for being such an idiot.[/quote] Haha! Funniest comment today! ![]()
[eluser]jshultz[/eluser]
I tried it with the way you had suggested but I was getting the same results. Here is what I have right now: Site controller: Code: function project_list()
[eluser]Phil Sturgeon[/eluser]
Code: foreach( $data['projects']->result_array() as &$project) You are missing the & before $project in foreach. this probably wont work if you are still foreach'ing through $result_array(); STOP RETURNING $query! :-p
[eluser]jshultz[/eluser]
I really appreciate your time in helping me. so often i'm on the other side of the coin helping friends/family with viruses (why???) or css so to be the newbie starting out again is really interesting for me. What does the & do?
[eluser]Phil Sturgeon[/eluser]
http://www.phpbuilder.com/manual/en/lang...s.pass.php If you pass by reference instead of making a copy you are modifying the original variable. So: Code: foreach( $data['projects'] as &$project) is effectively the same as: Code: $projects = array(); See what im getting at? Instead of creating a new array to hold them all then assigning it to the same value and unsetting the temporary array, you just modify the original variable on the fly. Basically stop returning $query and return $query->result_array() and use the code with the & in, it will work. :-) |
Welcome Guest, Not a member yet? Register Sign In |