Welcome Guest, Not a member yet? Register   Sign In
PHP json_encode and insert data to database
#1

[eluser]SaSa[/eluser]
I want to insert all the information in one field of myself database table by json_encode, this is my information(all information are related. It is not clear that the user registration several html. it is dynamic):
Example: http://jsfiddle.net/AQPwv/2/
Code:
<input name="name_r[1][]" value="hi">
<p>
    &lt;input name="units[1][]" value="how"&gt;
    &lt;input type="text" name="units[1][]" value="2" style="width:20px;"&gt;
    &lt;input type="text" name="units[1][]" value="256314" style="width:65px;"&gt;
    &lt;input name="units[1][]" value="fine"&gt;
    &lt;input type="text" name="units[1][]" value="4" style="width:20px;"&gt;
    &lt;input type="text" name="units[1][]" value="854621" style="width:65px;"&gt;
    <p>
        &lt;input type="text" name="price_change[1][]" value="21466" style="width:75px;"&gt;
        &lt;input type="text" name="price_change[1][]" value="54219" style="width:75px;"&gt;
        &lt;input type="text" name="price_change[1][]" value="48752" style="width:65px;"&gt;
        &lt;input type="text" name="price_change[1][]" value="86541" style="width:75px;"&gt;
        &lt;input type="text" name="price_change[1][]" value="47276" style="width:75px;"&gt;
        &lt;input type="text" name="price_change[1][]" value="74538" style="width:65px;"&gt;
        <p>
            <hr>
<p>
    &lt;input name="name_r[2][]" value="hello"&gt;
    <p>
        &lt;input name="units[2][]" value="Library"&gt;
        &lt;input type="text" name="units[2][]" value="5" style="width:20px;"&gt;
        &lt;input type="text" name="units[2][]" value="95641" style="width:65px;"&gt;
        &lt;input name="units[2][]" value="khobee"&gt;
        &lt;input type="text" name="units[2][]" value="6" style="width:20px;"&gt;
        &lt;input type="text" name="units[2][]" value="84527" style="width:65px;"&gt;
        &lt;input name="units[2][]" value="PowerTools "&gt;
        &lt;input type="text" name="units[2][]" value="7" style="width:20px;"&gt;
        &lt;input type="text" name="units[2][]" value="75462" style="width:65px;"&gt;
        <p>
            &lt;input type="text" name="price_change[2][]" value="8457" style="width:75px;"&gt;
            &lt;input type="text" name="price_change[2][]" value="134" style="width:75px;"&gt;
            &lt;input type="text" name="price_change[2][]" value="76144" style="width:65px;"&gt;
            &lt;input type="text" name="price_change[2][]" value="956414" style="width:75px;"&gt;
            &lt;input type="text" name="price_change[2][]" value="7546" style="width:75px;"&gt;
            &lt;input type="text" name="price_change[2][]" value="123" style="width:65px;"&gt;
            &lt;input type="text" name="price_change[2][]" value="84541" style="width:75px;"&gt;
            &lt;input type="text" name="price_change[2][]" value="654" style="width:75px;"&gt;
            &lt;input type="text" name="price_change[2][]" value="8165" style="width:65px;"&gt;
            &lt;input type="text" name="price_change[2][]" value="2145" style="width:75px;"&gt;
            &lt;input type="text" name="price_change[2][]" value="354" style="width:75px;"&gt;
            &lt;input type="text" name="price_change[2][]" value="4774" style="width:65px;"&gt;
I would like to have in the database:
Code:
[{
    "name_r": "hi",
    "units": ["how", "2", "256314"],["fine", "4", "854621"],
    "price_change": ["21466", "54219", "48752"],["86541", "47276", "74538"],
}, {
    "name_r": "hello",
    "units": ["Library", "5", "95641"],["khobee", "6", "84527"],["PowerTools ", "7", "75462"],
    "price_change": ["8457", "134", "76144"],["956414", "7546", "123"],["84541", "654", "8165"],["2145", "354", "4774"]
}]

i use of this PHP code:

Code:
$name_r=$this->input->post('name_r');
$units=$this->input->post('units');
$price_change=$this->input->post('price_change');

foreach($name_r as $key => $value)
{
    $arr[]=array(
     'name_r'=>$value['0'],
     'units'=>$units[$key],
     'price_change'=>$price_change[$key]
    );
}

$json=json_encode($arr);

but this php code inserted as(i not want this):

Code:
[{
    "name_r": "hi",
    "units": ["how", "2", "256314", "fine", "4", "854621"],
    "price_change": ["21466", "54219", "48752", "86541", "47276", "74538"]
},

{
    "name_r": "hello",
    "units": ["Library", "5", "95641", "khobee", "6", "84527", "PowerTools ", "7", "75462"],
    "price_change": ["8457", "134", "76144", "956414", "7546", "123", "84541", "654", "8165", "2145", "354", "4774"]
}]
How is it and what do i do?
With respect




Theme © iAndrew 2016 - Forum software by © MyBB