CodeIgniter Forums
What is problem behide my code? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: What is problem behide my code? (/showthread.php?tid=25984)



What is problem behide my code? - El Forum - 01-03-2010

[eluser]Unknown[/eluser]
I test again many time. But i don't know why below code execute wrong result.

This is code for controller
Code:
// Update number viewed of topic
            $query = $this->media_topic_model->read($id);
            $result = $query->result();
            if ($result) {
                $item = $this->objectToArray($result[0]);

                $item['topic_viewed'] = intval($item['topic_viewed']) + 1; // Count and inc correctly.
                var_dump($item);
                $this->media_topic_model->update($item['topic_id'], $item);
            }

This code for model
Code:
function update($id, $data)
    {
        var_dump($item); // check again data
        $this->db->where('topic_id', $data['topic_id']);
        $this->db->set('user_id', $data['user_id']);
        $this->db->set('topic_parent_id', $data['topic_parent_id']);
        $this->db->set('topic_title_ascii', $data['topic_title_ascii']);
        $this->db->set('topic_name', $data['topic_name']);
        $this->db->set('topic_order', $data['topic_order']);
        $this->db->set('topic_viewed', $data['topic_viewed']);
        $this->db->update('media_topic');

        return $this->db->affected_rows();
    }

I check var_dump before call $this->media_topic_model->update($item['topic_id'], $item); function and inside this function => data are OK. But when codeigniter core execute. Value of topic_viewed inc 4,5 or 6, 7... . Eg, 10 => 17 ?

Do you meet this problem ?