Manipulating query results |
[eluser]kjackson7_93[/eluser]
I have an application that queries a table. A field in the table is the name of a file that should be in the file system. I'd like to loop through the results, test to make sure the file exists. Currently (not CI nor MVC) I do: Code: $notes_sth = $dbh->prepare($sql); Using CI, in my model I do: Code: $board_notes = $this->db->get('board_notes'); How can I loop through $board_notes->results, insert "has_external_doc" and still return the same data structure as $board_notes->result()? Thanks in advance.
[eluser]Clooner[/eluser]
Use $board_notes->result_array() and then loop through the array using for or foreach. This all has very good documentation. See Generating Query Results Update and to convert an array back to object... You could use something like this Code: function array2obj($data)
[eluser]kjackson7_93[/eluser]
[quote author="clooner" date="1204802787"]Use $board_notes->result_array() and then loop through the array using for or foreach. This all has very good documentation. See Generating Query Results Update and to convert an array back to object... You could use something like this Code: function array2obj($data) Thanks! I like the array2obj function. I'll look into adding it. I ended up putting the loop and testing for the existence of the file in the view. I was debating if that is the proper place for it. I know using MVC isn't about separating HTML from PHP but rather separating display logic from controller logic. So in that way I could rationalize that adding logic to display a link only if a file exists is ok... BUT having too much (whatever that means) PHP code in a view page makes it more complicated for a designer with little or no programming experience to work in the views. It's all about trade offs and I usually try to err on the side of less PHP in views. I will put array2obj into an array helper and give it a try. Thanks again. |
Welcome Guest, Not a member yet? Register Sign In |