CodeIgniter Forums

Full Version: Error Array to string conversion
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Im gonna filtering recored from mysql database using dropdown. and when i run the code, i got some error like this : 

A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: models/NewTim_Model.php
Line Number: 13

and this is my model :

if  (!defined('BASEPATH')) exit('No direct script access allowed');
class NewTim_model extends CI_Model {

public function getTim(){
$query = $this->db->get('tim');
if($query->num_rows() > 0){
return $query->result();

public function getRecords($timid){
  $sql = "SELECT karyawan.* FROM tim INNER JOIN karyawan ON tim.tim_id=karyawan.tim_id WHERE tim.tim_id='$timid'";
        $d = $this->db->query($sql);
        //$data = $d->result();
        return $data;

my controller :

PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

Tim extends CI_Controller {
    public function __construct() {
    public function index()
        $getTim $this->NewTim_Model->getTim();
        $this->load->view('Tim/tim_list', ['getTim'=> $getTim]);

    public function getRecords(){
        $anggotaTim $this->input->post('tim_id');
        //$getAnggota = $this->NewKaryawan_model->getNamaKaryawan();
        $records $this->NewTim_Model->getRecords($getTim);
        $this->load->view('Tim/tim_list', ['getTim' => $getTim'records' => $records]);


my view

<?php $this->load->view('templates/header_manajer');?>
            <div class="row" style="margin-bottom: 10px">
            <div class="col-md-4">
                <h2 style="margin-top:10px">Data Tim</h2>
            <div class="col-md-4 text-right">
                <?php echo anchor(site_url('Tim/create'), 'Tambah', 'class="btn btn-primary"'); ?>
                <div class="panel-body">
                    <table class="table table-bordered table-striped" id="mytable">
                        <?php echo form_open('Tim/getRecords')?>
                            <td> ID</td>
                                <select class="form-control" name="tim_id" id="tim_id"><br>
                                <?php if(count($getTim)):?>

                                <?php foreach ($getTim as $tim): ?>
                                <option value="<?php echo $tim->tim_id;?>"> <?php echo $tim->tim_id;?> </option>
                            <?php endforeach;?>
                        <?php else:?>
                        <?php endif;?>

                        <tr><td><?php echo form_submit(['name'=>'submit', 'value'=>'Records'])?></td></tr>
                        <?php echo form_close()?>

                            <td> Anggota</td>
                            <?php if(count($records)): ?>

                            <?php foreach($records as $rec):


                            <td><?php echo $rec->kr_nama;?></td>
                             <tr><td><?php endforeach;?>  </td></tr>
                    <?php else:?>
                    <tr><td>No record founds!</td></tr>
                <?php endif;?>
                         <tr><td> Action</td><td><a href="<?php echo base_url()."index.php/Tim/getTimID/".$i['tim_id']; ?>" class="btn btn-default btn-sm">Ubah</a> | <a href="<?php echo base_url()."index.php/Tim/deleteTim/".$i['tim_id']; ?>" class="btn btn-default btn-sm">Hapus</a> </td> </tr>

<?php $this->load->view('templates/footer'); ?>

and my table on the attachment

please tell me what sholud i do with this code, thank youSmile
You are passing in an array. But it expects a single value (string).

PHP Code:
$records $this->NewTim_Model->getRecords($getTim); 

PHP Code:
public function getRecords($timid){ 

Also, you should not write your SQL like that. You can be a subject of SQL injections.