2014-03-18 176 views
-1

我正在一個PHP項目,我試圖將多維數組導出到Smarty。多維數組與smarty

下面是我的陣列的填充方式:

$array = array(); 
      while ($myrow = mysql_fetch_array($result)) 
      { 
       $array[$myrow['BugID']][$myrow['id']]['BugID'] = $myrow['BugID']; 
       $array[$myrow['BugID']][$myrow['id']]['DateSent'] = $myrow['DateSent']; 
       $array[$myrow['BugID']][$myrow['id']]['FromAddress'] = $encryption->decrypt($myrow['FromAddress']); 
       $array[$myrow['BugID']][$myrow['id']]['ToAddress'] = $encryption->decrypt($myrow['ToAddresss']); 
       $array[$myrow['BugID']][$myrow['id']]['Message'] = $encryption->decrypt($myrow['Message']); 
      } 
      $smarty->assign("array", $array); 
      $content = $smarty->fetch("message-list.tpl"); 

下面是我的模板文件

<table class="tableDetails"> 
    <tr> 
     <th>Date Sent</th> 
     <th>Bug ID</th> 
     <th>From Address</th> 
     <th>To Address</th> 
     <th>Message</th> 
    </tr> 
{foreach from=$array key=header item=table} 
    <tr> 
     <td>{$array.DateSent}</td> 
     <td>{$array.DateSent}</td> 
     <td>{$array.DateSent}</td> 
     <td>{$array.DateSent}</td> 
     <td>{$array.DateSent}</td> 
    </tr> 
{/foreach} 
</table> 

Array 
(
    [2] => Array 
     (
      [3] => Array 
       (
        [BugID] => 2 
        [DateSent] => 2014-03-17 22:51:08 
        [FromAddress] => [email protected] 
        [ToAddress] => 
        [Message] => This is a test. 
       ) 

     ) 

    [1] => Array 
     (
      [2] => Array 
       (
        [BugID] => 1 
        [DateSent] => 2014-03-15 00:04:03 
        [FromAddress] => [email protected] 
        [ToAddress] => 
        [Message] => This is a message from the bug administrator to the bug reporter 
       ) 

      [1] => Array 
       (
        [BugID] => 1 
        [DateSent] => 2014-03-15 00:03:17 
        [FromAddress] => [email protected] 
        [ToAddress] => 
        [Message] => This is the content of the message from the bug reporter to the administrator 
       ) 

     ) 

) 

目前沒有在我的表被輸出,除了表頭,從一無所有實際的數組。

感謝您提供的任何幫助。

+0

請閱讀智者的foreach是如何工作的:http://www.smarty.net/docs/en/language.function.foreach.tpl – Borgtex

回答

1

更新答案

您需要遍歷每個「錯誤」的內部陣列。參見:

<?php 
// index.php 
require_once('smarty/Smarty.class.php'); 
$smarty = new Smarty(); 

$bugs = array(); 
$bugs[1][412]['BugID'] = 1; 
$bugs[1][412]['DateSent'] = date("F jS, Y"); 
$bugs[1][412]['FromAddress'] = "[email protected]"; 
$bugs[1][412]['ToAddress'] = "[email protected]"; 
$bugs[1][412]['Message'] = "this is a test!"; 
$bugs[2][534]['BugID'] = 2; 
$bugs[2][534]['DateSent'] = date("F jS, Y"); 
$bugs[2][534]['FromAddress'] = "[email protected]"; 
$bugs[2][534]['ToAddress'] = "[email protected]"; 
$bugs[2][534]['Message'] = "lorem ipsum!"; 

$smarty->assign('bugs', $bugs); 

$smarty->display("index.tpl"); 

模板:

<table class="tableDetails"> 
    <tr> 
     <th>Date Sent</th> 
     <th>Bug ID</th> 
     <th>From Address</th> 
     <th>To Address</th> 
     <th>Message</th> 
    </tr> 
{foreach from=$bugs key=parentID item=table} 
    {foreach from=$table key=bugUID item=bug} 
    <tr> 
     <td>{$bug.DateSent}</td> 
     <td>{$parentID}</td> 
     <td>{$bug.FromAddress}</td> 
     <td>{$bug.ToAddress}</td> 
     <td>{$bug.Message}</td> 
    </tr> 
    {/foreach} 
{/foreach} 
</table> 

$parentID將是主要缺陷ID(或者 「1」 或 「2」)。 bugUID是內部ID(如「412」或「534」)。

結果:

Result

+0

這不會幫助我不幸。它的完成方式使得所有不同的ID都被分配給一個具有bugID值索引的數組,這樣做會改變結構並且不適合我的需求 – Boardy

+0

已更新的答案。 –

+0

謝謝你完美的作品。 – Boardy