wahyueka31
wahyueka31

Reputation: 672

Codeigniter Parsing Variable from Controller to View - Message: Array to string conversion

I have this function in my controller :

public function simpan() {
    $this->load->library('form_validation');
    // field name, error message, validation rules
    $this->form_validation->set_rules('name', 'User Name', 'trim|required|min_length[4]|xss_clean');
    $this->form_validation->set_rules('email', 'Your Email', 'trim|required|valid_email');
    $this->form_validation->set_rules('message', 'Your Message', 'trim|required');

    if ($this->form_validation->run() == FALSE) {
        $data['msg_save'] = "data not saved";
        $this->load->view("v_bukutamu, $data");
    } else {
        $this->m_bukutamu->simpan();
        $data['msg_save'] = "data saved";
        $this->load->view("v_bukutamu, $data");
    }
}

And this line to parsing varible $msg_save in View :

<p><?php if (isset($msg_save)) echo $msg_save; ?></p>

The data was successful saved to database, but it's give me this error message :

Message: Array to string conversion

I just want to show the successful or failed message with the same view. Does anyone have any idea what should I change in my script?

Upvotes: 0

Views: 2672

Answers (3)

user621394
user621394

Reputation:

The Correct way of passing variable on views page is given below

$this->load->view("message", $data);

The problem in your code was that you were using it in

$this->load->view("v_bukutamu, $data");

this way whereas you should use it in way given below

$this->load->view("v_bukutamu", $data);

Upvotes: 2

The Odd Developer
The Odd Developer

Reputation: 929

Simply change the

$this->load->view("v_bukutamu, $data");

To

$this->load->view('v_bukutamu', $data);

And use single quotes instead of double quotes

Upvotes: 1

Nouphal.M
Nouphal.M

Reputation: 6344

You should use

$this->load->view("v_bukutamu", $data);

instead of

$this->load->view("v_bukutamu, $data");

To load a particular view we use $this->load->view('name'); where name is the name of the view file without php extension.

Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading function. eg :

$data = array(
               'title' => 'My Title',
               'heading' => 'My Heading',
               'message' => 'My Message'
          );

$this->load->view('blogview', $data);

more info here

Upvotes: 2

Related Questions