更新和flashdata消息无法在CodeIgniter

时间:2017-11-18 06:57:22

标签: php html codeigniter

我一直致力于新的CodeIgniter应用程序,到目前为止,我已经能够创建" page_categories"进入表格,现在我的问题是当我尝试更新某个类别时。

我们说在page_categories表上有两条记录:

enter image description here

好的,现在更具体的是当我尝试更改" Page Category Two"我的表单更新了第一个记录" Page Category One"而不是更改该名称。知道如何解决这个问题吗?

这就是我在控制器上的内容:

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

这就是我在模特身上所拥有的:

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

最后我的观点:

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

我想说我的flash数据消息没有按预期工作。 (虽然它并不重要。)

我希望flash数据消息以不同的视图显示;这是我的观点:

&#13;
&#13;
<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; ?>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

在模型中的此代码中添加查询前的where子句

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

    return $query->row();
}

编辑:为flashdata添加答案

嗯,对于flashdata,您必须直接致电$_SESSION。资料来源:codeigniter documentation here

所以而不是

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

<?php if(isset($_SESSION['success']) { ?>

答案 1 :(得分:0)

您是否尝试过回显控制器中的$id?您的代码似乎没有任何问题。

关于Flash数据,您不会在视图中的任何位置使用它。这就是它没有被显示的原因。

<?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 } ?>

这样可以正常工作。

相关问题