CodeIgniter:将参数从View传递到控制器? [英] CodeIgniter: Passing Arguments from View to Controller?

查看:121
本文介绍了CodeIgniter:将参数从View传递到控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:现在使用下面的代码,我不确定如何正确打印书签和标签

我是CI的新手,我最近打了一个路障。我非常不确定如何将一个函数参数从视图文件传递到控制器,所以我可以使用它在一个函数?

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?

我有一个foreach循环在视图中遍历通过函数get_latest_bookmarks传递的所有项。该函数返回每个项目的ID,我想使用另一个名为get_bookmark_tags的函数,它将从另一个表中获取书签的标签。我已经提供了我到目前为止所做的代码。

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.

模型:

<?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;
    }

控制器:

<?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);
    }

}

查看:

<h1>Latest Bookmarks</h1>

<?php foreach ($bookmarks_latest as $bookmark): ?>

<?php print_r($bookmark); ?>

<?php print_r($tags_latest->result()); ?>

<?php endforeach; ?>


推荐答案

数据到View。
尝试这样:

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);
    }
} 

这篇关于CodeIgniter:将参数从View传递到控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