如何使用SMARTY创建具有多维数组的HTML表格?

时间:2015-09-09 01:49:54

标签: php html arrays smarty

查看底部查看最新代码...

我最挣扎的地方是处理这种特定类型的数组。见下文。在PHP中使用var_dump显示。您将在下面看到3组值。我希望能够使用此数据创建三列。然后这些值会填满行。

array(3) { [0]=> array(28) { [0]=> string(4) "2160" [1]=> string(5) "63800" [2]=> string(6) "175820" [3]=> string(6) "234823" [4]=> string(6) "253080" [5]=> string(6) "256680" [6]=> string(6) "258760" [7]=> string(6) "258840" [8]=> string(6) "259560" [9]=> string(6) "258480" [10]=> string(6) "258550" [11]=> string(6) "258660" [12]=> string(6) "258480" [13]=> string(6) "258300" [14]=> string(6) "256140" [15]=> string(6) "257770" [16]=> string(6) "255960" [17]=> string(6) "255780" [18]=> string(6) "252520" [19]=> string(6) "253980" [20]=> string(6) "252540" [21]=> string(6) "250560" [22]=> string(6) "245700" [23]=> string(6) "253080" [24]=> string(6) "248580" [25]=> string(6) "248860" [26]=> string(6) "249660" [27]=> string(6) "246780" } [1]=> array(34) { [0]=> array(28) { [0]=> string(4) "2160" [1]=> string(5) "63800" [2]=> string(6) "175820" [3]=> string(6) "234823" [4]=> string(6) "253080" [5]=> string(6) "256680" [6]=> string(6) "258760" [7]=> string(6) "258840" [8]=> string(6) "259560" [9]=> string(6) "258480" [10]=> string(6) "258550" [11]=> string(6) "258660" [12]=> string(6) "258480" [13]=> string(6) "258300" [14]=> string(6) "256140" [15]=> string(6) "257770" [16]=> string(6) "255960" [17]=> string(6) "255780" [18]=> string(6) "252520" [19]=> string(6) "253980" [20]=> string(6) "252540" [21]=> string(6) "250560" [22]=> string(6) "245700" [23]=> string(6) "253080" [24]=> string(6) "248580" [25]=> string(6) "248860" [26]=> string(6) "249660" [27]=> string(6) "246780" } [1]=> string(4) "2160" [2]=> string(5) "63800" [3]=> string(6) "175820" [4]=> string(6) "234823" [5]=> string(6) "253080" [6]=> string(6) "256680" [7]=> string(6) "258760" [8]=> string(6) "258840" [9]=> string(6) "259560" [10]=> string(6) "258480" [11]=> string(6) "258550" [12]=> string(6) "258660" [13]=> string(6) "258480" [14]=> string(6) "258300" [15]=> string(6) "256140" [16]=> string(6) "257770" [17]=> string(6) "255960" [18]=> string(6) "255780" [19]=> string(6) "252520" [20]=> string(6) "253980" [21]=> string(6) "252540" [22]=> string(6) "250560" [23]=> string(6) "245700" [24]=> string(6) "253080" [25]=> string(6) "248580" [26]=> string(6) "248860" [27]=> string(6) "249660" [28]=> string(6) "246780" [29]=> string(4) "2150" [30]=> string(5) "63800" [31]=> string(6) "175420" [32]=> string(6) "234843" [33]=> string(6) "253080" } [2]=> array(41) { 

[0]=> array(28) { [0]=> string(4) "2160" [1]=> string(5) "63800" [2]=> string(6) "175820" [3]=> string(6) "234823" [4]=> string(6) "253080" [5]=> string(6) "256680" [6]=> string(6) "258760" [7]=> string(6) "258840" [8]=> string(6) "259560" [9]=> string(6) "258480" [10]=> string(6) "258550" [11]=> string(6) "258660" [12]=> string(6) "258480" [13]=> string(6) "258300" [14]=> string(6) "256140" [15]=> string(6) "257770" [16]=> string(6) "255960" [17]=> string(6) "255780" [18]=> string(6) "252520" [19]=> string(6) "253980" [20]=> string(6) "252540" [21]=> string(6) "250560" [22]=> string(6) "245700" [23]=> string(6) "253080" [24]=> string(6) "248580" [25]=> string(6) "248860" [26]=> string(6) "249660" [27]=> string(6) "246780" } 

[1]=> array(34) { [0]=> array(28) { [0]=> string(4) "2160" [1]=> string(5) "63800" [2]=> string(6) "175820" [3]=> string(6) "234823" [4]=> string(6) "253080" [5]=> string(6) "256680" [6]=> string(6) "258760" [7]=> string(6) "258840" [8]=> string(6) "259560" [9]=> string(6) "258480" [10]=> string(6) "258550" [11]=> string(6) "258660" [12]=> string(6) "258480" [13]=> string(6) "258300" [14]=> string(6) "256140" [15]=> string(6) "257770" [16]=> string(6) "255960" [17]=> string(6) "255780" [18]=> string(6) "252520" [19]=> string(6) "253980" [20]=> string(6) "252540" [21]=> string(6) "250560" [22]=> string(6) "245700" [23]=> string(6) "253080" [24]=> string(6) "248580" [25]=> string(6) "248860" [26]=> string(6) "249660" [27]=> string(6) "246780" } [1]=> string(4) "2160" [2]=> string(5) "63800" [3]=> string(6) "175820" [4]=> string(6) "234823" [5]=> string(6) "253080" [6]=> string(6) "256680" [7]=> string(6) "258760" [8]=> string(6) "258840" [9]=> string(6) "259560" [10]=> string(6) "258480" [11]=> string(6) "258550" [12]=> string(6) "258660" [13]=> string(6) "258480" [14]=> string(6) "258300" [15]=> string(6) "256140" [16]=> string(6) "257770" [17]=> string(6) "255960" [18]=> string(6) "255780" [19]=> string(6) "252520" [20]=> string(6) "253980" [21]=> string(6) "252540" [22]=> string(6) "250560" [23]=> string(6) "245700" [24]=> string(6) "253080" [25]=> string(6) "248580" [26]=> string(6) "248860" [27]=> string(6) "249660" [28]=> string(6) "246780" [29]=> string(4) "2150" [30]=> string(5) "63800" [31]=> string(6) "175420" [32]=> string(6) "234843" [33]=> string(6) "253080" } [2]=> string(4) "2160" [3]=> string(5) "63800" [4]=> string(6) "175820" [5]=> string(6) "234823" [6]=> string(6) "253080" [7]=> string(6) "256680" [8]=> string(6) "258760" [9]=> string(6) "258840" [10]=> string(6) "259560" [11]=> string(6) "258480" [12]=> string(6) "258550" [13]=> string(6) "258660" [14]=> string(6) "258480" [15]=> string(6) "258300" [16]=> string(6) "256140" [17]=> string(6) "257770" [18]=> string(6) "255960" [19]=> string(6) "255780" [20]=> string(6) "252520" [21]=> string(6) "253980" [22]=> string(6) "252540" [23]=> string(6) "250560" [24]=> string(6) "245700" [25]=> string(6) "253080" [26]=> string(6) "248580" [27]=> string(6) "248860" [28]=> string(6) "249660" [29]=> string(6) "246780" [30]=> string(4) "2150" [31]=> string(5) "63800" [32]=> string(6) "175420" [33]=> string(6) "234843" [34]=> string(6) "253080" [35]=> string(4) "2110" [36]=> string(5) "63800" [37]=> string(6) "175820" [38]=> string(6) "234823" [39]=> string(6) "252180" [40]=> string(6) "256680" } }

