用json php创建表

时间:2014-07-22 05:49:51

标签: php html json

我有这样的Json结果:

数组(

[0] => Array (
     [Street] => Street_name 
     [status] => Best_Shop 
     [Shop] => Array ( [0] => Array ( 
                    [Name] => Bakery_Shop 
                    [Owner] => John
                    [Type] => 0 
                    [Food] => Cake 
                    [Drink] => Coffee 
                    [Best_Customer] => All
                    [a] => Good 
                    [b] => Normal
                    [c] => Bad  
               [1] => Array ( 
                    [Name] => Junk_Foodshop 
                    [Owner] => Mike
                    [Type] => 0 
                    [Food] => Burger 
                    [Drink] => Coke 
                    [Best_Customer] => All
                    [a] => Good 
                    [b] => Normal
                    [c] => Bad ) 
    [Rate] => Average 
    [Signature] => Boss ) )

我正在尝试创建一个商店表格,其中的行显示每个统计数据:

Name          Owner Type Food   Drink    Best_Customer  a    b      c

Bakery_Shop   John   0   Cake   Coffee       All        Good Normal Bad

Junk_Foodshop Mike   0   Burger Coke         All        Good Normal Bad

我目前已经编写了这些代码,虽然我得到的数据并不像我希望的那样工作。

****From json_shop.php****

function getAstCard(){
    $json = array();
    $json[0] = array(
        "Street" => "Street_name ",
        "status" => "Best_Shop ",
        "Shop" => $Shop,
        "Rate" => "Average ",
        "Signature" => $conf->get_IXAstUser()
    );
    return $json;
}
**Into jsonTable.php**

    <?php
    include 'json_shop.php';

    $AstCardTable.='<table style="width:990px;" id="shopcard">';
    $AstCardTable.='<thead>';
    $AstCardTable.='<tr>';
    $AstCardTable.='    
                            <th>Name</th>
                            <th>Owner</th>
                            <th>Type</th>
                            <th>Food</th>
                            <th>Drink</th>
                            <th>Best_Customer</th>
                            <th>a</th>
                            <th>b</th>
                            <th>c</th>
                            </tr>
                            </thead>
                            <tbody>';

    $AstCard = getAstCard();
    if (count($AstCard) > 0) {
        for ($i = 0; $i < count($AstCard); $i++) {
            $AstCardRecord = $AstCard[$i];
                $AstCardTable.='<tr>';
                $AstCardTable.='<td>' . $AstCardRecord['Name'] . '</td>';
                $AstCardTable.='<td>' . $AstCardRecord['Owner'] . '</td>';
                $AstCardTable.='<td>' . $AstCardRecord['Type'] . '</td>';
                $AstCardTable.='<td>' . $AstCardRecord['Food'] . '</td>';
                $AstCardTable.='<td>' . $AstCardRecord['Drink'] . '</td>';
                $AstCardTable.='<td>' . $AstCardRecord['Best_Customer'] . '</td>';
                $AstCardTable.='<td>' . $AstCardRecord['a'] . '</td>';
                $AstCardTable.='<td>' . $AstCardRecord['b'] . '</td>';
                $AstCardTable.='<td>' . $AstCardRecord['c'] . '</td>';
                $AstCardTable.='</tr>';
        }
    } else {
        $AstCardTable.='<tr>';
        $AstCardTable.='<td style="font-family: Verdana;font-weight: bold;font-size: 12px;" colspan=9>Temporarily no data in the list</td>';
        $AstCardTable.='</tr>';
    }

    $AstCardTable.='</tbody>';
    $AstCardTable.='</table>';

    echo $AstCardTable;


    ?>

是否有任何有此主题经验的人,看看我的代码是否有任何问题?任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

您需要从$json[0]['Shop'];方法返回getAstCard

否则,请稍微更改循环中的变量:

$AstCard = getAstCard();
$AstCardShop = $AstCard[0]['Shop'];
if (count($AstCardShop) > 0) {
    for ($i = 0; $i < count($AstCardShop); $i++) {
            $AstCardRecord = $AstCardShop[$i];

            $AstCardTable.='<tr>';
            $AstCardTable.='<td>' . $AstCardRecord['Name'] . '</td>';
            $AstCardTable.='<td>' . $AstCardRecord['Owner'] . '</td>';
            $AstCardTable.='<td>' . $AstCardRecord['Type'] . '</td>';
            $AstCardTable.='<td>' . $AstCardRecord['Food'] . '</td>';
            $AstCardTable.='<td>' . $AstCardRecord['Drink'] . '</td>';
            $AstCardTable.='<td>' . $AstCardRecord['Best_Customer'] . '</td>';
            $AstCardTable.='<td>' . $AstCardRecord['a'] . '</td>';
            $AstCardTable.='<td>' . $AstCardRecord['b'] . '</td>';
            $AstCardTable.='<td>' . $AstCardRecord['c'] . '</td>';
            $AstCardTable.='</tr>';
    }
}