我得到一个"未定义的变量"从数据库中获取时CodeIgniter中的错误

时间:2017-02-01 05:36:41

标签: php mysql codeigniter

从数据库中提取时,我在codeigniter中收到错误。

这是我的错误:

  

遇到PHP错误

     

消息:未定义的变量:结果

     

行号:122

这是我的代码:

  <?php $via = $this->session->userdata('firm_name');
if(!isset($firm_name)){ redirect ('Welcome');}
?>      
<link href="<?php echo base_url("assets/css/radiostyle.css"); ?>" rel="stylesheet">
<style type="text/css">
              .page-heading {
            border-top: 0;
            padding: 0px 10px 10px 10px;
        }
        input, button, select, textarea{
          margin: 10px;
        }
              .ibox-content {
          background-color: #ffffff;
          color: inherit;
          padding: 15px 20px 20px 20px;
          border-color: #e7eaec;
          border-image: none;
          border-style: none; 
          border-width: 1px 0px;
      }
      </style>

            <div class="row wrapper border-bottom white-bg page-heading">
                <div style="float: left;" class="col-lg-4">
                    <img style="    width: 90px;
    height: 90px;     padding-top: 5px;" src="http://www.miisky.com/ci/GST.png" alt="GST Logo">

                </div>
                <div class="col-lg-4 text-center">
                    <h1 style="    padding-top: 7px;
    padding-right: 20%;">Know Your Customers</h1>

                </div>
                <div style="float: right;" class="col-lg-4">
                    <h1 style="padding-top:9px"><?php echo "Date: ". date("d/m/Y");?></h1>

                </div>
            </div>
            <br><div style="text-align: center;"> 
            <label class="control control--radio">Manufacture
      <input type="radio" onclick="document.location='#';" name="radio" checked="checked"/>
      <div class="control__indicator"></div>
    </label>
    <label class="control control--radio">Service
      <input type="radio" onclick="document.location='#';" name="radio" />
      <div class="control__indicator"></div>
    </label>
     <label class="control control--radio">Trading
      <input type="radio" onclick="document.location='#';" name="radio" />
      <div class="control__indicator"></div>
    </label>
    <label class="control control--radio">Others
      <input type="radio" onclick="document.location='#';" name="radio" />
      <div class="control__indicator"></div>
    </label>



    </div>
            <div class="fh-breadcrumb">

                <div class="full-height">
                    <div class="full-height-scroll white-bg border-left">
                      <?php echo $this->session->flashdata('msg'); ?>
                        <div class="element-detail-box">

                            <div class="tab-content">
                               <div class="ibox-content">

                            <div class="form-group">

                                <div class="col-lg-12">
                                    <div class="row">

                                    <?php $this->load->helper('form'); ?>
                                    <?php $attributes = array("name" => "Categoryform","autocomplete"=>"off");
                                    echo form_open("Gst_customer/customersearch/", $attributes);?>
                                                <div class="col-lg-6">
                                                        <input name="CustomerName" type="text" placeholder="CustomerName" value="<?php echo set_value('CustomerName'); ?>" class="form-control" required>
                                                        <span class="text-danger"><?php echo form_error('CustomerName'); ?></span>
                                                    </div>


                                                    <div class="col-lg-12">
                                                       <?php echo form_submit('Search', 'Search','class="btn btn-info"'); ?>
                                                    </div> 

                                      <?php echo form_close(); ?>

                                    </div>

                                </div>
                            </div>

            </div>





                            </div>

                        </div>

                    </div>
                </div>
<div class="full-height">
                      <div class="full-height-scroll white-bg border-left">
                         <?php echo $this->session->flashdata('message'); ?>
                          <div class="element-detail-box">
                            <div class="table-responsive">
              <?php if(isset($results)){ ?>
<table class='table table-bordered'>
                    <thead>
                      <tr>
                      <th> Customer Name </th>
                      <th> Customer Id</th>
                      <th> Customer Code</th>
                      </tr>
                    </thead>
                      <tbody>
                          <?php foreach($results as $row){ ?>
                                    <tr>
                                         <td><?php echo $row->CustomerName;?></td>
                                         <td><?php echo $row->CustomerID;?></td>
                                         <td><?php echo $row->CustomerCode;?></td>
                                    </tr>

                          <?php } ?>


                      </tbody>
                  </table>
     <?php } ?>             

                  </tbody>
              </table>












                          </div>
                          </div>

                      </div>
                  </div>


            </div>

这是我的控制器文件:

<?php
class Gst_customer extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('url','html'));
        $this->load->library('session');
        $this->load->database();
        $this->load->model('user_model');

    }   

    function index()
    {
        $details = $this->user_model->get_user_by_id($this->session->userdata('id'));

        $data['firm_name'] = $details[0]->firm_name;
        $data['email'] = $details[0]->email;
        $data['vault_no'] = $details[0]->vault_no;
        $data['active'] = 'know_your_customer';
        $data['mobile'] = $details[0]->mobile;

        $this->load->view('view_header', $data);
        $this->load->view('view_menu', $data);
        $this->load->view('gst_customer_view', $data);
        $this->load->view('view_footer');
    }

     function customersearch()
    {
        $CustomerName    =   $this->input->post('CustomerName');
        $data['results']    =   $this->user_model->search($CustomerName);
        $results = $data['results'];
       $this->load->view('view_header', $data);
        $this->load->view('view_menu', $data);
        $this->load->view('gst_customer_view', $data);
        $this->load->view('view_footer');
    }

    function logout()
    {
    $user_data = $this->user_model->get_user_by_id($this->session->userdata('id'));
        foreach ($user_data as $key => $value) {
            if ($key != 'session_id' && $key != 'ip_address' && $key != 'user_agent' && $key != 'last_activity') {
                $this->session->unset_userdata($key);
            }
        }
    $this->session->sess_destroy();

    redirect('Welcome', 'refresh');
    }
}

这是我的模特: 的 module.php

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

    class User_model extends CI_Model
    {
        function __construct()
        {
            parent::__construct();
        }



function search($CustomerName,$CustomerID)
    {
        $this->db->like('CustomerName',$CustomerName);
        $this->db->like('CustomerID',$CustomerID);
        $query  =   $this->db->get('gst_customermaster');
        return $query->result();
    }

    }?>

2 个答案:

答案 0 :(得分:1)

在您的视图中。如果设置了$results数组,则显示表格。因为您正在将不同功能的不同数据发送到同一视图。

喜欢这个..

<?php if(isset($results)){ ?>
<table class='table table-bordered'>
                    <thead>
                      <tr>
                      <th> Customer Name </th>
                      <th> Customer Id</th>
                      <th> Customer Code</th>
                      </tr>
                    </thead>
                      <tbody>
                          <?php foreach($results as $row){ ?>
                                    <tr>
                                         <td><?php echo $row->CustomerName;?></td>
                                         <td><?php echo $row->CustomerID;?></td>
                                         <td><?php echo $row->CustomerCode;?></td>
                                    </tr>

                          <?php } ?>


                      </tbody>
                  </table>
     <?php } ?>             

答案 1 :(得分:0)

获取索引中的所有数据。试试这个:

<强> module.php

class User_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }

    function search($CustomerName)
    {
        $this->db->like('CustomerName',$CustomerName);
        $query  =   $this->db->get('gst_customermaster');
        return $query->result();
    }

    function getAll()
    {            
        $query  =   $this->db->get('gst_customermaster');
        return $query->result();
    }

}?>`enter code here`

控制器文件:

 <?php
class Gst_customer extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('url','html'));
        $this->load->library('session');
        $this->load->database();
        $this->load->model('user_model');

    }   

function index()
    {
        $details = $this->user_model->get_user_by_id($this->session->userdata('id'));

        $data['firm_name'] = $details[0]->firm_name;
        $data['email'] = $details[0]->email;
        $data['vault_no'] = $details[0]->vault_no;
        $data['active'] = 'know_your_customer';
        $data['mobile'] = $details[0]->mobile;
        //$data['results']  =   $this->user_model->getAll();
        $data['results']  =   array();
        $this->load->view('view_header', $data);
        $this->load->view('view_menu', $data);
        $this->load->view('gst_customer_view', $data);
        $this->load->view('view_footer');
    }

     function customersearch()
    {
         $results = $this->user_model->get_user_by_id($this->session->userdata('id'));
         $data['firm_name'] = $results[0]->firm_name;
           $id = $this->input->post('id');
       $data['results']=array();
        $CustomerName    =   $this->input->post('CustomerName');
       if(trim($CustomerName) != "")
       {
        $data['results']    =   $this->user_model->search($CustomerName);
       }
        $results = $data['results'];
       $this->load->view('view_header', $data);
        $this->load->view('view_menu', $data);
        $this->load->view('gst_customer_view', $data);
        $this->load->view('view_footer');
    }

    function logout()
    {
    $user_data = $this->user_model->get_user_by_id($this->session->userdata('id'));
        foreach ($user_data as $key => $value) {
            if ($key != 'session_id' && $key != 'ip_address' && $key != 'user_agent' && $key != 'last_activity') {
                $this->session->unset_userdata($key);
            }
        }
    $this->session->sess_destroy();

    redirect('Welcome', 'refresh');
    }
}