如何使用CodeIgniter将字符串和数组值插入表格中

huangapple go评论76阅读模式
英文:

How to insert string and array values in table using codigniter

问题

我有一个多维数组,该多维数组有四个数组,每个数组都有两个字符串值,应该插入到表格的列名中。

但我的问题是,一个数组的四个数组中有两个数组,其中一个数组有两个字符串值,所以我的问题是如何插入多维数组的值。

Array
(
    [0] => Array
        (
            [stu_ans_id] => 1
            [stu_answer] => True
        )

    [1] => Array
        (
            [stu_ans_id] => 2
            [stu_answer] => B
        )

    [2] => Array
        (
            [stu_ans_id] => 3
            [stu_answer] => hi
        )

    [3] => Array
        (
            [stu_ans_id] => 4
            [stu_answer] => Array
                (
                    [2] => B
                    [3] => C
                )

        )

)

实际上,这四种类型的数组是测验问题的答案,前三个数组是文本框和单选按钮,但最后一个数组是复选框值,所以对我来说很复杂,请帮助我解决这个问题。

这是我的控制器:

public function add_stu_ans()
{
    $id = $values['stu_ans_id'] = $this->input->post('qstn_id');
    $ans = $values['stu_answer'] = $this->input->post('ans');

    $testArr = array_combine($id, $ans);

    $arr = [];

    foreach ($testArr as $key => $value) 
    {
        $arr[] = array('stu_ans_id' => $key, 'stu_answer' => $value);
    }

    $this->Common_model->insert_answer('student', $arr);
    $this->session->set_flashdata('message_name', 'Your Data is Inserted');
    redirect('Quiz/tot_marks');
}

这是我的模型:

public function insert_answer($table, $arr)
{
    $result = $this->db->insert_batch($table, $arr);
    return $this->db->affected_rows();
}
英文:

I have A multi-dimension array, that multi-dimension array has four arrays each array has two string values that should be inserted for table's column name.

But my problem is, four arrays of one array has two arrays, that has one array, that one array has two string values, so my question is how to insert the values of multi-dimension array

Array
(
    [0] => Array
        (
            [stu_ans_id] => 1
            [stu_answer] => True
        )

    [1] => Array
        (
            [stu_ans_id] => 2
            [stu_answer] => B
        )

    [2] => Array
        (
            [stu_ans_id] => 3
            [stu_answer] => hi
        )

    [3] => Array
        (
            [stu_ans_id] => 4
            [stu_answer] => Array
                (
                    [2] => B
                    [3] => C
                )

        )

)

actually this four type arrays are quiz question answers, First 3 arrays are textbox and radio buttons but that last array is checkbox value so it is complicated to me so please help to solve this issue

 public function add_stu_ans()

{

      $id = $values['stu_ans_id'] = $this->input->post('qstn_id');
      $ans =  $values['stu_answer']  =  $this->input->post('ans'); 

      $testArr=array_combine($id,$ans);

      $arr=[];
      
      foreach ($testArr as $key => $value) 
      {
        $arr[]=array('stu_ans_id'=>$key,'stu_answer'=>$value);
      }


        $this->Common_model->insert_answer('student',$arr);
        $this->session->set_flashdata('message_name' , 'Your Data is Inserted');
        redirect('Quiz/tot_marks');

}

It's my controller...

public function insert_answer($table, $arr)
	{

	    $result = $this->db->insert_batch($table, $arr);
	    return $this->db->affected_rows();

	}

this is my model

答案1

得分: 1

使用php中的json_encode函数将数组转换为字符串格式,并将其插入到数据库中。

英文:

Use json_encode php function to convert array into string format and insert it to Database.

huangapple
  • 本文由 发表于 2020年1月6日 21:00:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612588.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定