0

1.hi guys i'm new to codeigniter please help i want to pass data from id to controller to model

    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.Then when i click on EDIT it will redirect to controller code below

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

3.then goes back to model

        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.update validation code

            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';

/** i just want to edit items on database when i click on it's ID the value should be inside the form TAG and then will edit some content and click update button but it failed. i badly need help please. thanks **/

  • 1. Your `foreach` do not do anything, you added a `;` just after it. 2. What is the error displayed ? 3. If you want to pass anything to a model's function, just call your function `$this->model->example($id);` – Kalzem Mar 06 '14 at 08:07
  • A PHP Error was encountered Severity: Notice Message: Undefined index: id Filename: controllers/main.php Line Number: 158 A PHP Error was encountered Severity: Warning Message: Missing argument 1 for Model_items::can_update_item(), called in C:\xampp\htdocs\code\application\controllers\main.php on line 175 and defined Filename: models/model_items.php Line Number: 26 A PHP Error was encountered Severity: Notice Message: Undefined index: id Filename: models/model_items.php Line Number: 28 – user3386671 Mar 06 '14 at 09:20
  • thank you for your reply i'm a little confused of managing models and controllers, i will try $this->model->example($id); – user3386671 Mar 06 '14 at 09:21

1 Answers1

0

Here is how it should work :

For the controller

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

For the model

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
}
Kalzem
  • 7,320
  • 6
  • 54
  • 79