Welcome Guest, Not a member yet? Register   Sign In
Insert value JSON with foreach?
#1

[eluser]SaSa[/eluser]
I want insert this value JSON to database, but with following PHP code it insert just [ in table. how can fix this problem?
-- i use mysql database and "var_dump($start_date,$percent);" output is as: 'string(31) "["2012\/08\/07","2011\/10\/29"]" array(1) { [0]=> string(2) "56" } ["2012\/08\/07","2011\/10\/29"]'

Html code:

Code:
<input name="start_date[]" value="2012/08/07">
<input name="start_date[]" value="2011/10/29">
<input name="percent[]" value="56">

This value JSON: ["2012\/08\/07","2011\/10\/29"]

PHP code:
Code:
$start_date         = json_encode($this->input->post('start_date'));
    $percent       = $this->input->post('percent');

            print_r($start_date); // This output is as: ["2012\/08\/07","2011\/10\/29"]

            $data = array();
            foreach ($percent as $idx => $name) {
                $data[] = array(
                    'start_date' => $start_date[$idx],
                    'price_change' => $percent[$idx]
                );
            };                
            $this->db->insert_batch('table', $data);




Theme © iAndrew 2016 - Forum software by © MyBB