消息:未定义的变量:年

时间:2019-06-23 21:58:52

标签: php

在添加/编辑的第48行中失败。我已经做了几件事可以帮助我附加控制器和对应部件并添加。我写/ / / / 控制器:

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Members extends CI_Controller {

    public function __construct() {
        parent::__construct();

        // Load member model
        $this->load->model('Member_model');
        $this->is_logged_in();
        // Load form helper and library
        $this->load->helper('form');
        $this->load->library('form_validation');

        // Load pagination library
        $this->load->library('pagination');


        // Per page limit
        $this->perPage = 5;
    }
    public function is_logged_in()
    {
        $is_logged_in = $this->session->userdata('is_logged_in');
        if(!isset($is_logged_in) || $is_logged_in != true)
        {
            redirect('login');
        }
    }

    public function index(){


        $data = array();
        /* From*/
        $data['year'] = $this->Member_model->get_years();
        /*TO*/

        // Get messages from the session
        if($this->session->userdata('success_msg')){
            $data['success_msg'] = $this->session->userdata('success_msg');
            $this->session->unset_userdata('success_msg');
        }
        if($this->session->userdata('error_msg')){
            $data['error_msg'] = $this->session->userdata('error_msg');
            $this->session->unset_userdata('error_msg');
        }

        // If search request submitted
        if($this->input->post('submitSearch')){
            $inputKeywords = $this->input->post('searchKeyword');
            $searchKeyword = strip_tags($inputKeywords);
            if(!empty($searchKeyword)){
                $this->session->set_userdata('searchKeyword',$searchKeyword);
            }else{
                $this->session->unset_userdata('searchKeyword');
            }
        }elseif($this->input->post('submitSearchReset')){
            $this->session->unset_userdata('searchKeyword');
        }
        $data['searchKeyword'] = $this->session->userdata('searchKeyword');

        // Get rows count
        $conditions['searchKeyword'] = $data['searchKeyword'];
        $conditions['returnType']    = 'count';
        $rowsCount = $this->Member_model->getRows($conditions);

        // Pagination config
        $config['base_url']    = base_url().'members/index/';
        $config['uri_segment'] = 3;
        $config['total_rows']  = $rowsCount;
        $config['per_page']    = $this->perPage;

        // Initialize pagination library
        $this->pagination->initialize($config);

        // Define offset
        $page = $this->uri->segment(3);
        $offset = !$page?0:$page;

        // Get rows
        $conditions['returnType'] = '';
        $conditions['start'] = $offset;
        $conditions['limit'] = $this->perPage;
        $data['members'] = $this->Member_model->getRows($conditions);
        $data['title'] = 'Medlems Liste';

        // Load the list page view
        $this->load->view('templates1/header', $data);
        $this->load->view('members/index', $data);
        $this->load->view('templates/footer');
    }

    public function view($id){
        $data = array();

        // Check whether member id is not empty
        if(!empty($id)){
            $data['member'] = $this->Member_model->getRows(array('id' => $id));;
            $data['title'] = 'Member Details';

            // Load the details page view
            $this->load->view('templates1/header', $data);
            $this->load->view('members/view', $data);
            $this->load->view('templates/footer');
        }else{
            redirect('members');
        }
    }

    public function add(){
        $data = array();
        $memData = array();

        // If add request is submitted
        if($this->input->post('memSubmit')){
            // Form field validation rules
            $this->form_validation->set_rules('phone', 'phone', 'required');
            $this->form_validation->set_rules('name', 'name', 'required');
            $this->form_validation->set_rules('adress', 'adress', 'required');
            $this->form_validation->set_rules('zipcode', 'zipcode', 'required');
            $this->form_validation->set_rules('city', 'city', 'required');
            $this->form_validation->set_rules('email', 'email', 'required|valid_email');
            $this->form_validation->set_rules('year', 'year', 'required');

            // Prepare member data
            $memData = array(
                'phone'=> $this->input->post('phone'),
                'name'=> $this->input->post('name'),
                'adress'=> $this->input->post('adress'),
                'zipcode'=> $this->input->post('zipcode'),
                'email'=> $this->input->post('email'),
                'city'=> $this->input->post('city'),
                'year'=> $this->input->post('year')
            );

            // Validate submitted form data
            if($this->form_validation->run() == true){
                // Insert member data
                $insert = $this->Member_model->insert($memData);

                if($insert){
                    $this->session->set_userdata('success_msg', 'Member has been added successfully.');
                    redirect('members');
                }else{
                    $data['error_msg'] = 'Some problems occured, please try again.';
                }
            }
        }

        $data['member'] = $memData;
        $data['title'] = 'Add Member';

        // Load the add page view
        $this->load->view('templates1/header', $data);
        $this->load->view('members/add-edit', $data);
        $this->load->view('templates/footer');
    }

    public function edit($id){
        $data = array();

        // Get member data
        $memData = $this->Member_model->getRows(array('id' => $id));

        // If update request is submitted
        if($this->input->post('memSubmit')){
            // Form field validation rules
            $this->form_validation->set_rules('phone', 'phone', 'required');
            $this->form_validation->set_rules('name', 'name', 'required');
            $this->form_validation->set_rules('adress', 'adress', 'required');
            $this->form_validation->set_rules('zipcode', 'zipcode', 'required');
            $this->form_validation->set_rules('city', 'city', 'required');
            $this->form_validation->set_rules('email', 'email', 'required|valid_email');
            $this->form_validation->set_rules('year', 'year', 'required');

            // Prepare member data
            $memData = array(
                'phone'=> $this->input->post('phone'),
                'name'=> $this->input->post('name'),
                'adress'=> $this->input->post('adress'),
                'zipcode'=> $this->input->post('zipcode'),
                'city'=> $this->input->post('city'),
                'email'=> $this->input->post('email'),
                'year'=> $this->input->post('year')
            );

            // Validate submitted form data
            if($this->form_validation->run() == true){
                // Update member data
                $update = $this->Member_model->update($memData, $id);

                if($update){
                    $this->session->set_userdata('success_msg', 'Member has been updated successfully.');
                    redirect('members');
                }else{
                    $data['error_msg'] = 'Some problems occured, please try again.';
                }
            }
        }

        $data['member'] = $memData;
        $data['title'] = 'Update Member';

        // Load the edit page view
        $this->load->view('templates1/header', $data);
        $this->load->view('members/add-edit', $data);
        $this->load->view('templates/footer');
    }

    public function delete($id){
        // Check whether member id is not empty
        if($id){
            // Delete member
            $delete = $this->Member_model->delete($id);

            if($delete){
                $this->session->set_userdata('success_msg', 'Member has been removed successfully.');
            }else{
                $this->session->set_userdata('error_msg', 'Some problems occured, please try again.');
            }
        }

        // Redirect to the list page
        redirect('members');
    }
}

