0

i have a array as json.stringify object. the array is from html table. i have made the array and it made well. i use the code below for create the array and try to getting the array for inserting into my database. i use ajax call function in there

$(document).on('click','#display_data',function(e){
    var convertTableToJson = function()
        {
            var rows = [];
            $('.table-bordered tr').each(function(i, n){
                var $row = $(n);
                rows.push([
                    $row.find('td:eq(0)').text(),
                    $row.find('td:eq(1)').text(),
                    $row.find('td:eq(2)').text(),
                    $row.find('td:eq(3)').text(),
                    $row.find('td:eq(4)').text(),
                    $row.find('td:eq(5)').text(),
                    $row.find('td:eq(6)').text(),
                    $row.find('td:eq(7)').text(),
                ]);
            });
            return JSON.stringify(rows);
        };
	$.ajax({
			data:convertTableToJson,
			type:"POST",
			url:"../php/tagihan/save_data.php",
			success: function(data){
				alert ("Data:" + data);
			}
		});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js"></script>
<table class="table table-bordered">
<thead>
<tr>
<th>Kode Material</th>
<th>Storage Location</th>
<th>Movement Type</th>
<th>Id Indentifier</th>
<th>Item</th>
<th>Date Input</th>
<th>Netto</th>
<th>Unit</th>
<th><input type="checkbox" id="mycheckbox1" name="mycheckbox1"></th>
</tr>
</thead>
<tbody>
<tr>
<td>101200</td>
<td>WCB</td>
<td>101</td>
<td>5006540050</td>
<td>1</td>
<td>10.08.2017</td>
<td>23.970</td>
<td>KG</td>
<td><input type="checkbox" id="mycheckbox" name="mycheckbox"></td>
</tr>
<tr>
<td>101200</td>
<td>WCB</td>
<td>101</td>
<td>5006539985</td>
<td>1</td>
<td>10.08.2017</td>
<td>42.970</td>
<td>KG</td>
<td><input type="checkbox" id="mycheckbox" name="mycheckbox"></td>
</tr>
</tbody>
</table>
<input type="button" id="display_data" name="display_data" />

i don't know why. when i try to run this code with this php function i get an error say's invalid argument of foreach(). and i think the syntax of the foreach that i use is right the php function that i use for insert the data just like the code below

<?php
include('../../Connections/koneksi.php');

// reading json file
$array = json_decode($_POST['convertTableToJson']);
$data = json_decode($array, true);
foreach ($data as $user) {
    $kode = $user[0];
    $sloc = $user[1];
    $move = $user[2];
    $id = $user[3];
    $date = $user[4];
    $netto = $user[5];
    $unit = $user[6];
    $payroll = $user[7];

    // preparing statement for insert query
    $st = mysqli_prepare($db, 'INSERT INTO wjm(kode, sloc, move, id, date, netto, unit, payroll) VALUES (?, ?, ?, ?, ?, ?, ?, ?)');

    // bind variables to insert query params
    mysqli_stmt_bind_param($st, 'ssssssss', $kode, $sloc, $move, $id, $date, $netto, $unit, $payroll);

    // executing insert query
    mysqli_stmt_execute($st);
}

?>

please someone help me to solve this.

1
  • You're using json_decode twice; this might be your problem Commented Aug 19, 2017 at 13:32

3 Answers 3

1

If you want to obtain the values of a foreach it is necessary to obtain in foreach the key value

foreach ($data as $key => $value) {
    $kode       = $value[0];
    $sloc       = $value[1];
    $move       = $value[2];
    $id         = $value[3];
    $date       = $value[4];
    $netto      = $value[5];
    $unit       = $value[6];
    $payroll    = $value[7];

    ...
}

More info: http://php.net/manual/en/control-structures.foreach.php

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

Comments

0

did you make sure $array = json_decode($_POST['convertTableToJson']); getting the array object?

1 Comment

use comments to requests clarifications. answers are for ... well answers.
0

After you fix the syntax error with @Raul code, you will run into pragmatic error. Is should be writing with an if/else statement inside a for loop. so what you need is a for loop to avoid duplicates as in the case of @Raul code.

for ($x = 0; $x < count($data); $x++) {
    if($x == 0) {
        $kode = $data[0];
    } else if($x == 1) {
        $sloc = $data[1];
    } else if($x == 2) {
        $move = $data[2];
    } else if($x == 3) {
        $id = $data[3];
    } else if($x == 4) {
        $date = $data[4];
    } else if($x == 5) {
        $netto = $data[5];
    } else if($x == 6) {
        $unit = $data[6];
    } else if($x == 7) {
        $payroll = $data[7];
    } else { // do nothing }
}

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.