Member Avatar for iamthwee
foreach ($query->result() as $row) 
{
    echo anchor("forum/topic/$row->topicid",  my_html_escape(($row->name)), 'attributs');
    echo ' by ';
    echo $this->Stuff->get_topic_author($row->topicid);
    echo ' replies ';
    echo $this->Stuff->get_replies($row->topicid);
    echo ' Last post ';
    echo $this->Stuff->get_topic_last_post($row->topicid);
    echo br();
}

How do I conveniently pass that to my view. Calling a model function within the original query->result has perplexed me?

The contents from view are retrieved response on controller's request , the controller will take the stored response and then assign them to the view file.

I haven't have the chance to look at the model file named stuff yet, but I am assuming there are methods there that should return a row array as a result of the query.

In CI we can use $query->row_array() and $query->result_array() depending on requirements.

Again, I need to assume that the model method $this->Stuff->get_topic_last_post($row->topicid) should return the latest posts.

the method can be similar to this

file location /application/model/

public function get_topic_last_post(){

    ## I did not put any parameter in it because. I am assuming here that the posts can be sorted by date.

    ## We prepare the query
    $your_query = " query here ";
    $query = $this->db->query($your_query);

    ## you must also add an statements here just in case the query return empty result

    return($query->result_array());

    }

}

I need to modify your controller a little because it is not necessary to iterate the result in the controller or in the model. We are only storing the result as an array and let the view take care of the iteration.

file name /application/controller/forum.php

public function get_topics()
{
    $this->load->model('Stuff');
    $result = $this->Stuff->get_topic_last_post();

    $this->load->view('views/forum', $result);

 }

The above example will load the $result to the template file named forum.php. The template file should do the iteration like this

file name : /application/views/forum.php

<?php
    foreach($result as $post){

    echo $post['title'].'<br/>';
    //echo the rest of the result.

    }

    ?>

Make sure to optimize your query so that you don't have to run more than what you need. A good tool for optimizing MySQL queries is MySQL workbench.

There is a template parser class in CI, but It is not as advance or feature rich like Smarty, Twig, and TBS.

I strongly suggest integrating with Smarty or Twig. I just posted a demo of CI/Smarty and CI/Twig yesterday, but I can't remember on which thread it was.

With Smarty integration, the controller file will look something like this

public function get_topics()
{
   $this->load->model('Stuff');

   $this->smarty->assign('result', $this->Stuff->get_topic_last_post());
   $this->smarty->view('views/forum.tpl');

 }

smarty supports (multi-dimensional).

$this->smarty->assign(array (
                'title'=> $title,
                'desc'=> $description,
                'tags'=> array('nice','top','popular'));

The view file or the template file will look something like this

{foreach $content as $post}

{$post.title}
<br/>
{$post.article}
<br/>

{/foreach}

Notice the above template source codes? It uses a DOT notation which is extremely useful in displaying layers of multi-dimensional arrays on the template file. We can also avoid using the foreach syntax in the template file by using section function.

Member Avatar for iamthwee

Sorry I wasn't clear.

Effectively, I want to dynamically create my own class and pass this to the view eg.

class details
{

 name;
 last poster;
 number_of_replies;
 author;
}

So have an array of class details objs then loop through them in the view.

How would I go about doing this.

You can't directly pass the model data to view.You have to go via controller.Normally in Model we define functions.Not classes.Model is the class.We extends it with codeigniter model.So you can define whatever functions that you want inside the model.After that you can get the reult to an array.Normally this is done by using result_array().

Then you can load the Model in the Controller's construct.Subsequently you can access the model functions.Simply load them to $data variable and loads the $data into view as you normally doing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.