foreach php-smarty help
I have the php script as follows :
$sql = "
SELECT
*
FROM
tbl_messages
WHERE
开发者_JS百科 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?
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.
精彩评论