【问题标题】:Preserve a variable's value from one method to another method via a view in controller class of Codeigniter通过 Codeigniter 控制器类中的视图将变量的值从一种方法保留到另一种方法
【发布时间】:2014-10-17 04:58:34
【问题描述】:

我是 php 新手。我正在做一个关于在线巴士票系统的项目。

在系统的一部分中,我想更新一个数据库表。起初我想显示我在索引方法中完成的表的所有行。 When a row is selected it's attribute value(which is primary key) is caught in 'edit schedule' method.该值保存在类变量 attr_value 中。在那里我调用另一个视图来接收更新的信息。从该视图中,信息按表单传递给“e_schedules”方法。但是在那里类变量 attr_value 的值被重置为默认值。但我希望保存在“edit_schedule”方法中的值。我该怎么做?

控制器类

<?php

//$flag_edit_schedule = 1 ;

class edit_schedule extends CI_Controller {

    //public $flag_edit_schedule = 1 ;
    public $attr_value=1;
    protected $table= 'schedule';

    function index() {

        $data= array();

        if($query = $this->database_model->get_records($this->table)){
            $data['records']= $query;
        }
        $this->load->view('select_schedule_view',$data) ;
    }

    function e_schedules(){
        //$table='schedule';
        $attribute_name='schedule_no';
        $attribute_value=$this->attr_value;
        echo $this->attr_value;
        $data = array(
            'schedule_no' => $this->attr_value,
            'bus_no' => $this->input->post('bus_no'),
            'route_no' => $this->input->post('route_no'),
            'start_time' => $this->input->post('start_time')
        );
//$this->database_model->update_record($this->table,$attribute_name,$attribute_value,$data);
        //redirect('/admin/admin_home');    
        echo $attribute_name;   
    }

    function edit_schedules() {
        $this->attr_value=$this->uri->segment(4) ;
        echo $this->attr_value;
        $this->load->view('edit_schedule_view');
    }
}

select_schedule_view

<!DOCTYPE html>

<head>
 <meta charset="utf-8">
 <title></title>
 <style>
 table, th, td {
    padding: 5px;
 }
 th,td {
    text-align: center;
}
 table {
    border-spacing: 15px;
 }
 </style>
</head>

<body>
<h2>Select Schedule </h2>

    <table style="width:75%">
    <tr>
        <th> bus_no </th>
        <th> route_no </th>
        <th> time </th>
    </tr>

    <?php if(isset($records)) : foreach($records as $rows) : ?>

        <tr>
            <td><?php echo anchor("admin/edit_schedule/edit_schedules/$rows->schedule_no", $rows->bus_no); ?></td>
            <td> <?php echo $rows->route_no?> </td>
            <td> <?php echo $rows->start_time ?></td>
        </tr>


    <?php endforeach; ?>

    <?php else: ?>
        <h2> No records were returned </h2>

    <?php endif; ?>
    </table>

</body>

edit_schedule_view

<!DOCTYPE html>

<head>
 <meta charset="utf-8">
 <title></title>
</head>

<body>
<h2> Edit Route </h2>
    <?php echo form_open('admin/edit_schedule/e_schedules'); ?>


    <p>
        <label for="bus_no"> Enter Bus Number:</label>
        <input type="text" name="bus_no" id="bus_no" />
    </p>

    <p>
        <label for="route_no"> Enter Route Number:</label>
        <input type="text" name="route_no" id="route_no" />
     </p>

     <p>
        <label for="start_time"> Start Time:</label>
        <input type="text" name="start_time" id="start_time" />
     </p>

    <p>
    <input type="submit" value="Edit" />

    </p>

    <?php echo form_close(); ?>

    <hr />
</body>

这是我的第一篇文章。通过滚动其他问题和答案,我已经做了很多。但现在我需要快速帮助,因为我的时间不多了。

【问题讨论】:

  • 您到底想做什么?您是否正在尝试执行编辑操作?
  • 是的。我想对表格进行编辑操作。
  • 为什么你被这个 $attr_value 占用了?我认为这不是必需的。
  • 将主键从“edit_schedule”传递给“e_schedule”。还有其他人可以执行此操作吗?
  • 您必须从您的edit_schedule 数据库中的id 获取数据并将其传递给您的edit_schedule_view。比您能够在 e_schedule 中取回您的主键。

标签: php codeigniter


【解决方案1】:

您必须进行以下更改:

控制器类

function edit_schedules($schedule_no) {
    $this->db->select('*');
    $this->db->from($this->table);
    $this->db->where('schedule_no',$schedule_no);
    $query = $this->db->get();
    $data['current_schedule'] = $query->result();
    $this->load->view('edit_schedule_view',$data);
}

