Help with array refencing database table. - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Help with array refencing database table. (/showthread.php?tid=14637) |
Help with array refencing database table. - El Forum - 01-09-2009 [eluser]spmckee[/eluser] Greetings, This is my first dive into CI and am loving it but running into a small snag. I can get the whole "Works Table" array and pass it on to the view for display but I need to change the values of "task_1, task_2 and task_3" into the names from the "Task Table", which is currently not happening. Any help is greatly appreciated. Thanks! Works Table: +++++++++++++++++++++++++++++++++++++++++++++++ ID name task_1 task_2 task_3 ---------------------------------------------- 1 Nike 1 3 3 2 Dell 3 1 5 3 IBM 4 2 +++++++++++++++++++++++++++++++++++++++++++++++ Task Table: ++++++++++++++ ID name -------------- 1 Design 2 Development 3 Research 4 Concepting ++++++++++++++ The Model Code: class MOurWork extends My_Model{ The Controller Code: <?php The View Code: <div class="works-thumbs-container"> Help with array refencing database table. - El Forum - 01-09-2009 [eluser]kgill[/eluser] You can do that either in the query using a join or in code. Because of the way you've structured your tables the query is going to be ugly, basically you're going to end up joining the works table to the tasks table 3 times (one for each task) which also means aliasing the tasks table to prevent ambiguous columns. To do it in code, put a 2nd function in your model for getting the tasks, fetch those into an array indexed by the task id; that way you can just dump the task out to the screen using your $list[$task_key_name], e.g. $tasks[$list[$task_key_name]]. Help with array refencing database table. - El Forum - 01-09-2009 [eluser]OwanH[/eluser] Hey, I would suggest normalizing your database further so you can have better table relationships. Here are what the normalized tables would look like (I'm arbitrarily assuming the names in your works table are clients): Client Table: +++++++++ ID name ————————— 1 Nike 2 Dell 3 IBM +++++++++ Task Table: ++++++++++++++ ID name ————————————— 1 Design 2 Development 3 Research 4 Concepting ++++++++++++++ Works Table: +++++++++++++++++++++++++++++++++++++ ID client_id task_id active ————————————————————————————————————— 1 1 1 1 2 1 3 1 3 1 3 1 4 2 3 1 5 2 1 1 6 2 5 1 7 3 4 1 8 3 2 1 +++++++++++++++++++++++++++++++++++++ Your model will then become Code: class MOurWork extends My_Model { Hope this helps. |