通过设置列数和行数在php中创建json表

时间:2014-08-07 07:27:21

标签: php html json

我有这样的Json结果:

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

我正在尝试使用行和列格式10 x 3创建一个商店表格,只显示这样的所有者名称:

+-----------+---------+-----------+---------+-----------+---------+---------+
|    John   |  Mike   |   owner_3 |  owner_5    owner_6   .....    owner_10 |
+-----------+---------+-----------+---------+-----------+---------+---------+
| owner_11  | ................................................... |owner_20
+-----------+---------+-----------+---------+-----------+---------+---------+
| owner_21  |  .................................................. |owner_30 |
+-----------+---------+-----------+---------+-----------+---------+---------+

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

$ShopTable.='<table style="width:990px;" id="" border="1">';
$ShopTable.='<thead>';

$ShopJson = getShop();
$ShopChan = $ShopJson['Shop'];
$ShopTable.='<tr>';
if (count($ShopChan ) > 0) {
    for ($i = 0; $i < count($ShopChan); $i++) {
        $ShopChanRecord = $ShopChan[$i];
            $ShopTable.='<td>' . $ShopChanRecord['Owner'] . '</td>';

    }
}
$ShopTable.='</tr>';
$ShopTable.='</tbody>';
$ShopTable.='</table>';
echo $ShopTable;

是否有人可以查看我的代码是否有任何问题?任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

试试这个

$ShopChan = $ShopJson['Shop'];

if(count($ShopChan ) > 0) 
{
    $ShopTable.='<table style="width:990px;" id="" border="1">';
    for ($i = 0; $i < count($ShopChan); $i++) {
        $rem = ($i+1)%10;
        if($rem==1)
        {
            $ShopTable.='<tr>'; 
        }
            $ShopChanRecord = $ShopChan[$i];
            $ShopTable.='<td>' . $ShopChanRecord['Owner'] . '</td>';

        if(($rem==0)
        {
            $ShopTable.='</tr>';    
        }   
    }

    if($rem!=0)
    {
        for($j=$rem; $j>=0; $j--)
        {
            $ShopTable.='<td>&nbsp;</td>';
        }
        $ShopTable.= '</tr>';
    }

    $ShopTable.='</table>';
}