在codeigniter中使用输出返回警告

时间:2017-06-14 09:28:06

标签: codeigniter

得到警告输出我cudnt找到原因请帮助我

     **Controller file  form_ctrl.php**

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

    class form_ctrl extends CI_Controller {

        public function index()
        {
            //$this->load->view('welcome_message');
                 $this->load->helper(array('form', 'url'));
                 $this->load->library('form_validation');
                 $this->load->model('data_model');

                    //$this->form_validation->set_rules('name', 'Username', 'required');
                  $this->form_validation->set_rules('name', 'name','required|min_length[5]|max_length[12]');
                    $this->form_validation->set_rules('pass', 'Password', 'required',
                            array('required' => 'You must provide a %s.')
                    );
                    $this->form_validation->set_rules('email', 'Email', 'required');
                    $this->form_validation->set_rules('mobile', 'Mobile', 'required');
                    $this->form_validation->set_rules('address', 'Address','required|min_length[5]');

                    if ($this->form_validation->run() == FALSE)
                    {
                            $this->load->view('table');
                    }
                    else
                    {
                            $this->load->view('results');
                            $name=$this->input->post('name');
                            $pass=$this->input->post('pass');
                            $email=$this->input->post('email');
                            $mobile=$this->input->post('mobile');
                             $address=$this->input->post('address');
                                 $data = array(
                                               'name' =>$name ,
                                               'pass' => $pass,
                                               'email' => $email,
                                               'mobile' => $mobile,
                                               'address' => $address
                                               );
                                $this->data_model->insert('form', $data);
                                $this->load->model('data_model');
                                 $val=$this->data_model->emp_getall();
                                 $data['query']=$val;
                                //print_r($data);exit;
                                     $this->load->view('results',$data);


                                           }
            }
    }


    **model file data_model.php**

    <?php
    class Data_model extends CI_Model {

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

        public function insert($tableName,$data){

           return $this->db->insert($tableName, $data);

        }
        function emp_getall()
          {
               $this->load->database();
               $query=$this->db->get('form');
               if($query->num_rows() > 0) {
               $results = $query->result();
               }
              return $results;
          }
    }


    ?>


**view file results.php** 

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<table width="100%" border="1px">
    <tr>
        <td>Name</td>
        <td>Email</td>
        <td>Mobile</td>
        <td>Address</td>
        <td>Action</td>
        </tr>
         <?php
        foreach ($query as $val) {
            ?>
       <tr>
            <td><?php echo $val->name; ?></td>
            <td><?php echo $val->email; ?></td>
            <td><?php echo $val->mobile;?></td>
            <td><?php echo $val->address; ?></td>
            <td><a href="basic/update?uid=<?php echo $val->id; ?>">Edit</a>||<a href="<?php echo base_url() ?>delete/index/<?php echo $val->id; ?>">link</a></td>

        </tr>
<?php } ?>
</table>
    </body>
</html>


problem is that when m getting the results from view it showing me the warning with the result: 
first warning:
A PHP Error was encountered
    Severity: Notice
    Message: Undefined variable: query
    Filename: views/results.php
    Line Number: 16

第二次警告:     遇到PHP错误         严重性:警告         消息:为foreach()提供的参数无效         文件名:views / results.php         行号:16

and page also not redirecting to the next url that is results.php.what is the issue in this code y m getting warning and why page is not redirecting

2 个答案:

答案 0 :(得分:1)

您已在同一个控制器中加载了两次视图页

$this->load->view('results');

 $this->load->view('results',$data);

删除第一个

  

$这 - &GT;负载&gt;查看( '结果',$数据);

答案 1 :(得分:0)

尝试将查询更改为结果!!!

并且还使用error_reporting(0);(在函数__construct中的控制器中)

foreach( $ results as $ val)