【问题标题】:codeigniter passing data from model to controller or any alternativecodeigniter 将数据从模型传递到控制器或任何替代方案
【发布时间】:2014-04-08 14:54:03
【问题描述】:

1.大家好,我是 codeigniter 的新手,请帮助我想将数据从 id 传递到控制器到模型

    public function itemdetails(){

            $id = $_REQUEST['id'];

            $this->db->WHERE('assetTag', $id);
            $query = $this->db->get('mis_inventory');

            foreach ($query->result_array() as $row);
            echo "<table border='1'>";
            echo 'ASSET TAG';
            echo strtoupper($row['assetTag']);
            echo 'ITEM TYPE';
            echo $row['itemType'];
            echo 'BRAND';
            echo $row['brand'];
            echo 'MODEL';
            echo $row['model'];
            echo 'SERIAL';
            echo $row['serial'];


           echo anchor('main/update_item?id='.  ucwords($row['assetTag']), 'EDIT');

2.然后当我点击编辑时,它将重定向到下面的控制器代码

            public function update_item(){
                if ($this->session->userdata('is_logged_in')){
                    $this->load->model('model_items');
                    $this->load->view('update');
                }
            }

3.然后返回模型

        public function update_item(){


            $id = $_REQUEST['id'];

            $this->db->WHERE('assetTag', $id);
            $query = $this->db->get('mis_inventory');

            foreach ($query->result_array() as $row);

            echo form_open('main/update_validation');
            echo "<table border='1'>";
            echo 'ASSET TAG';
            echo strtoupper($row['assetTag']);
            echo 'ITEM TYPE';
            echo form_input('itemType', $row['itemType']);
            echo 'BRAND';
            echo form_input('itemType', $row['brand']);
            echo 'MODEL';
             echo form_input('itemType', $row['model']);
            echo 'SERIAL';

            echo form_submit('submit', 'UPDATE');
            echo validation_errors();

4.更新验证码

            public function update_validation(){

                $this->load->library('form_validation');

                $this->load->model('model_items');
                $id = $_REQUEST['id'];

                $this->form_validation->set_rules('itemType', 'itemtype', 'required');
                $this->form_validation->set_rules('brand', 'brand', 'required');
                $this->form_validation->set_rules('model', 'model', 'required');
                $this->form_validation->set_rules('serial', 'serial', 'required');
                $this->form_validation->run();
                $this->model_items->can_update_item();

                echo $this->db->affected_rows().' record updated';

/** 我只想编辑数据库中的项目,当我单击它的 ID 时,该值应该在 TAG 表单内,然后将编辑一些内容并单击更新按钮,但它失败了。 我非常需要帮助。 谢谢**/

【问题讨论】:

  • 1.你的foreach 什么都不做,你在它之后添加了一个;。 2. 显示的错误是什么? 3.如果你想传递任何东西给模型的函数,只需调用你的函数$this-&gt;model-&gt;example($id);
  • 遇到 PHP 错误严重性:通知消息:未定义索引:id 文件名:controllers/main.php 行号:158 遇到 PHP 错误严重性:警告消息:缺少 Model_items 的参数 1:: can_update_item(),在第 175 行的 C:\xampp\htdocs\code\application\controllers\main.php 中调用并定义文件名:models/model_items.php 行号:26 遇到 PHP 错误严重性:通知消息:未定义索引: id 文件名:models/model_items.php 行号:28
  • 感谢您的回复我对管理模型和控制器有点困惑,我会尝试 $this->model->example($id);

标签: codeigniter pass-data


【解决方案1】:

这是它的工作原理:

对于控制器

public function my_url()
{
    $userInput['id'] = $this->input->post('id');  //It is a safe $_POST['id'] (XSS Injection safe, so use this instead of $_POST)
    $userInput['email'] = $this->input->post('email');

    $this->load->model('user_model');
    $result = $this->user_model->create_user($userInput);
    echo $result; 
    //Or you can do :
    //$data['result'] = $this->user_model->create_user($userInput);
    //$this->load->view('result_view',$data);
}

对于模型

public function create_user($userInput)
{
    //Check for the required field etc
    if(!isset($userInput['email'])
        return false;

    $this->db->insert('my_user_table',$userInput);

    return $this->db->affected_rows();  //Should return 1 if user is created else 0
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    相关资源
    最近更新 更多