如何显示mysql Json数据

时间:2017-06-03 09:05:35

标签: php mysql json codeigniter

我在数据库电话购买中有一张表。我插入少量数据作为JOSN格式。在那个json formate中存储了多个数据。 exp-“products”有2个数据

Database Table Image

[{"products":["Asulak Silver 7","Print Gum MAP NEW"]},{"qnt":["2","1"]},{"unit_price":["1500","600"]},{"pack_size":["60","60"]},{"unit_pack":["2","1"]},{"packing":["Dram","Jar"]},{"total_kg":["120","60"]},{"total_price":["3000","600"]}]

我使用json_decode()函数,我得到了这个 -

Array
(
    [0] => stdClass Object
        (
            [products] => Array
                (
                    [0] => Asulak Silver 7
                    [1] => Print Gum MAP NEW
                )

        )

    [1] => stdClass Object
        (
            [qnt] => Array
                (
                    [0] => 2
                    [1] => 1
                )

        )

    [2] => stdClass Object
        (
            [unit_price] => Array
                (
                    [0] => 1500
                    [1] => 600
                )

        )

    [3] => stdClass Object
        (
            [pack_size] => Array
                (
                    [0] => 60
                    [1] => 60
                )

        )

    [4] => stdClass Object
        (
            [unit_pack] => Array
                (
                    [0] => 2
                    [1] => 1
                )

        )

    [5] => stdClass Object
        (
            [packing] => Array
                (
                    [0] => Dram
                    [1] => Jar
                )

        )

    [6] => stdClass Object
        (
            [total_kg] => Array
                (
                    [0] => 120
                    [1] => 60
                )

        )

    [7] => stdClass Object
        (
            [total_price] => Array
                (
                    [0] => 3000
                    [1] => 600
                )

        )

)

但我不知道如何在我的视图表格中显示 - enter image description here

我是Codeigniter的新手。请帮我提前谢谢你。

我已经使用了这段代码

$json = array(  array('products' => $products), 
                array('qnt' => $qnt), 
                array('unit_price' => $unit_price), 
                array('pack_size' => $pack_size), 
                array('unit_pack' => $unit_pack), 
                array('packing' => $packing), 
                array('total_kg' => $total_kg), 
                array('total_price' => $total_price) 
            ); 
$json_type = json_encode($json); //Sending arrays into json

1 个答案:

答案 0 :(得分:2)

将此代码用于数组重新范围:

$array = json_decode( $json, true );
$data=array();
foreach($array as $value){
    foreach($value as $key=>$value){
    }
    $data[$key]=$value;
}

对于视图使用此代码:

<table border="1">
<tr>
<td>Product Name</td>
<td>Quantity</td>
<td>unit_price</td>
<td>pack_size</td>
<td>unit_pack</td>
<td>packing</td>
<td>total_kg</td>
<td>total_price</td>
</tr>
<?php
foreach($data['products'] as $k=>$v){?>
    <tr>
    <td><?php echo $v; ?></td>
    <td><?php echo $data['qnt'][$k]; ?></td>
    <td><?php echo $data['unit_price'][$k]; ?></td>
    <td><?php echo $data['pack_size'][$k]; ?></td>
    <td><?php echo $data['unit_pack'][$k]; ?></td>
    <td><?php echo $data['packing'][$k]; ?></td>
    <td><?php echo $data['total_kg'][$k]; ?></td>
    <td><?php echo $data['total_price'][$k]; ?></td>
    </tr>
<?php }
?>
</table>