-3

I use codeigniter.how can insert this values(in the html code) to a row of database by json_encode?

Demo: http://jsfiddle.net/AQPwv/3/

<input name="name_r[0][]" value="hi">
<p>
    <input name="units[1][]" value="how">
    <input type="text" name="units[1][]" value="2" style="width:20px;">
    <input type="text" name="units[1][]" value="256314" style="width:65px;">
    <input name="units[1][]" value="fine">
    <input type="text" name="units[1][]" value="4" style="width:20px;">
    <input type="text" name="units[1][]" value="854621" style="width:65px;">
    <p>
        <input type="text" name="price_change[2][]" value="21466" style="width:75px;">
        <input type="text" name="price_change[2][]" value="54219" style="width:75px;">
        <input type="text" name="price_change[2][]" value="48752" style="width:65px;">
        <input type="text" name="price_change[2][]" value="86541" style="width:75px;">
        <input type="text" name="price_change[2][]" value="47276" style="width:75px;">
        <input type="text" name="price_change[2][]" value="74538" style="width:65px;">
        <p>
            <hr>
<p>
    <input name="name_r[0][]" value="hello">
    <p>
        <input name="units[1][]" value="Library">
        <input type="text" name="units[1][]" value="5" style="width:20px;">
        <input type="text" name="units[1][]" value="95641" style="width:65px;">
        <input name="units[1][]" value="khobee">
        <input type="text" name="units[1][]" value="6" style="width:20px;">
        <input type="text" name="units[1][]" value="84527" style="width:65px;">
        <input name="units[1][]" value="PowerTools ">
        <input type="text" name="units[1][]" value="7" style="width:20px;">
        <input type="text" name="units[1][]" value="75462" style="width:65px;">
        <p>
            <input type="text" name="price_change[2][]" value="8457" style="width:75px;">
            <input type="text" name="price_change[2][]" value="134" style="width:75px;">
            <input type="text" name="price_change[2][]" value="76144" style="width:65px;">
            <input type="text" name="price_change[2][]" value="956414" style="width:75px;">
            <input type="text" name="price_change[2][]" value="7546" style="width:75px;">
            <input type="text" name="price_change[2][]" value="123" style="width:65px;">
            <input type="text" name="price_change[2][]" value="84541" style="width:75px;">
            <input type="text" name="price_change[2][]" value="654" style="width:75px;">
            <input type="text" name="price_change[2][]" value="8165" style="width:65px;">
            <input type="text" name="price_change[2][]" value="2145" style="width:75px;">
            <input type="text" name="price_change[2][]" value="354" style="width:75px;">
            <input type="text" name="price_change[2][]" value="4774" style="width:65px;">

Update: I want store data in the database table as:

[{
    "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"],
    "checkbox_units": ["8457", "134", "76144"],["956414", "7546", "123"],["84541", "654", "8165"],["2145", "354", "4774"]
}]

how is it?

5
  • 1
    Not sure if I follow your question. Why do you want to use json_encode? Do you want to insert all the information in one field of your database? Or do you want each piece of information in a different field? Commented Sep 17, 2011 at 16:25
  • If you see the html code find answer this Q(Why do you want to use json_encode?). i have 2 html simulation with different values. i want to insert all the information in one field of your database.What do you suggest? Commented Sep 17, 2011 at 16:34
  • I suggest you don't insert all information in one field. Commented Sep 17, 2011 at 16:47
  • if you want use database which support type of json, you can use MongoDB ( mongodb.org/display/DOCS/Introduction ) Commented Sep 17, 2011 at 16:52
  • @Alfwed all information are related. It is not clear that the user registration several html. it is dynamic. However,what is your say? Commented Sep 17, 2011 at 17:07

1 Answer 1

0

You have to change the markup for this, please follow the code below

$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);

// insert $json into your db table

Change markup to this

<input name="name_r[1][]" value="hi">
<p>
    <input name="units[1][]" value="how">
    <input type="text" name="units[1][]" value="2" style="width:20px;">
    <input type="text" name="units[1][]" value="256314" style="width:65px;">
    <input name="units[1][]" value="fine">
    <input type="text" name="units[1][]" value="4" style="width:20px;">
    <input type="text" name="units[1][]" value="854621" style="width:65px;">
    <p>
        <input type="text" name="price_change[1][]" value="21466" style="width:75px;">
        <input type="text" name="price_change[1][]" value="54219" style="width:75px;">
        <input type="text" name="price_change[1][]" value="48752" style="width:65px;">
        <input type="text" name="price_change[1][]" value="86541" style="width:75px;">
        <input type="text" name="price_change[1][]" value="47276" style="width:75px;">
        <input type="text" name="price_change[1][]" value="74538" style="width:65px;">
        <p>
            <hr>
<p>
    <input name="name_r[2][]" value="hello">
    <p>
        <input name="units[2][]" value="Library">
        <input type="text" name="units[2][]" value="5" style="width:20px;">
        <input type="text" name="units[2][]" value="95641" style="width:65px;">
        <input name="units[2][]" value="khobee">
        <input type="text" name="units[2][]" value="6" style="width:20px;">
        <input type="text" name="units[2][]" value="84527" style="width:65px;">
        <input name="units[2][]" value="PowerTools ">
        <input type="text" name="units[2][]" value="7" style="width:20px;">
        <input type="text" name="units[2][]" value="75462" style="width:65px;">
        <p>
            <input type="text" name="price_change[2][]" value="8457" style="width:75px;">
            <input type="text" name="price_change[2][]" value="134" style="width:75px;">
            <input type="text" name="price_change[2][]" value="76144" style="width:65px;">
            <input type="text" name="price_change[2][]" value="956414" style="width:75px;">
            <input type="text" name="price_change[2][]" value="7546" style="width:75px;">
            <input type="text" name="price_change[2][]" value="123" style="width:65px;">
            <input type="text" name="price_change[2][]" value="84541" style="width:75px;">
            <input type="text" name="price_change[2][]" value="654" style="width:75px;">
            <input type="text" name="price_change[2][]" value="8165" style="width:65px;">
            <input type="text" name="price_change[2][]" value="2145" style="width:75px;">
            <input type="text" name="price_change[2][]" value="354" style="width:75px;">
            <input type="text" name="price_change[2][]" value="4774" style="width:65px;">

Here you get the JSON string below

[
{"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"]}
]
3
  • How is for <input name="name_r[0][]" value="hi">? did nos use of foreach? Commented Sep 17, 2011 at 16:29
  • for units i want this:"units": ["how", "2", "256314"],["fine", "4", "854621"], and for price_change this: "price_change": ["21466", "54219", "48752"],["86541", "47276", "74538"], ... no "units":["how","2","256314","fine","4","854621"],"price_change":["21466","54219","48752","86541","47276","74538"]}, Commented Sep 17, 2011 at 17:54
  • did should use of other function(for this format) Instead of json_encode? Commented Sep 18, 2011 at 13:30

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.