Author Topic: Iterating Variable Pairs in CodeIgniter?  (Read 811 times)

0 Members and 1 Guest are viewing this topic.

Offline Hybrid Kill3rTopic starter

  • Enthusiast
  • Posts: 344
    • View Profile
    • LenseReflex.com
Iterating Variable Pairs in CodeIgniter?
« on: September 07, 2010, 07:09:42 PM »
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:

Code: [Select]
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);

Code: [Select]

<div id='news'>

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

Offline keevitaja

  • Irregular
  • Posts: 41
    • View Profile
Re: Iterating Variable Pairs in CodeIgniter?
« Reply #1 on: September 09, 2010, 09:08:00 AM »
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}