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();
        }

This has been bothering me for a while. Let's say I have that code I want to pass this to a view and easily output it in a loop.

say this in my view

foreach blah as row

row->name
row->topicauthor
row->replies
row->lasttopic

end foreach

How?

Hi,

You can pass result to view either using model or execute in controller

a) Using Model (in model "topicmodel" you can execute query)

//Get Result via model
$this->load->model('topicmodel');
$result = $this->topicmodel->get_details();

//and pass into to view
//View
$data = array();
$data['topicresult'] = $result ; //Assign result to variable data

$this->load->view('myview',$data); // Pass result to view

// You can access all result as variable $topicresult

If you have any query, Please let me know.

Thanks,
Ajay

Member Avatar for iamthwee

OMGGGGGGGGGGGGGGGGGGGGG.

I hate how codeigniter doesn't let you pass objects to views. Instead I spend hours trying to convert it to a multidimensional array using this stupid foreach syntax. I mean wtf, give me a break. Couldn't work out for the life of me how to DYNAMICALLY populate the multidimensional array, then loop through it in my view. Is it possible.

So I instead went for this primitive syntax that at least work, but seems much more laborious and IMO is harder to read.

$i = 1;

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

    $topics[$i]['topicid'] = $row->topicid;

    $topics[$i]['title'] = my_html_escape(($row->name));

    $topics[$i]['author'] = $this->Stuff->get_topic_author($row->topicid);

    $topics[$i]['replies'] =$this->Stuff->get_replies($row->topicid);

    $topics[$i]['lastpost'] =$this->Stuff->get_topic_last_post($row->topicid);

    $topics[$i]['lastdate'] =$this->Stuff->get_topic_last_date($row->topicid);


    $i++;

}

        $data['total'] = $i;
        $data['topics'] = $topics;
        $this->load->view('users/create-new-topic',$data);

And loading the view:

for ($row = 1; $row < $total; $row++)
{
    $id =$topics[$row]['topicid'];
    echo anchor("forum/topic/$id", $topics[$row]['title'], 'attributs');

    echo " replies ";
    echo $topics[$row]['replies'];
    echo " last poster ";
    echo $topics[$row]['lastpost'];
    echo " time: ";
    echo $topics[$row]['lastdate']; 
    echo br();         
}
Member Avatar for iamthwee

OK so it is not so hard to read... but just...ugh...

Hi,

can you give my some idea of your table structure? So I will give you more useful solution.

Thanks,
Ajay

Member Avatar for iamthwee

Sure my table is as follows:

 users
+-------+-------+----------+-----------+
| userid| name  | no_posts |admin_level|
+-------+-------+----------+-----------+

topics
+----------+------------+
| topic_id | topic_name |
+----------+------------+

posts
+--------+----------+---------+--------+---------+
| postid | userid   | content | date   | topic_id|
+--------+----------+---------+--------+---------+

I would like to list all topics with author name, number of replies, last name of last poster and order by last poster date.

I'm assuming I can do a nested MYSQL statement to somewhat accomplish this however, for the time being called these as SEPARATE functions and passing to a 2D array seems to work.

Hi,

Thank you for your response. Give me some time, so I will configure on my local machin and check.

Thanks,
Ajay

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.