1

I want insert following date values as json value in database mysql, as following example:

I want they as in database table row:

     Column static  |                     Column data_1
Row1:      12       |  ["1111111111", "2222222222", "3333333333", "4444444444"]
Row2:      34       |  ["5555555555", "6666666666", "7777777777"]
Row1:      56       |  ["8888888888", "9999999999"]

DEMO: http://codepad.viper-7.com/WzGz3p

<form method="post">
<input name="static[]" value="12">
<input name="data_1[]" value="1111111111">
<input name="data_1[]" value="2222222222">
<input name="data_1[]" value="3333333333">
<input name="data_1[]" value="4444444444">

<input name="static[]" value="34">
<input name="data_1[]" value="5555555555">
<input name="data_1[]" value="6666666666">
<input name="data_1[]" value="7777777777">

<input name="static[]" value="56">
<input name="data_1[]" value="8888888888">
<input name="data_1[]" value="9999999999">

<input type="submit">
</form>

My php code that don't work true:

<?php
$data = array();
$data_1 = $_POST['data_1'];
$static = $_POST["static"];
foreach($static as $idx=>$val){
    $data[] = array(
              'static' => $static[$idx],
              'data_1' => json_encode(Array($data_1[$idx*2],$data_1[$idx*2+1]))             
                   );
}
//$this->db->insert_batch('MyTable', $data);

echo "<pre>";
print_r($data);
?>
2
  • What is NOT working? Can you tell us any error that is occurring or any similar symptoms? My php code that don't work true: doesn't make people understand your problem adequately Commented Jul 22, 2012 at 18:47
  • Are you sure you want the dates serialized in one column? Sounds like a database design issue. Commented Jul 22, 2012 at 18:47

1 Answer 1

2

I don't recommend this format data to store it in a database, but if you want to save in this way you must do a few changes:

Change your form:

you are working with non fixed number of fields, you can use an structure like:

<input name="static[NUMBER]" value="XXX">
<input name="data_NUMBER[1]" value="YYYYY">

and in php you can get the info of the static=NUMBER as $_POST["data_".NUMBER]

<form method="post">
<input name="static[1]" value="12">
<input name="data_1[0]" value="1111111111">
<input name="data_1[1]" value="2222222222">
<input name="data_1[2]" value="3333333333">
<input name="data_1[3]" value="4444444444">

<input name="static[2]" value="34">
<input name="data_2[0]" value="5555555555">
<input name="data_2[1]" value="6666666666">
<input name="data_2[2]" value="7777777777">

<input name="static[3]" value="56">
<input name="data_3[0]" value="8888888888">
<input name="data_3[1]" value="9999999999">

<input type="submit">
</form>

and change the php:

foreach($static as $idx=>$val){
    $data_=$_POST["data_".$idx];
    $data[] = array(
        'static' => $static[$idx],
        'data_1' => json_encode($data_)             
    );
}

http://codepad.viper-7.com/i2xvKC (demo here)

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

Comments

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.