dtf76989 2017-11-18 06:57
浏览 88
已采纳

更新和flashdata消息无法在CodeIgniter上工作

I've been working on a new CodeIgniter application and so far I've been able to create "page_categories" into the table, now my problem is when I try to update a certain category.

Let's say that I have two records on page_categories table:

enter image description here

Ok, now to be more specific when I try to change the name of "Page Category Two" rather than changing that name, my form updates the first record "Page Category One". Any idea how to fix this?

This is what I have on my controller:

public function edit($id){
    $this->form_validation->set_rules('name', 'Name', 'trim|required|min_length[3]');

    if($this->form_validation->run() == FALSE){
        // Get Current Subject
        $data['item'] = $this->Pages_categories_model->get($id);

        // Load template
        $this->template->load('admin', 'default', 'pages_categories/edit', $data);
    } else {
        $old_name = $this->Pages_categories_model->get($id)->name;
        $new_name = $this->input->post('name');

        // Create Post Array
        $data = array(
            'name'  => $this->input->post('name')
        );

        // Insert Subject
        $this->Pages_categories_model->update($id, $data);

        // Activity Array
        $data = array(
            'resource_id'   => $this->db->insert_id(),
            'type'          => 'subject',
            'action'        => 'updated',
            'user_id'       => 1,
            'message'       => 'A page category ('.$old_name.') was renamed to ('.$new_name.')'
        );

        // Insert Activity
        $this->Activity_model->add($data);

        // Set Message
        $this->session->set_flashdata('success', 'Page category has been updated');

        //Redirect
        redirect('admin/pages_categories');
    }
}

and this is what I have on my model:

<?php
class Pages_categories_model extends CI_MODEL{
    function __construct(){
        parent::__construct();
        $this->table = 'pages_categories';
    }

    public function get_list(){
        $query = $this->db->get($this->table);
        return $query->result();
    }

    public function get($id){
        $query = $this->db->get($this->table);
        $this->db->where('id', $id);
        return $query->row();
    }

    public function add($data){
        $this->db->insert($this->table, $data);
    }

    public function update($id, $data){
        $this->db->where('id', $id);
        $this->db->update($this->table, $data);
    }

    public function delete($id){
        $this->db->where('id', $id);
        $this->db->delete($this->table);
    }
}

and finally my view:

<h2 class="page-header">Edit Page Category</h2>

<?php echo form_open('admin/pages_categories/edit/'.$item->id); ?>
    <div class="form-group">
        <?php echo form_label('Page Category Name', 'name'); ?>
        <?php
            $data = array(
                'name' => 'name',
                'id'    => 'name',
                'maxlength' => '100',
                'class'     => 'form-control',
                'value'     => $item->name,
            );
        ?>
        <?php echo form_input($data); ?>
    </div>

    <?php echo form_submit('mysubmit', 'Update Page Category', array('class' => 'btn btn-primary')); ?>
<?php echo form_close(); ?>

I would like to say that my flash data message is not working as I expected. (It's not that important though.)

I want the flash-data message to be displayed in a different view; here it is my view:

<h2 class="page-header">Page Categories</h2>
<?php $CI =& get_instance(); if($CI->session->flashdata('success'){ ?>
           <div class="alert alert-success">
              <strong>Success!! </strong><?php echo $this->session->flashdata('success'); ?>
           </div>
     <?php } ?>
<?php if($this->session->flashdata('error')) : ?>
    <?php echo '<div class="alert alert-danger">'.$this->session->flashdata('error').'</div>'; ?>
<?php endif; ?>




<?php if($subjects) : ?>
<table class="table table-striped">
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Date Created</th>
        <th></th>
    </tr>
    <?php foreach($subjects as $subject) : ?>
    <?php $date = strtotime($subject->create_date); ?>
    <?php $formatted_date = date('F j, Y, g:i a', $date); ?>
        <tr>
            <td><?php echo $subject->id; ?></td>
            <td><?php echo $subject->name; ?></td>
            <td><?php echo $formatted_date; ?></th>
            <td>
                <?php echo anchor('admin/pages_categories/edit/'.$subject->id.'', 'Edit', 'class="btn btn-default"'); ?>
                <?php echo anchor('admin/pages_categories/delete/'.$subject->id.'', 'Delete', 'class="btn btn-danger"'); ?>
            </td>
        </tr>
    <?php endforeach; ?>
</table>
<?php else : ?>
    <div class="alert alert-danger">No Page Category Found</div>
<?php endif; ?>

</div>
  • 写回答

2条回答 默认 最新

  • dream02008 2017-11-18 07:53
    关注

    Add the where clause before the query in this code in your model

    public function get($id){
        $this->db->where('id', $id);  // Give where clause before query
        $query = $this->db->get($this->table);
    
        return $query->row();
    }
    

    EDIT: Adding answer for flashdata

    Well, for flashdata, You have to call $_SESSION directly. Source: codeigniter documentation here

    so instead of

    <?php $CI =& get_instance(); if($CI->session->flashdata('success'){ ?>
    

    write

    <?php if(isset($_SESSION['success']) { ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1