0

I create a project using codeigniter now. But, I got an error message "PHP Error wasArray to string conversion". What's wrong? Thanks before...

enter image description here

This is my controller:

public function index($page = 'dashboard') {
    $data['num_rows'] = $this->admin_produk_model->count_product();

    $this->load->view('admin/templates/header', $data);
    $this->load->view('admin/templates/sidebar', $data);
    $this->load->view('admin/pages/' . $page, $data);
    $this->load->view('admin/templates/footer', $data);

}

My model:

public function count_product() {
    $this->db->select('*')->from('produk');
    $q = $this->db->get();
    return $q->num_rows();
}

My view:

<span class="info-box-number"><?php echo ['num_rows'];?></span>
2
  • 1
    ['num_rows'] is an array. You should be using the set view variable. In this case, it's probably $num_rows. Commented Jun 13, 2015 at 19:10
  • In view $data['num_rows'] become $num_row. But it is array too according to your model Commented Jun 13, 2015 at 19:12

2 Answers 2

1

$q->num_rows() in you model returns an object and You are Passing that object to view correctly, now you can simply use $num_rows in your view file to show it.

<span class="info-box-number"><?php echo $num_rows;?></span>

or

<span class="info-box-number"><?=$num_rows;?></span>
Sign up to request clarification or add additional context in comments.

1 Comment

I agree with this answer. The problem is in your view. You are passing the information correctly from model to controller to view, but you are not using the right syntax in your view.
0

When you send the datas in CodeIgniter using $data variable, you can directly access the datas using the keyname as variable name i.e

 $data['num_rows'] = $this->admin_produk_model->count_product();

Then in your view page access it like $num_rows.

You are getting that error because you are trying to echo an array. You have to use print_r() function to print it

So Instead of this

 <span class="info-box-number"><?php echo ['num_rows'];?></span>

Do this

 <span class="info-box-number"><?php print_r($num_rows);?></span>

1 Comment

they can also use echo $num_rows , print_r is not necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.