CodeIgniter: Passing Arguments from View to Controller?

10,904

Solution 1

You should do that in your Controller before you are passing the data to the View. Try with something like this:

<?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
        $query = $this->Bookmarks->get_latest_bookmarks(4);
        $bookmarks = array();
        $tags = array();
        foreach ($query->result() as $row) {
             $bookmark_query = $this->Bookmarks->get_bookmark_tags($row->id);
             $bookmark_arr = array();
             foreach (bookmark_query->result() as $bookm) {
                 array_push($bookmark_arr, $bookm);
             }
             array_push($tags, $bookmark_arr);
             array_push($bookmarks, $row);
        }
        $data['tags'] = $tags;
        $data['bookmarks'] = $bookmarks;
        // Load and Pass Data into View
        $this->load->view('welcome_message', $data);
    }
} 

Solution 2

You don't.

The point in using a Framework is to default to proper standards. CodeIgniter follows a loose MVC pattern but you should never pass things from the view to the controller.

You can do it, but if you do it you'll be getting into a spaghetti mess pretty soon.

Grab the ID's on the controller. Even if it implicates running the same loop twice. You'll thank yourself latter on.

Share:
10,904
ritch
Author by

ritch

Updated on June 15, 2022

Comments

  • ritch
    ritch almost 2 years

    EDIT: With the code below now, I am unsure on how to print out the bookmarks and the tags correctly


    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:

    <?php 
    
    class Bookmark_model extends CI_Model {
    
        function __construct()
        {
            parent::__construct();
        }
    
        function get_latest_bookmarks($limit) 
        {
            // Load Database
            $this->load->database();
            // Query Database 
            $query = $this->db->get('Bookmark', $limit);
            // Return Result
            return $query;
        }
    
        function get_bookmark_tags($id)
        {
            // Load Database
            $this->load->database();
            $query = $this->db->query('SELECT Tag.Title 
                                        FROM `Tag` 
                                        INNER JOIN BookmarkTag
                                        WHERE BookmarkTag.BookmarkID = "'.$id.'" AND Tag.TagID = BookmarkTag.TagID');
            return $query;
        }
    

    Controller:

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Welcome extends CI_Controller {
    
        public function index()
        {
            // Load URL Helper
            $this->load->helper('url');
            // Load User Library
            $this->load->library('ion_auth');
            // Is User Logged In
            if ($this->ion_auth->logged_in())
            {
                $data['user'] = $this->ion_auth->get_user_array();
            }
            else
            {
                redirect('auth/login');
            }
            // Load Bookmark Model
            $this->load->model('Bookmark_model');
    
            // Create Arrays
            $bookmarks = array();
            $tags = array();
    
            // Query Database
            $query = $this->Bookmark_model->get_latest_bookmarks(4);
            // 
            foreach ($query->result() as $row) {
                 array_push($tags, $this->Bookmark_model->get_bookmark_tags($row->BookmarkID));
                 array_push($bookmarks, $row);
            }
            $data['tags_latest'] = $tags;
            $data['bookmarks_latest'] = $bookmarks;
            $this->load->view('welcome_message', $data);
        }
    
    }
    

    View:

    <h1>Latest Bookmarks</h1>
    
    <?php foreach ($bookmarks_latest as $bookmark): ?>
    
    <?php print_r($bookmark); ?>
    
    <?php print_r($tags_latest->result()); ?>
    
    <?php endforeach; ?>
    
    • b_dubb
      b_dubb almost 13 years
      there may be a way to do this but data is supposed to flow from the controller to the view and not the other way around.
    • ASEN
      ASEN almost 9 years
      Could anyone answer how to pass the argument from view to controller for example writing this in view don't work <?=form_open("Welcome/bookmark_tags($id)";?> if $id argument is an array, then how we will pass
  • Jonas
    Jonas almost 13 years
    @ritch: No, but you can add that with $data['query'] = $bookmarks; before loading the view, if you want.
  • ritch
    ritch almost 13 years
    How would I print the tags? I have added edited the code above to the latest.
  • ritch
    ritch almost 13 years
    Hi, are you answering the question after the EDIT?
  • Jonas
    Jonas almost 13 years
    @ritch: I have updated my code. Try print_r($tags); and print_r($bookmarks); now in your view. And you can loop through them.
  • Frankie
    Frankie almost 13 years
    @ritch my answer is not really related to your code so wouldn't matter if it were before or after the edit. Just don't pass things from the view to the controller. Now, if you have doubts on other things, just open a new question for those.
  • Undermine2k
    Undermine2k over 11 years
    I disagree. You can pass "things" to controllers, how else would you implement say a Login, surely you need to get POST data from the view, this answer is nonsense.
  • Frankie
    Frankie over 11 years
    @Undermine2k, I can understand your point of view but on a web application it does not work that way. The client (user) submits data to the apache (server) that will then interpreter your PHP code (CI App). As the CI App is loaded your data is readily available from the $_SERVER, $_POST whatever... and the first thing CI runs is your controller, you then do what you have to do and pass the data to the viewer to interpret it and serve it to the client as a nice web page. Viewer in this scenario means something completely different than your browser UI.