![]() |
Get ID from view and send it over controller to model - 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: Get ID from view and send it over controller to model (/showthread.php?tid=49902) |
Get ID from view and send it over controller to model - El Forum - 03-06-2012 [eluser]someone ![]() Hello! As the name of thread says, I don't know how to send ID from foreach (which is in view) to controller which connect view and model, to model where the code will be executed and showed back to view. What's the best way to do that? Thanks! :-) Get ID from view and send it over controller to model - El Forum - 03-06-2012 [eluser]pbflash[/eluser] It should be done in the controller, then you pass it to the model and perform what you want, then pass the results to the view. Get ID from view and send it over controller to model - El Forum - 03-06-2012 [eluser]Aken[/eluser] Do all of your logic that generates data to be displayed in the view BEFORE passing to it. That's the point of MVC. Get ID from view and send it over controller to model - El Forum - 03-06-2012 [eluser]Unknown[/eluser] This is not a complete code. Just the concept. Your view: Code: <?php foreach($lists as $list): ?> Your controller: Code: class Controller_name extends CI_Controller Your model: Code: class Model_name extends CI_Model Get ID from view and send it over controller to model - El Forum - 03-07-2012 [eluser]someone ![]() [quote author="Aken" date="1331079400"]Do all of your logic that generates data to be displayed in the view BEFORE passing to it. That's the point of MVC.[/quote] Ok, I will explain my problem and you will see what I think. I have news system and two tables articles and comments. On first (index) page of site I want to show article's title and then the content of article. After that, I want to show how many comments have been entered -> in background I will set query which selects all comments with id of article and then display number of them with num_rows() on the first page. What's the best way to do that? @axe: Thanks for this piece of code! Get ID from view and send it over controller to model - El Forum - 03-08-2012 [eluser]someone ![]() Anyone? Get ID from view and send it over controller to model - El Forum - 03-08-2012 [eluser]animatora[/eluser] What is your DB structure, can u get all data with one query ? Code: SELECT *, (SELECT COUNT(coment_id) FROM comment WHERE comment.article_id = article.id) AS num_of_comments FROM article Get ID from view and send it over controller to model - El Forum - 03-08-2012 [eluser]someone ![]() Solved! Thanks to all :-) |