Codeigniter PHP Foreach重复发布

时间:2016-07-12 23:42:54

标签: php mysql codeigniter foreach

img 每次我添加第二条评论时,我的帖子都会重复。当我在系统中包含注释时出现了这种情况。

我的代码如下。每次添加新评论时,帖子都会重复。

 1 comment = 1 post; 2 comments = 2 post; 3 comments = 3 post[...] 

enter image description here

我的控制器:

public function index()
{
    if($this->session->userdata('u_logged') == TRUE)
    {
        $config                 = array();
        $config['base_url']     = base_url().'core/index';
        $config['total_rows']   = $this->core_model->types_count();
        $config['per_page']     = 4;
        $config['uri_segment']  = 3;
        $config['use_page_numbers'] = TRUE;

        $config['full_tag_open'] = "<ul class='pagination noborder'>";
        $config['full_tag_close'] ="</ul>";
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['cur_tag_open'] = "<li class='disabled noborder'><li class='active noborder'><a href='#'>";
        $config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
        $config['next_tag_open'] = "<li>";
        $config['next_tagl_close'] = "</li>";
        $config['prev_tag_open'] = "<li>";
        $config['prev_tagl_close'] = "</li>";
        $config['first_tag_open'] = "<li>";
        $config['first_tagl_close'] = "</li>";
        $config['last_tag_open'] = "<li>";
        $config['last_tagl_close'] = "</li>";

        $this->pagination->initialize($config);
        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data['results']    = $this->core_model->fetchTypes($config['per_page'], $page);
        $data['links']      = $this->pagination->create_links();

        $data['notifications'] = $this->core_model->getLastNotifications();
        $this->loadTemplate('pages', 'index_view', $data);

        if(!empty($this->input->post('comm')))
        {
            $this->core_model->addComment();
        }
    }
    else if($this->session->userdata('u_logged') == FALSE)
    {
        $this->loadTemplate('pages', 'index_view', '0');
    }
}

我的模特

    public function types_count()
    {
        return $this->db->count_all('types');
    }

    public function fetchTypes($limit, $start)
    {
        $this->db->limit($limit, $start);
        $this->db->order_by('t_id', 'DESC');
        $this->db->from('types');
        $this->db->join('comments', 'comments.c_type_id = types.t_id');
        $query = $this->db->get();

        if($query->num_rows() > 0)
        {
            foreach($query->result() as $row)
            {
                $data[] = $row;
            }
            return $data;
        }
        return false;
    }

    public function addComment()
    {
        $text       = $this->input->post('inputComment');
        $authorid   = $this->session->userdata('u_id');
        $typeid     = $this->input->post('t_id');

        $this->db->select('c_date');
        $this->db->where('c_author', $authorid);
        $query = $this->db->get('comments');
        $data = $query->result();

        if($query->num_rows() > 0)
        {
            if($data[0]->c_date - time() < -120)
            {
                $data = array(
                    'c_author'  => $authorid,
                    'c_date'    => time(),
                    'c_show'    => '1',
                    'c_text'    => $text,
                    'c_type_id' => $typeid
                );
                $this->db->insert('comments', $data);
            }
            else
            {
                echo 'poczekaj';
            }
        }
        else
        {
            $data = array(
                    'c_author'  => $authorid,
                    'c_date'    => time(),
                    'c_show'    => '1',
                    'c_text'    => $text,
                    'c_type_id' => $typeid
                );
            $this->db->insert('comments', $data);
        }
    }

**And the view**

    <?php foreach($results as $type) { ?>
                <div class="panel panel-default noborder">
                    <div class="panel-body">
                        <div class="media noboder">
                            <div class="media-left">
                                <img class="media-object" style="width: 48px;" src="https://scontent-waw1-1.xx.fbcdn.net/v/t1.0-9/13413743_1729080443973113_1289643015497858641_n.jpg?oh=98e497c14d65c2102bd9a41777447937&oe=582E9E1E">
                            </div>
                            <div class="media-body">
                                <h4 class="media-heading"><strong>Rafał Chojnowski</strong><small class="pull-right"><?php if($type->t_type_for == '0') { echo 'TYP DARMOWY'; } else { echo 'TYP PREMIUM'; } ?></small></h4>
                                <small><?php echo mdate($dateString, $type->t_date); ?></small><br />
                                <p>
                                    <?php echo $type->t_match.', liga '.$type->t_league.', typ '.$type->t_type.', rozpocznie się '.$type->t_start_date.'. Kurs: '.$type->t_course.' '.$type->t_comment; ?>
                                </p>

                            </div>
                            <hr>
                            <?php foreach($results as $comment) { ?>
                            <div class="media-left">
                                <img class="media-object" style="width: 32px;" src="https://scontent-waw1-1.xx.fbcdn.net/v/t1.0-9/13001028_1523205167987703_4311803603524761516_n.jpg?oh=fb2068c87060975ab7187cde7f1a161f&oe=57EA8DA5">
                            </div>
                            <div class="media-body">
                                <h6 class="media-heading"><strong>Tomasz Chwicewski</strong> <small><?php echo mdate($dateString, $comment->c_date); ?></small></h6>
                                <?php echo $comment->c_text; ?>
                            </div>
                            <br />
                            <?php } ?>
                            <?php $hidden = array('t_id' => $type->t_id, 'c_author' => $this->session->userdata('u_id'), 'comm' => '1'); ?>
                            <?php echo form_open('core/index', '', $hidden); ?>
                            <div class="input-group">
                                <input type="text" class="form-control input-sm noborder" name="inputComment" placeholder="Dodaj komentarz">
                                <span class="input-group-btn">
                                    <button class="btn btn-primary btn-sm noborder" type="submit">Dodaj</button>
                                </span>
                            </div>
                            <?php echo form_close(); ?>
                        </div>
                    </div>
                </div>
                <?php } ?>

0 个答案:

没有答案