MODEL:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Member_model extends CI_Model{

    function __construct() {
        // Set table name
        $this->table = 'members';
    }

    /*
     * Fetch members data from the database
     * @param array filter data based on the passed parameters
     */

    function getRows($params = array()){
        $this->db->select('*');
        $this->db->from($this->table);

        if(array_key_exists("conditions", $params)){
            foreach($params['conditions'] as $key => $val){
                $this->db->where($key, $val);
            }
        }

        if(!empty($params['searchKeyword'])){
            $search = $params['searchKeyword'];
            $likeArr = array('phone' => $search, 'name' => $search, 'email' => $search);
            $this->db->or_like($likeArr);
        }

        if(array_key_exists("returnType",$params) && $params['returnType'] == 'count'){
            $result = $this->db->count_all_results();
        }else{
            if(array_key_exists("id", $params)){
                $this->db->where('id', $params['id']);
                $query = $this->db->get();
                $result = $query->row_array();
            }else{
                $this->db->order_by('name', 'asc');
                if(array_key_exists("start",$params) && array_key_exists("limit",$params)){
                    $this->db->limit($params['limit'],$params['start']);
                }elseif(!array_key_exists("start",$params) && array_key_exists("limit",$params)){
                    $this->db->limit($params['limit']);
                }

                $query = $this->db->get();
                $result = ($query->num_rows() > 0)?$query->result_array():FALSE;
            }
        }

        // Return fetched data
        return $result;
    }

    /*
     * Insert members data into the database
     * @param $data data to be insert based on the passed parameters
     */
    public function insert($data = array()) {
        if(!empty($data)){
            // Add created and modified date if not included
            /*if(!array_key_exists("created", $data)){
                $data['created'] = date("Y-m-d H:i:s");
            }
            if(!array_key_exists("modified", $data)){
                $data['modified'] = date("Y-m-d H:i:s");
            }*/

            // Insert member data
            $insert = $this->db->insert($this->table, $data);

            // Return the status
            return $insert?$this->db->insert_id():false;
        }
        return false;
    }

    /*
     * Update member data into the database
     * @param $data array to be update based on the passed parameters
     * @param $id num filter data
     */
    public function update($data, $id) {
        if(!empty($data) && !empty($id)){
            // Add modified date if not included
            /*if(!array_key_exists("modified", $data)){
                $data['modified'] = date("Y-m-d H:i:s");
            }*/

            // Update member data
            $update = $this->db->update($this->table, $data, array('id' => $id));

            // Return the status
            return $update?true:false;
        }
        return false;
    }

    /*
     * Delete member data from the database
     * @param num filter data based on the passed parameter
     */
    public function delete($id){
        // Delete member data
        $delete = $this->db->delete($this->table, array('id' => $id));

        // Return the status
        return $delete?true:false;
    }
    /*from*/
    public function get_years()
    {
           $results = $this->db->select('id, year')
                               ->get('year')
                               ->result_array();

           return array_column($results, 'year', 'id');
    } 
    /*to*/
}

