dongzhang7961 2011-06-11 00:45 采纳率: 0%
浏览 55
已采纳

CodeIgniter:从视图传递参数到控制器?

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; ?>
  • 写回答

2条回答 默认 最新

  • dtvjl64442 2011-06-11 00:53
    关注

    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);
        }
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大