简体   繁体   English

foreach php-smarty帮助

[英]foreach php-smarty help

I have the php script as follows : 我有如下的PHP脚本:

$sql = "
        SELECT
            *
        FROM
            tbl_messages
        WHERE
            msg_sender_id = '$this->UM_index'
    ";
    $res = $this->db->returnArrayOfObject($sql);
    $this->assign_values('sent_messages',$res);

And in the templates I retrieved : 在我检索的模板中:

{foreach name = feach key = indx item = k from = $sent_messages}
{$k->msg_sender_id}
{$k->msg_subject}
{/forach}

I can retriev the above two fields. 我可以检索以上两个字段。 But I want to retrieve the message_sender_name for the field sender_id which can be obtained from a function getDetails($id) . 但是我想检索sender_id字段的message_sender_name ,它可以从函数getDetails($id)

My question is how can I assign the sender_name fields (which is a array) to my smarty template? 我的问题是如何为我的smarty模板分配sender_name字段(是一个数组)?

So you want also want to be able to have the message_sender_name, wich is returned by the function getDetails($id)? 因此,您还希望能够拥有message_sender_name,而该函数由getDetails($ id)函数返回?

You will have to loop trough your results, and fetch the message_sender_name for each row. 您将不得不遍历结果,并为每一行获取message_sender_name。 Something like this: 像这样:

$arrResults = array();
while ($res = $this->db->returnArrayOfObject($sql)))
{
  $res['msg_sender_name'] = getDetails($res['msg_sender_id']);
  $arrResults[] = $res;
}
$this->assign_values('sent_messages',$arrResults);

If I'm right, you can now access the sender_name via {$k->msg_sender_name} in your template-file. 如果我是对的,您现在可以通过模板文件中的{$k->msg_sender_name}访问sender_name。

But I would suggest that you use a JOIN in your SQL-statement, so you don't have to make another request for the sender_name. 但是我建议您在SQL语句中使用JOIN,这样就不必对sender_name进行其他请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM