【问题标题】:View from model and controller in codeigniter在 codeigniter 中从模型和控制器查看
【发布时间】:2017-06-14 07:29:54
【问题描述】:
        **Controller file i have form_ctrl.php and code given below**
        <?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_fun('form', $data);

                                               }
                }
         function GetAll()
          {
             $this->load->model('emp_model');
               $data['query']=$this->emp_model->emp_getall();
               $this->load->view('emp_viewall',$data);
          }

              }


    **model file i have data_model.php**

    <?php
    class data_model extends CI_Model {

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

        public function insert_fun($tableName,$data){

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

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


    ?>


view file i have results.php

<html>
<head>
<title>My Form</title>
</head>
<body>
<table width="100%" border="1">
<tr>
    <td>Name</td>
    <td>Email</td>
    <td>Mobile</td>
    <td>Address</td>
    <td>Action</td>
</tr>
 <?php
foreach($query as $row)
{
  print_r($row);exit;

}
?> 
<tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
</tr>


</table>


</body>
</html>

数据插入正确,但视图未显示数据库字段,这意味着 IN 模型文件函数(函数 emp_getall())或控制器文件(函数 GetAll())不起作用给我解决方案,该代码中的错误...

【问题讨论】:

    标签: codeigniter


    【解决方案1】:
    function GetAll()
              {
                 $this->load->model('emp_model');
                   $data['query']=$this->emp_model->emp_getall();
                   $this->load->view('results',$data);
              }
    

    你错误的视图调用检查这个

    【讨论】:

    • 兄弟你 print_r 在控制器中你得到记录或在你的视图中传递数据后不检查
    • hothi jimit bro i m newbe in codeigniter 但它无法正常工作,没有获得任何价值
    【解决方案2】:
        <html>   <head>
    <title>My Form</title>
    </head>
    <body>
    <table width="100%" border="1">
    <tr>
        <td>Name</td>
        <td>Email</td>
        <td>Mobile</td>
        <td>Address</td>
        <td>Action</td>
    </tr>
     <?php
    foreach($query as $row) : ?>
    <tr>
        <td><?php echo $row->name;?></td>
        <td><?php echo $row->email;?></td>
        <td><?php echo $row->mobile;?></td>
        <td><?php echo $row->address;?></td>
        <td><?php echo $row->action;?></td>
    </tr>
    
    
    </table>
    <?php endforeach ?>
    
    </body>
    </html>
    

    尝试一次......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-10
      • 1970-01-01
      • 1970-01-01
      • 2012-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多