dongzhang7961 2011-06-10 16: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:

  1. <?php
  2. class Bookmark_model extends CI_Model {
  3. function __construct()
  4. {
  5. parent::__construct();
  6. }
  7. function get_latest_bookmarks($limit)
  8. {
  9. // Load Database
  10. $this->load->database();
  11. // Query Database
  12. $query = $this->db->get('Bookmark', $limit);
  13. // Return Result
  14. return $query;
  15. }
  16. function get_bookmark_tags($id)
  17. {
  18. // Load Database
  19. $this->load->database();
  20. $query = $this->db->query('SELECT Tag.Title
  21. FROM `Tag`
  22. INNER JOIN BookmarkTag
  23. WHERE BookmarkTag.BookmarkID = "'.$id.'" AND Tag.TagID = BookmarkTag.TagID');
  24. return $query;
  25. }

Controller:

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class Welcome extends CI_Controller {
  3. public function index()
  4. {
  5. // Load URL Helper
  6. $this->load->helper('url');
  7. // Load User Library
  8. $this->load->library('ion_auth');
  9. // Is User Logged In
  10. if ($this->ion_auth->logged_in())
  11. {
  12. $data['user'] = $this->ion_auth->get_user_array();
  13. }
  14. else
  15. {
  16. redirect('auth/login');
  17. }
  18. // Load Bookmark Model
  19. $this->load->model('Bookmark_model');
  20. // Create Arrays
  21. $bookmarks = array();
  22. $tags = array();
  23. // Query Database
  24. $query = $this->Bookmark_model->get_latest_bookmarks(4);
  25. //
  26. foreach ($query->result() as $row) {
  27. array_push($tags, $this->Bookmark_model->get_bookmark_tags($row->BookmarkID));
  28. array_push($bookmarks, $row);
  29. }
  30. $data['tags_latest'] = $tags;
  31. $data['bookmarks_latest'] = $bookmarks;
  32. $this->load->view('welcome_message', $data);
  33. }
  34. }

View:

  1. <h1>Latest Bookmarks</h1>
  2. <?php foreach ($bookmarks_latest as $bookmark): ?>
  3. <?php print_r($bookmark); ?>
  4. <?php print_r($tags_latest->result()); ?>
  5. <?php endforeach; ?>

展开全部

  • 写回答

2条回答 默认 最新

  • dtvjl64442 2011-06-10 16:53
    关注

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

    1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    2. class Welcome extends CI_Controller {
    3. public function index()
    4. {
    5. // Load Model
    6. $this->load->model('Bookmarks');
    7. // Get Latest Bookmarks
    8. $query = $this->Bookmarks->get_latest_bookmarks(4);
    9. $bookmarks = array();
    10. $tags = array();
    11. foreach ($query->result() as $row) {
    12. $bookmark_query = $this->Bookmarks->get_bookmark_tags($row->id);
    13. $bookmark_arr = array();
    14. foreach (bookmark_query->result() as $bookm) {
    15. array_push($bookmark_arr, $bookm);
    16. }
    17. array_push($tags, $bookmark_arr);
    18. array_push($bookmarks, $row);
    19. }
    20. $data['tags'] = $tags;
    21. $data['bookmarks'] = $bookmarks;
    22. // Load and Pass Data into View
    23. $this->load->view('welcome_message', $data);
    24. }
    25. }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部