Welcome Guest, Not a member yet? Register   Sign In
Get ID from view and send it over controller to model
#1

[eluser]someone Smile[/eluser]
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! :-)
#2

[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.
#3

[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.
#4

[eluser]Unknown[/eluser]
This is not a complete code. Just the concept.

Your view:
Code:
<?php foreach($lists as $list): ?>

// Do what you want here..
<?php echo anchor('controller_name/function_to_pass_your_id/' . $list->$id, 'Link'); ?>

<?php endforeach; ?>

Your controller:
Code:
class Controller_name extends CI_Controller
{
  function function_to_pass_your_id($id)
  {
    // call the model
    $this->load->model('model_name');
    // call the function in the model to perform logic
    $this->model_name->do_something_with_id($id);
  }
}

Your model:
Code:
class Model_name extends CI_Model
{
  function do_something_with_id($id)
  {
    // do something. maybe query the db with this id
    return $something;
  }
}
#5

[eluser]someone Smile[/eluser]
[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!
#6

[eluser]someone Smile[/eluser]
Anyone?
#7

[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
#8

[eluser]someone Smile[/eluser]
Solved!

Thanks to all :-)




Theme © iAndrew 2016 - Forum software by © MyBB