Welcome Guest, Not a member yet? Register   Sign In
Getting a list within a list, each is a different table in the database
#1

[eluser]Unknown[/eluser]
I have been using Codeigniter for about 2 weeks, and I'm really impressed with how quick it is to write this code. I apologise for what is probably a noob question but I've had a good look through the forum and on google and can't find the answer.

My project is a customer database. I am trying to make a page which displays the customer's details, then has a list of notes for this customer. Within each note is a piece of text, then a list of photos relating to the note. I guess it's similar to someone's facebook page, which has a list of wall posts then each post can contain several photos.

The problem is, when I load the customer view, I am listing the notes as follows:

Code:
<? foreach($notes as $note) {
   echo $note->posted_time;
   echo $note->message;
   // list photos for this note here.
} ?>

So each individual note is seen for the first time inside the view. I need to query the photos database for photos with the note_id equal to $note->id, but this must be called from the model / controller, not the view.

Please help if you have any ideas! I am sure this kind of 'list within a list' must be quite common?

Dave
#2

[eluser]TheFuzzy0ne[/eluser]
When you make your query, you can join the two tables together, which will essentially merge the two sources together.

If there may be more than one per note, you'll need a bit of logic to work around the cross-join effects, to put your photos into an array. If you can show us the code you've got so far, we might be able to give you an example.
#3

[eluser]Unknown[/eluser]
Thanks for the response thefuzzyone. It is more than one photo per note. I eventually came up with the solution... rather than storing the results of the database query in an object, i stored them in an array. Once I was working in an array I found it easy to step through the array and add a sub-array of photos for each note.

So I have another question if you don't mind! Given that query results can be stored in both arrays and objects, and that in this case it was necessary to use an array:

When would it be necessary to use an object to store the results?
#4

[eluser]TheFuzzy0ne[/eluser]
Some people feel more comfortable using objects instead of arrays, and others use it because of the added benefits, such as encapsulation, access control modifiers and inheritance.




Theme © iAndrew 2016 - Forum software by © MyBB