开发者

codeigniter 2 and smarty 3 : multi array of stdClass results returned - how do i display these in smarty

My Sql function getLive_projects has this query -

$query = "SELECT p.* FROM projects p WHERE p.live = 1 ORDER BY client_name ASC";

$response = $this->db->query($query)->result();

My Controller :

// get live projects

$data['live_projects'] = $this->projects->get_live_projects();

// load the projects with the data

$this->parser->parse("projects.tpl", $data);

The data in my template looks like this when I use {$live_projects|print_r}

Array
(
[0] => stdClass Object
    (
        [id] => 1
        [client_name] => client one
        [dev_url] => clientone.com
    开发者_运维技巧    [live_url] => clientone.com
        [version] => 1.0
        [live] => 1
    )

[1] => stdClass Object
    (
        [id] => 2
        [client_name] => client two
        [dev_url] => clienttwo.com
        [live_url] => clienttwo.com
        [version] => 3.1
        [live] => 1
    )

)

How can I output this in my template? I tried {foreach from=$live_projects item=$project} and then tried to use {$project->client_name} but that didn't work. Should the object not be available to me when I loop over the array? Have I missed something obvious? Am I going about this in the right way? I just want to loop over the returned results and display each one as a row in my template.

I have searched the forums but was unable to find an answer that solved my issue - any help would be much appreciated. There are some example out there about using $live_projects[0]->id but how can this be incorporated into output from smarty where many items are returned in the result set.


You were almost there! When defining the name of your item variable, you don't have to include the '$' sign. Change your code to:

{foreach from=$live_projects item=project}
    {$project->client_name}
{/foreach}

That should do the trick.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