Jump to content

Iterating Variable Pairs in CodeIgniter?


ShoeLace1291

Recommended Posts

I'm using the template parser class in codeigniter to display database results in a view file.  Is it possible to parse an array like this:

 

foreach($query->result() as $news){

     $articles[$news->id] = array(
                                                   'ID' => $news->id,
                                                   'TITLE' => $news->title,
                                                   'BODY' => $news->body
                                                   );

}

$data = array(
                       'NEWS_ARTICLES' => array(
                                                                   $artcicles
                                                                   );
$this->parser->parse('index.tpl', $data);

 


<div id='news'>

<h1>Recent News</h1.
{NEWS_ARTICLES}
<div class='article'>
<h1>{TITLE}</h1>
<p>{BODY</p>
</div>
{/NEWS_ARTICLES}
</div>

 

Link to comment
https://forums.phpfreaks.com/topic/212809-iterating-variable-pairs-in-codeigniter/
Share on other sites

i have never used templated before, but:

 

$query->result() should allready give you the result as array, so no need to walk it trought!

 

whatabout just

 

$data['articles'] = $query->result();
$this->parser->parse('index.tpl', $data);

 

and in template file

{articles}

so some stuff with values like {title} and {body}

{/articles}

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.