2011-06-10 47 views
1

如果問題不清楚,請道歉。新手關於在頁面上添加數組元素的問題(php腳本)

我是一個總的新手php用戶,我有這個腳本,我想編輯。

腳本使用.tpl作爲主題。

memberprofile.tpl存在first name元素$profilearray[0].firstname我也想加入這個結果元素的另一個.tpl文件名爲docs.tpl

我試圖複製和粘貼在docs.tpl`$profilearray[0].firstname,但沒有奏效。 我注意到docs.tpl使用它自己的$docsarray[0].xxx

所以大家任何想法如何做到這一點?因爲我希望將會員資料中的一些信息添加到文檔頁面中。

我試圖與MySQL玩,但我不知道如何使用同一元素firstname兩個memberprofiledocs

我敢肯定有一個簡單的方法來做到這一點。

下面是從memberprofile.tpl完整的代碼,我想展示一些這些信息在該主題docs.tpl

<p class="gray"> 
    {$lang112}: <b>{$profilearray[0].firstname}&nbsp;{$profilearray[0].lastname}</b><br> 
    {$lang130}: <b>{$profilearray[0].birthday}</b><br> 
    {$lang134}: <b>{if $profilearray[0].gender eq "1"}Male{elseif $profilearray[0].gender eq "0"}Female{/if}</b><br>     
    {$lang140}: <b>{$profilearray[0].city}</b> <br> 
    {$lang139}: <b>{$profilearray[0].country}</b> <br> 
    {$lang113}: <b>{insert name=get_stripped_phrase value=a assign=pdesc details=$profilearray[0].description}{$pdesc}</b> <br> 
    {$lang259}: <b><a href="{$profilearray[0].url}" target="_blank">{$profilearray[0].url|stripslashes|truncate:20:"...":true}</a></b> <br> 
    {$lang260}: <b>{insert name=get_time_to_days_ago value=var time=$profilearray[0].lastlogin}</b> <br> 
    {$lang261}: <b>{insert name=get_time_to_days_ago value=var time=$profilearray[0].addtime}</b> 
</p> 
+0

這些'.tpl'文件屬於哪個模板引擎? – hakre 2011-06-10 18:51:33

+0

看起來像Smarty對我。 – 2011-06-10 18:52:10

+0

看起來像samrty – mcgrailm 2011-06-10 18:53:07

回答

1
$profilearray 
在你的榜樣

被分配給Smarty模板模板被稱爲以某種方式simmilar這個

  $smarty->assign('profilearray',$somearray); 

你需要找到什麼是設置在第一個文件然後確保前即包含在您的第二個模板中

但您當然應該閱讀smarty documentation以瞭解您正在嘗試執行的操作。

1

要退後一會兒......有兩個部分。第一部分是實際接受用戶輸入,查詢數據庫,處理數據等的PHP代碼。第二部分是TPL文件。儘可能地,TPL文件應該只關注演示文稿,而不是數據處理,數據交叉引用等。

所有數據庫讀取和交叉引用應該發生在普通的PHP內部文件,不在TPL內。

爲了將「作者信息」添加到「文檔列表」(或任何您呼叫docs.tpl)的頁面,您需要找到拉取文檔列表的PHP代碼。找到PHP代碼,它說是這樣的:

$smarty->assign('docsarray',$document_list); 

現在你要做的是通過更多信息Smarty模板(TPL文件),以便它可以顯示它是什麼。喜歡的東西:

for($document_list as $index => $doc){ 
    $owner = $doc['owner'];  // Get the owner of the document 
    $profile = getProfile($owner); // Create one of the same things that go into $profilearray elsewhere 
    $document_list[$index]['profile'] = $profile; // Modify original array 
} 
$smarty->assign('docsarray',$document_list); 

然後進入docs.tpl並找到它顯示有關文檔的信息,並添加Smarty的模板代碼從您添加的新的每個文檔的信息閱讀。 (檢查Smarty reference page瞭解詳細信息)

例如,如果docs.tpl顯示的文檔的表格,你可以添加一個新列,顯示了作者的名/姓:

<tr> 
    <td>{$docsarray[$index].title}</td> 
    <td>{$docsarray[$index].created_date}</td> 
    <!-- Next line is new --> 
    <td>{$docsarray[$index].profile.firstname} {$docsarray[$index].profile.lastname}</td> 
</tr> 

如果你想要的東西,看起來完全像「檔案盒」,你也可以這樣做。實際上,使用{include}您可以創建profilebox.tpl並在兩個地方使用它以減少冗餘代碼。