0

I am a student who learns CodeIgniter, I have a school assignment about the database, I create a project, and run query in my model just like this.

class Send_model extends CI_Model{        
   function hello(){ $table = $this->db->query("SELECT * FROM (`tbl1`) LEFT JOIN `tbl2` ON `tbl2`.`id` = `tbl1`.`child_id` LEFT JOIN `tbl3` ON `tbl3`.`id` = `tbl1`.`child_id` ");
    foreach($table->result() as  $row){
    $modbus[]= $row->modbus;
    $data []= $row->data;
    $alert[]= $row->alert;
    };
        $param0 = '&'.$modbus[0].'='.$data[0].':'.$alert[0]; 
        $param1 = '&'.$modbus[1].'='.$data[1].':'.$alert[1];
        $param2 = '&'.$modbus[2].'='.$data[2].':'.$alert[2];
        $param3 = '&'.$modbus[3].'='.$data[3].':'.$alert[3];
        $param4 = '&'.$modbus[4].'='.$data[4].':'.$alert[4];
        $param5 = '&'.$modbus[5].'='.$data[5].':'.$alert[5];
        $param6 = '&'.$modbus[6].'='.$data[6].':'.$alert[6];
        $param7 = '&'.$modbus[7].'='.$data[7].':'.$alert[7];
        $param8 = '&'.$modbus[8].'='.$data[8].':'.$alert[8];
        $param9 = '&'.$modbus[9].'='.$data[9].':'.$alert[9];
        $param10 = '&'.$modbus[10].'='.$data[10].':'.$alert[10];
        $param11= '&'.$modbus[11].'='.$data[11].':'.$alert[11];
        $param12 = '&'.$modbus[12].'='.$data[12].':'.$alert[12];
        $param13 = '&'.$modbus[13].'='.$data[13].':'.$alert[13];
        $param14 = '&'.$modbus[14].'='.$data[14].':'.$alert[14];
        $param15 = '&'.$modbus[15].'='.$data[15].':'.$alert[15];
        $param16 = '&'.$modbus[16].'='.$data[16].':'.$alert[16];
        $param17 = '&'.$modbus[17].'='.$data[17].':'.$alert[17];
        $param18 = '&'.$modbus[18].'='.$data[18].':'.$alert[18];
    $sent = $param0.$param1.$param3.$param4.$param5.$param6.$param7.$param8.$param9.$param10.$param11.$param12.$param13.$param13.$param14.$param15.$param16.$param17;

    return $sent;
}

my controller just like this

class Send extend CI_Controller{
function data ()
{ 
$this->load->model('send_model');
$send = $this->model->send_model->hello();
echo $sent;
}
}
  • i have a problem,if tbl1 add data then i have to write adding code to my script
  • can anyone help me to simplify this code?

1 Answer 1

3

It is because you're hard-coding to send only 19 rows of data by creating 19 $param variables. In a dynamic condition, you may have an empty record-set or hundreds of records. Modify the structure of your foreach loop to the following:

$param = array();
foreach($table->result() as  $row){
$param[] = '&'.$row->modbus.'='.$row->data.':'.$row->alert;
} 
return $param;

Hope it answers your question.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your answer, i modify my script as your suggestion, but i have an error result "array",when i call my controller in a browser
It is because you're using echo to display the array. Use var_dump to check if the array is returning values and then you can apply your logic of displaying the array the way you want to. PS: please mark my answer by clicking on the tick mark next to it. Thank you.
finally it works, and learn much from you , i'm very grateful to you

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.