foreach php-smarty 帮助
我的 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_id
的 message_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以您还希望能够拥有由函数 getDetails($id) 返回的 message_sender_name 吗?
您将必须循环遍历结果,并获取每一行的 message_sender_name 。像这样的事情:
如果我是对的,您现在可以通过模板文件中的
{$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:
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.