foreach php-smarty 帮助

发布于 2024-12-04 04:48:17 字数 657 浏览 1 评论 0原文

我的 php 脚本如下:

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

在我检索的模板中:

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

我可以检索上述两个字段。但我想检索字段 sender_idmessage_sender_name,该字段可以从函数 getDetails($id) 获取。

我的问题是如何将 sender_name 字段(这是一个数组)分配给我的 smarty 模板?

I have the php script as follows :

$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).

My question is how can I assign the sender_name fields (which is a array) to my smarty template?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱娆 2024-12-11 04:48:17

所以您还希望能够拥有由函数 getDetails($id) 返回的 message_sender_name 吗?

您将必须循环遍历结果,并获取每一行的 message_sender_name 。像这样的事情:

$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);

如果我是对的,您现在可以通过模板文件中的 {$k->msg_sender_name} 访问 sender_name。

但我建议您在 SQL 语句中使用 JOIN,这样您就不必再次请求 sender_name。

So you want also want to be able to have the message_sender_name, wich is returned by the function getDetails($id)?

You will have to loop trough your results, and fetch the message_sender_name for each row. 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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文