新手关于在页面上添加数组元素的问题(php脚本)


Newbie Question about adding an array element on a page (php script)

如果问题不清楚,请向我道歉。

我是一个完全的php新手用户,我有这个脚本要编辑。

该脚本使用了.tpl作为主题。

memberprofile.tpl中有first name元素$profilearray[0].firstname,我想将该元素的结果也添加到另一个名为docs.tpl.tpl文件中

我尝试在docs.tpl中复制和粘贴`$profilearray[0].firstname,但没有成功。我注意到docs.tpl使用自己的$docsarray[0].xxx

伙计们知道怎么做吗?因为在成员配置文件中有一些信息,我想将它们添加到文档页面中。

我试着玩mysql,但我不知道如何对memberprofiledocs表使用相同的元素firstname

我相信有一种简单的方法可以做到

这是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>

Smarty有一个名为{include}的标签,你可以在谷歌上搜索它,它可以做你想做的事情。http://www.smarty.net/docsv2/en/language.function.include.tpl在你的docs.tpl文件上使用这个标签,你会没事的。

$profilearray

在您的示例中,在以类似于该的方式调用模板之前,已将其分配给smarty模板

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

你需要在你的第一个文件中找到设置的内容,然后确保它包含在你的第二个模板中

但是你当然应该阅读smarty文档来了解你想要做什么。

退一步。。。这一切有两部分。第一部分是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参考页面了解详细信息。)

例如,如果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并在两个位置使用它来减少冗余代码。