$flock等于为数组指定的变量。 $numFlocks等于1,2,3

的数组
<table>
 {section name=stand loop=$numFlocks}       

    <tr>
       {foreach from=$numFlocks item=columns}
          <td>{$flock[stand].$columns}</td>
       {/foreach}
   </tr>
 {/section}

</table>

以上产生如下表格。

63800 175820 234823

2160 63800 175820

Array 2160 63800

这远远不是我想要的。我显然得到了我的三个专栏,但我不知道为什么我只得到三行而且价值都错了。

下面是我的PHP代码,它创建了上面的数组$ flock。

for ($x = 1; $x <= $numFlocksTot; $x++) {

    $result1 = mysql_query("SELECT Amount_eggs FROM farm_weekly WHERE Flock_id = '" . $x . "' AND Client_id = '" . $_SESSION["id"] . "'", $connection) or die("Query failed : " . mysql_error()); 

    while ($line1 = mysql_fetch_assoc($result1))
    {
        $val[] = $line1["Amount_eggs"]; 
    }

    $flock[] = array_merge($flock, $val); 
} 

我修复了我的PHP,

for ($x = 1; $x <= $numFlocksTot; $x++) {

$Q = "SELECT units FROM farm_weekly WHERE Flock_id = '" . $x . "' AND Client_id = '" . $_SESSION["id"] . "'";

$result1 = mysql_query($Q, $connection) or die("Query failed : " .      mysql_error()); 

while ($line1 = mysql_fetch_assoc($result1))
{
    $val = $line1["units"];
    array_push($flock, $val); 
}

    array_push($result, $flock); 
    unset($flock);
    $flock[] = array();
} 