function e_schedules(){
    $data = array(
        'bus_no' => $this->input->post('bus_no'),
        'route_no' => $this->input->post('route_no'),
        'start_time' => $this->input->post('start_time')
    );
    $this->db->where('schedule_no',$this->input->post('schedule_no');
    $this->db->update($this->table);
    redirect('/admin/admin_home');    
}

edit_schedule_view

<!DOCTYPE html>

<head>
    <meta charset="utf-8">
    <title></title>
</head>

<body>
<h2> Edit Route </h2>
    <?php echo form_open('admin/edit_schedule/e_schedules'); ?>

    <p>
        <label for="bus_no"> Enter Bus Number:</label>
        <input type="text" name="bus_no" id="bus_no" value="<?php echo $current_schedule->bus_no; ?>" />
    </p>

    <p>
        <label for="route_no"> Enter Route Number:</label>
        <input type="text" name="route_no" id="route_no" value="<?php echo $current_schedule->route_no; ?>" />
     </p>

     <p>
        <label for="start_time"> Start Time:</label>
        <input type="text" name="start_time" id="start_time" value="<?php echo $current_schedule->start_time ?>" />
     </p>

    <p>
    <input type="hidden" name="schedule_no" value="<?php echo $current_schedule->id; ?>" />
    <input type="submit" value="Edit" />

    </p>

    <?php echo form_close(); ?>

    <hr />
</body>

【讨论】:

  • 我已经按照您的建议进行了编辑。我带来了一些更改,因为我没有将任何变量传递给“edit_schedule”方法。我通过“$this->uri->segment(4)”得到它。如果我有,那么是否有必要运行查询?我不是已经有了吗?我只是将它传递给视图,正如您建议的那样,我制作了一个隐藏的表单。但是我的数据库仍然没有更新。我正在发布我所做的事情。
  • 如果您不想这样做,我们正在运行查询以获取 current_schedule 的所有数据,而不是直接将 schedule_no 传递给您的视图。您必须将 schedule_no 传递给您的模型才能更新特定记录。
  • 在您提出建议之前,我已经添加了一个答案。我不知道如何在cmets中添加代码。
  • 您无法在 cmets 中格式化您的代码,因此如果需要,只需粘贴即可。
  • 我们不需要传递数组来更新吗?我正在完美地完成 schedule_no 并且已经完成了你所做的。但它仍然无法正常工作。
【解决方案2】:

您的变量$attr_value 将始终包含默认值,因为您正在初始化类内部的值,并且使用外部参数不会更改值。您需要在路由中传递 schedule_no 才能在控制器中使用它。我将建议以下更改:

控制器

function e_schedules($schedule_no){

    //$table='schedule';
    $attribute_name='schedule_no';
    $attribute_value=$schedule_no;
    $this->attr_value = $schedule_no;
    echo $this->attr_value;
    $data = array(
        'schedule_no' => $this->attr_value,
        'bus_no' => $this->input->post('bus_no'),
        'route_no' => $this->input->post('route_no'),
        'start_time' => $this->input->post('start_time')
    );
//$this->database_model->update_record($this->table,$attribute_name,$attribute_value,$data);
    //redirect('/admin/admin_home');    
    echo $attribute_name;   
}

function edit_schedules() {
    $this->attr_value=$this->uri->segment(4) ;
    echo $this->attr_value;
    $this->load->vars($this->attr_value);
    $this->load->view('edit_schedule_view');
}

edit_schedule_view

<!DOCTYPE html>
 <head>
  <meta charset="utf-8">
   <title></title>
  </head>

   <body>
   <h2> Edit Route </h2>
   <?php echo form_open('admin/edit_schedule/e_schedules/' . $attr_value); ?>


<p>
    <label for="bus_no"> Enter Bus Number:</label>
    <input type="text" name="bus_no" id="bus_no" />
</p>

<p>
    <label for="route_no"> Enter Route Number:</label>
    <input type="text" name="route_no" id="route_no" />
 </p>

 <p>
    <label for="start_time"> Start Time:</label>
    <input type="text" name="start_time" id="start_time" />
 </p>

<p>
<input type="submit" value="Edit" />

</p>

<?php echo form_close(); ?>

<hr />
  </body>

如果您需要任何帮助来理解我建议的更改,请告诉我。

【讨论】:

  • 感谢您的回复。我不清楚“$this->load->vars($this->attr_value)”。我们为什么不将它作为 load->view() 的第二个参数传递。而且它没有在 edit_schedule_view 中识别 $attr_value。
  • 是的,您也可以将其作为第二个参数传递。 load-&gt;vars 只是一种替代语法,你可以随意命名,不一定是$attr_value
猜你喜欢
  • 1970-01-01
  • 2019-01-22
  • 1970-01-01
  • 1970-01-01
  • 2013-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-19
相关资源
最近更新 更多