Home > Enterprise >  Unable to retrieve status count after passing Mysql query
Unable to retrieve status count after passing Mysql query

Time:10-06

Currently I'm using CodeIgniter to retrieve my data in a particular timeframe. All these entries have a status. I am trying to get a count of all the data that are present in the particular time frame. Currently this is my model class where I have the following entry to return all the entries in a particular date range:

public function get_records($st_date,$end_date){
        $this->db->select('*');
        $this->db->from('crm_listings');
        $this->db->where('cast(added_date as date) BETWEEN "' . $st_date . '" AND "' . $end_date . '" AND status= "D"');
        
        return $this->db->count_all_results();
     }

And my controller class:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
   class Testcontroller extends CI_Controller
   {  
      public function index() 
      { 
      $this->load->view('crm/test');      
       }

function fetch_status(){
            $output ='';
            $startDate = '';
            $endDate = '';
            $this->load->model('crm/user_model');
  
            if($this->input->post('startDate')){
              $startDate = $this->input->post('startDate');
            }
            if($this->input->post('endDate')){
               $endDate = $this->input->post('endDate');
             }
            $data = $this->user_model->get_records($startDate,$endDate);
            
            $output .= '
              <div class="table-responsive">
                 <table class="table table-bordered table-striped">
                    <tr>
                    <th>Draft</th>
                    </tr>
              ';
           if($data->num_rows() > 0)
           {
            $output .= '
               <tr>
                  <td>'.$data.'</td>
               </tr>
            ';
            }
           else
           {
              $output .= '<tr>
                 <td colspan="7">No Data Found</td>
                 </tr>';
           }
           $output .= '</table>';
           echo $output;
           }
}     
?>

So as of now when I comment everything in my controller class after $data and just make a statement echo $data; I get the output I want. But when I'm wrapping it around <td>'.$data.'</td>, it doesn't give an output.

CodePudding user response:

Edit your function at line echo $this->db->count_all_results();

function get_records($st_date,$end_date){
echo $this->db->count_all_results();// change this code
// to
return $this->db->get();
}

for make sure there is any data, try to print_r($data) after you call that method/function

$data = $this->user_model->get_records($startDate,$endDate);
print_r($data) // check data
  • Related