VAR_EXPORT吐出:

   array ( 0 => array ( ), 1 => array ( 0 => array ( ), 1 => '2160', 2 => '63800', 3 => '175820', 4 => '234823', 5 => '253080', 6 => '256680', 7 => '258760', 8 => '258840', 9 => '259560', 10 => '258480', 11 => '258550', 12 => '258660', 13 => '258480', 14 => '258300', 15 => '256140', 16 => '257770', 17 => '255960', 18 => '255780', 19 => '252520', 20 => '253980', 21 => '252540', 22 => '250560', 23 => '245700', 24 => '253080', 25 => '248580', 26 => '248860', 27 => '249660', 28 => '246780', ), 2 => array ( 0 => array ( ), 1 => '2150', 2 => '63800', 3 => '175420', 4 => '234843', 5 => '253080', ), 3 => array ( 0 => array ( ), 1 => '2110', 2 => '63800', 3 => '175820', 4 => '234823', 5 => '252180', 6 => '256680', ), )

但是我的SMART表仍然会导致问题:

 <tr>
  {foreach from=$numFlocks item=columns}
    {section loop=$flock name=unit}
        <td>{$flock[unit].$columns}</td>
    {/section}
  {/foreach}
 </tr>

上述尝试只给了我一行10列。第一列是空白,然后是每个内部数组的前三个条目,但随后停止。

1 个答案:

答案 0 :(得分:0)

SMART / HTML

<th>Blank</th>
  {foreach from=$numFlocks item=foo}
<th>Flock {$foo}</th>
  {/foreach}
</tr>

{section loop=$flock[1] name=unit}
  <tr {if $smarty.section.unit.first}style="display:none"{/if}>
{foreach from=$flock item=foo}
    <td>{$foo[unit]}</td>
{/foreach}
  </tr>
{/section}

PHP代码

for ($x = 1; $x <= $numFlocksTot; $x++) {

 $Q = "SELECT Amount_eggs FROM farm_weekly WHERE Flock_id = '" . $x . "' AND Client_id = '" . $_SESSION["id"] . "'";

 $result1 = mysql_query($Q, $connection) or die("Query failed : " . mysql_error()); 

while ($line1 = mysql_fetch_assoc($result1))
{
    $val = $line1["Amount_eggs"];
    array_push($flock, $val); 
}

array_push($result, $flock); 
unset($flock);
$flock[] = array();
}