保留一个变量';s值通过Codeigniter的控制器类中的视图从一个方法传递到另一个方法


Preserve a variable's value from one method to another method via a view in controller class of Codeigniter

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

在系统的一部分,我想更新数据库的一个表。起初,我想显示表中的所有行,这是我在index方法中完成的。当一行被选中时,它的属性值(它是主键)被捕获在"编辑计划"方法中。该值保存在类变量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>

编辑日程表查看

<!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>

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

您必须进行以下更改:

控制器类

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>

您的变量$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');
}

编辑日程表查看

<!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>

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