robins
robins

Reputation: 1668

Count post data in a CodeIgniter controller

I have five text fields that are not mandatory. Sometimes enter data in one field, sometimes 2 or 3 or 4 or 5. I have to count this data:

     $sib1=$this->input->post(sib1);
     $sib2=$this->input->post(sib2);
     $sib3=$this->input->post(sib3);

Like this, I want to count this. Now 3

Upvotes: 0

Views: 1307

Answers (2)

Linus
Linus

Reputation: 909

this is way to do it:

Take an array $arr=array();



 $arr['sib1']=$this->input->post("sib1",true);

    $arr['sib2']=$this->input->post("sib2",true);

    $arr['sib3']=$this->input->post("sib3",true);

    $arr['count']=count($this->input->post($arr)); //this will count your post
  $result = $this->Model_name->model_function($arr);

In Model

function model_function($arr)
{

    $this->db->insert('tbl',$arr);

    if ($this->db->affected_rows() > 0) {

    return true;

    } else {
    return false;
    }

Upvotes: 0

Jan
Jan

Reputation: 43199

You can use a for loop with length = number of text fields. Put it to the database afterwards. sibcount is the number of filled entries.

$data = array();
$length = 5;
$sibcount = 0;
for ($i=0;$i<$length;$i++) {
    $entry = $this->input->post("sib".$i);
    if (!empty($entry)) {
        $data["sib".$i] = $entry;
        $sibcount++;
    }
}
$data["sibcount"] = $sibcount;
// use in controller or model
if (!empty($data))
     $this->db->insert('mytable', $data); 

Upvotes: 1

Related Questions