combine functions ? |
[eluser]zztemp[/eluser]
Hi, So its been i while since ive coded, i restarted. Creating some small order tool. The language you'll see is dutch. The function bekijkDetails() gets al the info of an order. But i want to retrieve all the comments posted on an order aswell, and i would like to do that in the same function. How can i do this? Code: public function bekijkDetails() Following is the function to retrieve the comments on an order. I know i could do it in one sql command, but that it only retrieves 1 comment (cause of the for each) Code: public function HaalBerichtOp(){ I hope someone could clarify this for me. If you would like to see how it works: example hit the application logo to view the details of an order
[eluser]neilmcgann[/eluser]
Nooooo! SQL injection alert! Passing an unfiltered part of a URL string into a SQL query is disastrous. If you must build the SQL this way use a parameterised query, or use active record which does escaping for you. To address the main question - do a join between the tables on the id to fetch all the comments in one db query.
[eluser]zztemp[/eluser]
I know that there is possibility for SQL injection. THanks for mentioning it. But i know the 20 people that will be using this tool (on our intranet) So i'm not concerned with it. But you are right, it is an issue. I tried the join, but i couln't get it to werk properly so i started looking for other possibilities
[eluser]zztemp[/eluser]
So i got it to work but sadly, with "old" php : Code: public function HaalBerichtOp(){ how do i do this in codeigniter? Would be helpfull in furter functions so i re-wrote it to: Code: public function HaalBerichtOp(){ I just post this, maybe other people (beginners) will find it usefull someday
[eluser]neilmcgann[/eluser]
I couldn't resist replying. The last function could be re-coded as: Code: public function HaalBerichtOp() Or in active record as: Code: public function HaalBerichtOp() I'm presuming that you don't actually need to loop through the returned results for any reason, so I just send them back in the same numerically-keyed array format as result_array returns them. You can make the active record version even more concise by using get_where or chaining the sections together to remove most of the $this->db parts. Thinking about the overall problem, I'd probably not bother with the join as it would make it messier to handle the results. I'd just retrieve the product and then do something like: Code: $data['comments'] = HaalBerichtOp(); ...to add an array of comments in the product data assembled in $data. Or something like that
[eluser]zztemp[/eluser]
Dear neilmcgann, Why wouldn't you reply? I'm thankfull that you did, i didn't know that trick with the questionmark. Adding to this is the fact that i only created one or two small projects in CI before i "stopped coding". So i don't realy know my way around CI that well. As for your solution, i figured that out myself this is what i did (i'll rewrite my sql after this post ) : i have two functions in my controller: This one is to get my messages: Code: public function HaalBerichtOp(){ Code: public function bekijkDetails(){ Code: for($i=0; $i<count($comment);$i++){ The only problem with this is, i was thinking of refreshing my div with jquery if someone added a comment to an order. But now i'll have to refresh all the details + the comments. So it's not ideal i guess. Thanks for the reply! |
Welcome Guest, Not a member yet? Register Sign In |