并查看:

<div class="container">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
    <h2><?php echo $title; ?></h2>

    <!-- Display status message -->
    <?php if(!empty($success_msg)){ ?>
    <div class="col-xs-12">
        <div class="alert alert-success"><?php echo $success_msg; ?></div>
    </div>
    <?php }elseif(!empty($error_msg)){ ?>
    <div class="col-xs-12">
        <div class="alert alert-danger"><?php echo $error_msg; ?></div>
    </div>
    <?php } ?>

    <div class="row">
        <div class="col-md-6">
            <form method="post">
                <div class="form-row">
                <div class="col-md-6 mb-3">
                        <label>Telefon</label>
                        <input type="text" class="form-control" name="phone" placeholder="Enter phone" value="<?php echo !empty($member['phone'])?$member['phone']:''; ?>" >
                        <?php echo form_error('phone','<div class="invalid-feedback">','</div>'); ?>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label>Navn</label>
                        <input type="text" class="form-control" name="name" placeholder="Enter name" value="<?php echo !empty($member['name'])?$member['name']:''; ?>" >
                        <?php echo form_error('first_name','<div class="invalid-feedback">','</div>'); ?>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label>Adress</label>
                        <input type="text" class="form-control" name="adress" placeholder="Enter adress" value="<?php echo !empty($member['adress'])?$member['adress']:''; ?>" >
                        <?php echo form_error('adress','<div class="invalid-feedback">','</div>'); ?>
                    </div>          
                    <div class="col-md-6 mb-3">
                        <label>PostNr</label>
                        <input type="text" class="form-control" name="zipcode" placeholder="Enter zipcode" value="<?php echo !empty($member['zipcode'])?$member['zipcode']:''; ?>" >
                        <?php echo form_error('zipcode','<div class="invalid-feedback">','</div>'); ?>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label>By</label>
                        <input type="text" class="form-control" name="city" placeholder="Enter city" value="<?php echo !empty($member['city'])?$member['city']:''; ?>" >
                        <?php echo form_error('city','<div class="invalid-feedback">','</div>'); ?>
                    </div> 
                    <!--from-->
                    <div class="col-md-6 mb-3">
                        <label>År</label>
                        <?php echo form_dropdown('id', $years); ?>
                    </div> 
            <!--to-->
                <div class="form-group">
                    <label>Email</label>
                    <input type="text" class="form-control" name="email" placeholder="Enter email" value="<?php echo !empty($member['email'])?$member['email']:''; ?>" >
                    <?php echo form_error('email','<div class="invalid-feedback">','</div>'); ?>
                </div>

                <a href="<?php echo site_url('members'); ?>" class="btn btn-secondary">Back</a>
                <input type="submit" name="memSubmit" class="btn btn-success" value="Submit">
            </form>
        </div>
    </div>
</div>

0 个答案:

没有答案