0

I am getting error in codeigniter while fetching from the database.

Here is my error:

A PHP Error was encountered

Message: Undefined variable: results

Line Number: 122

Here is my code:

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

This is my controller file:

<?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');
    }
}

This is my model: 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();
    }
    
    }?>
8
  • What is name of view file? And in which action are you getting error? Commented Feb 1, 2017 at 5:44
  • view file name is gst_customer_view Commented Feb 1, 2017 at 6:02
  • may be you are getting this error in index action because in index() you are not passing results in view. Commented Feb 1, 2017 at 6:04
  • i need to fetch the data it is show like empty page...but error is cleared Commented Feb 1, 2017 at 6:18
  • Which data you want on index()? Commented Feb 1, 2017 at 6:25

2 Answers 2

1

In your view.Show table if $results array is set.Because your are sending different data from different function to same view.

Like this..

<?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 } ?>             
Sign up to request clarification or add additional context in comments.

7 Comments

error is cleared...but not able to fetch the data...how to rectify that
you we see table data.only after executing customersearch function.
i have added model also...if any error is there pls tell me
CustomerName,CustomerID and CustomerCode are matching with database columns names or not?check
i am not able to fetch the data from database to display in view page it is coming like blank page...if any error is in model let me know
|
0

To get all data in index. Try this:

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`

controller file:

 <?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');
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.