CodeIgniter: Passing arguments from a view to a controller?

EDIT: With the code below, I'm not sure how to print bookmarks and tags correctly


Im brand new to CI and I recently hit the road block. Im very unsure how I would like to pass the argument of the function from the view file to the controller so that I can use it for the function?

I have a foreach loop on a view passing through all the elements passed by the get_latest_bookmarks function. This function returns an identifier for each element, and I want to use it with another function called get_bookmark_tags, which will receive bookmark tags from another table. I have provided the code that 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; ?> 
+7
source share
2 answers

You must do this in your controller before passing data to the view. Try 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); } } 
+4
source

You do not have.

The point of use of the Framework is the corresponding standards by default. CodeIgniter follows the free MVC pattern, but you should never pass things from the view to the controller.

You can do it, but if you do, you will soon be confused.

Take the controller ID. Even if this implies executing the same cycle twice. You will forget yourself last.

0
source

All Articles