开发者

create table from the post data array in php

I need help here in my practice program. I have a json array:

myData=[{"plank_number":"1","thickness":"5","width":"7","length_t":"8","quantity":"1"},
 {"plank开发者_高级运维_number":"2","thickness":"5","width":"6","length_t":"7","quantity":"1"},
 {"plank_number":"3","thickness":"6","width":"7","length_t":"8","quantity":"1"},
 .........................(could be more)]

this array is generated everytime you add plank in my grid (jqgrid). I also have here a php file with some html tag. What I am confused about is how will I code my .php program so that it will accept my variable myData and create another table in from it.

EDIT

My questions are: How to POST a JSON array to an other page? and,

How to output the JSON array in an html table? I have a submit button in here in my html file. And my variable myData is from my javascript file. Thanks


You have to use json_decode to convert the post data into a php associative array then you can loop through it and generate the table. Something along the lines of....

<?PHP
$data = json_decode($_POST['myData'], true); // convert into a php array
$numrows = count($data); // count the number of rows you need

// generate the table
echo "<table>";
echo "<tr><td>plank number</td><td>thickness</td><td>width</td></tr>"; // your headings
for($i = 0; $i < $numrows; $i++)
{
    echo "<tr>";
    echo "<td>" . $data[$i]['plank_number'] . "</td>";
    echo "<td>" . $data[$i]['thickness'] . "</td>";
    echo "<td>" . $data[$i]['width'] . "</td>";
    echo "</tr>";
}
echo "</table>";
?>

http://php.net/manual/en/function.json-decode.php


Not sure to understand your problem, but what about using "json_decode" PHP function ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