Welcome Guest, Not a member yet? Register   Sign In
Passing Arguments from View to Controller?
#1

[eluser]Unknown[/eluser]
I'm completely new to CI and I have recently hit a road block. I'm very unsure how I would go about passing a function argument from the view file to the controller so I could use it on a function?

I have a foreach loop on the view going through the all the items passed by function get_latest_bookmarks. That function returns a ID for each item and I am wanting to use this with another function called get_bookmark_tags which will get the tags of the bookmark from another table. I have provided the code I have done so far below.

Model:

Code:
class Bookmarks extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
    
    // GET LATEST BOOKMARKS
    
    function get_latest_bookmarks($limit)
    {
        $this->load->database();
        $this->db->order_by("id", "desc");
        $query = $this->db->get('site', $limit);
        return $query;
    }
    
    // GET HOTTEST BOOKMARKS
    
    function get_hottest_bookmarks($limit)
    {
        $this->load->database();
    }
    
    // GET BOOKMARK TAGS
    
    function get_bookmark_tags($id)
    {
        $this->load->database();
    }

}

Controller:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

    public function index()
    {
        // Load Model
        $this->load->model('Bookmarks');
        // Get Latest Bookmarks
        $data['query'] = $this->Bookmarks->get_latest_bookmarks(4);
        // Load and Pass Data into View
        $this->load->view('welcome_message', $data);
    }
}

View:

Code:
<body>

<h1>Hot Bookmarks</h1>

<p>The most popular bookmarks will appear here.</p>

<h1>Latest Bookmarks</h1>

&lt;?php foreach ($query->result() as $row): ?&gt;

<div class="bookmark">
    <b><a >url;?&gt;">&lt;?=$row->title;?&gt;</a></b>
    <p>Source: &lt;?=$row->source;?&gt;</p>
</div>

&lt;?php endforeach; ?&gt;

&lt;/body&gt;
#2

[eluser]mikelexp[/eluser]
Something like this:

Controller:

Code:
$this->load->model("bookmarks");

View:

Code:
$this->bookmarks->get_latest_bookmarks();
#3

[eluser]jmadsen[/eluser]
beans,

My first thought is, you should make the join in the database query and return a single resultset. If you're not clear on how to do that, give us a shout and folks can explain how to set it up.

If you know about that, but have a reason for not joining the tables, then do all of your work in the controller. You can return either a resultset object or an array, so check how you want things set up. returning

Psuedo code to give you the idea:

Code:
if ($this->data->bookmarks = $this->Bookmarks->get_latest_bookmarks(4)){
    foreach ($this->data->bookmarks as &$bookmark){
        $bookmark->tags = $this->Bookmarks->get_bookmark_tags($bookmark->id);
    }
}
$this->load->view('welcome_message', $data);

That was banged out really quickly just to give you the idea. In your view, each bookmark will now have an array of tags linked to it. Try not to call model functions from a view; that goes against the idea of MVC




Theme © iAndrew 2016 - Forum software by © MyBB