• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Better practice to do this?

I've been seeing some similar examples to the problem I have, but none has been working or been fully similar.

| id | subcategory_id | generic_name | name | type | started_by | last_post | last_post_by | replies | views |

| user_id | user_login | user_pass | useraccess | user_screenname | usergroup_id | user_posts | user_title | user_email | user_sig | user_joined | user_lastpost | user_lastlogin | user_msn | user_aim | user_timeformat | user_avatar | user_blocked | user_blockedmsg | user_ip | user_website | user_usesignature | user_usesmileys |

function getTopics()
        $generic_name = $this->uri->segment(1);

        $this->db->select('topics.*, users.*, forum_subcategories.*');
                        'users.user_id = topics.started_by', 'left');
                        'forum_subcategories.subcategory_id = topics.subcategory_id', 'right');
                        'forum_categories.category_id = forum_subcategories.category_id');
        $this->db->where('forum_subcategories.subcategory_generic_name', $generic_name);
        $query = $this->db->get('topics');
        return $query;

This all works out fine, except that I'm not able to do a join again for getting the topics.last_post_by since it overwrites the user.user_screenname from topics.started_by (it can be a different user who started the topic and another user who posted the last thing in the topic). Both started_by and last_post_by are INTS that corresponds to the ID of a user.

So what I then tried to do is to manipulate the data in the controller by doing this:
function doForum()
        $this->forum['rawTopics'] = $this->f->getTopics();
        foreach ($this->forum['rawTopics']->result() as $topic) {
            $this->forum['topics'][$topic->id]['last_post_by_userName']    = $this->user->idToName($topic->last_post_by);
            $this->forum['topics'][$topic->id]['started_by_userName']    = $this->user->idToName($topic->started_by);

What it's suppose to do is to make a new variable (I would prefere to edit the initial variable...), and add ['last_post_by_userName'] and ['started_by_userName'] to each row which should contain the username for both last_post_by and started_by.

I'll also show you my view
<div id="forum-container">

    <div id="forum-name">&lt;?= $rawTopics->row()->subcategory_name ?&gt;</div>

    <div class="forum-title-header">
        <div class="forum-topicpicture-header">
        <div class="forum-lastpost-header">
            Last Post
        <div class="forum-views-header">
        <div class="forum-replies-header">

        Topic Title
    foreach ($topics as $row) {
    <div class="forum-title">
            <div class="forum-topicpicture-standard">
            <div class="forum-lastpost">
                Posted: &lt;?=$row->last_post_by_userName?&gt;<br />
                Author: <a href="viewProfile/darky-2/">USERNAME</a>

What you should notice about this is that I need to do a $rawTopics->row()->subcategory_name in the top to get out the Subcategory (as like a page title) for those topics, which needs to be outside of the foreach loop (since we only need it once.)

I've never looped through the results in my controller trying to edit the outcome before, so I have no idea if it's the right way of doing it. If you can come up with a better solution don't hold back! Smile

EDIT: I should also mention that my example doesn't work as well Sad

Best regards,

The bad news... 40 views and 0 replies...

The good news... I've sorted it out, at least partially.

What I did was change the controller code

$this->doForum['rawTopics'] = $this->f->getTopics($this->uri->segment_array());
        $this->doForum['rawTopics2'] = $this->doForum['rawTopics'];
        foreach ($this->doForum['rawTopics2']->result_array() as $topic) {
            $this->forum['topics'][$topic['id']] = $topic;
            $this->forum['topics'][$topic['id']]['last_post_by_userName']    = $this->user->idToName($topic['last_post_by'])->row_array();
            $this->forum['topics'][$topic['id']]['started_by_userName']    = $this->user->idToName($topic['started_by'])->row_array();
        //Get subcategory as well
        $this->forum['subcategory'] = $this->doForum['rawTopics']->row();

Works like a charm, except that I would like it to be object instead of array... Except I have absolutly no idea at all on how to do that. All these problems makes me feel like a noob all over again haha.



For everyone else who might be in need of doing something like this, here's what I finally found out!

foreach ($this->doForum['rawTopics']->result() as $topic) {
            $this->forum['topics'][$topic->id] = $topic;

            $this->forum['topics'][$topic->id]->last_post_by_username    = $this->user->id_to_loginname($topic->last_post_by)->row()->user_screenname;
            $this->forum['topics'][$topic->id]->started_by_username        = $this->user->id_to_loginname($topic->started_by)->row()->user_screenname;

Basically what you need to do is to save it as a new object array (once you get ->result() you can't edit it anymore) and then add [$topic->id] to the object array to get it listed and finally (if you want to) you do ->whatever. that "->whatever" means what you want to use when to call that value.
Sorry if my descriptions on how to do this is a little lame, but I think the code speaks for itself Smile

EDIT: removed some debugging die(print_r()); rows from the code to make it cleaner

Best regards,

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.