douping5226
2014-10-17 04:58
浏览 64
已采纳

通过Codeigniter的控制器类中的视图,将变量的值从一个方法保存到另一个方法

I'm new in php. I'm doing a project on online bus ticket system.

In a part of the system i want to update a table of database. At first i want to display all the rows of the table which i have done in index method. When a row is selected it's attribute value(which is primary key) is caught in 'edit schedule' method. This value is saved in the class variable attr_value. There I call another view to receive updated information. From that view the info's are passed to 'e_schedules' method by form. But there the value of class variable attr_value is reset to default value. But I want the value saved in 'edit_schedule' method. How can i do it?

controller class

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

This is my first post. I have done this much by scrolling other questions and answer. But now i need a quick help because i am running short of time.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongzhang6544 2014-10-17 05:52
    已采纳

    You have to make following changes:

    Controller class

    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>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongpi9494 2014-10-17 05:25

    Your variable $attr_value will always contain the default value because you are initializing the value inside class and no change in value is made using external parameters. You need to pass the schedule_no in the route to use it in your controller. I will recommend following changes:

    Controller

    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>
    

    Please let me know if you require any help understanding the changes I suggested.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题