Jump to content

Showing all data initially before a filter in CodeIgniter


JJM50

Recommended Posts

I have a table in PHP that displays the status count report in a particular date range. I have created the code so that it fetches the 2 inputs and displays my output according to those 2 values. So initially the value it shows is 0 before the date filter, but I want it so that the initial value that it shows is the total count from day 0 till today. Currently this is my model class:

function get_listingstatus($fdate,$tdate){
        $this->db->select("SUM(CASE WHEN status = 'Active' THEN 1 END) AS active");
        $this->db->from('logs');
        $this->db->where('cast(logtime as date) BETWEEN "' . $fdate . '" AND "' . $tdate . '"');
        $results = $this->db->get();
        return $results;
     } 

Controller Class:

public function totallistings($slug='')
    {
        $fdate = $this->input->post("fdate");
        $content['tdate'] = $tdate = $this->input->post("tdate");
        if(isset($fdate)){
            $content['fdate'] =$fdate;
        }else{
            $content['fdate'] = '';
        }
        if(isset($tdate)){
            $content['tdate'] =$tdate;
        }else{
            $content['tdate'] ='';
        }
        $content['data_sum'] = $this->leads_model->get_listingstatus($fdate,$tdate)->result();
        $main['content']=$this->load->view('crm/reports/totallistings',$content,true);      
        $this->load->view('crm/main',$main);    
    }

And my View class is like so:

<?php
         if(isset($data_sum) && count($data_sum) > 0)
           {
            foreach($data_sum as $row ){
               $active = $row->active ? $row->active : 0;                           
          ?>
              <tr>
                    <td><?= $active ?></td>
              </tr>
          <?php }  }  else { ?>
          <tr>
             <td>No Data Found</td>
          </tr>
       <?php } ?>

Here my initial value is 0 and when I make the search filter it returns the output within a date range, But I want to change this initial value to have the full count shown.

Edited by JJM50
Link to comment
Share on other sites

28 minutes ago, requinix said:
        if(isset($fdate)){
            $content['fdate'] =$fdate;
        }else{
            $content['fdate'] = '';
        }
        if(isset($tdate)){
            $content['tdate'] =$tdate;
        }else{
            $content['tdate'] ='';
        }

That looks relevant. Have you tried changing those two empty strings to be something else?

So if I want fdate to get my day 0 and tdate to get todays date, how can I edit it to show those variables?

Edited by JJM50
Link to comment
Share on other sites

30 minutes ago, requinix said:

I don't know what "day 0" is, but you can get a string representing today's date with, surprisingly enough, the date() function.

Okay so I tried this and it's not the solution, but I've found out that I've to update my else statement with an additional model. So what I've done now is added an extra model removing the where statement:

function get_listingstatus_total(){
        $this->db->select("SUM(CASE WHEN status = 'Active' THEN 1 END) AS active");
        $this->db->from('logs');
        $results = $this->db->get();
        return $results;
     } 

and added it in my controller class:

$content['data_total'] = $this->leads_model->get_listingstatus_total()->result();

And put the following in my view class:

<?php
         if(isset($data_sum) && count($data_sum) > 0)
           {
            foreach($data_sum as $row ){
               $active = $row->active ? $row->active : 0;                           
          ?>
              <tr>
                    <td><?= $active ?></td>
              </tr>
          <?php }  }  else { ?>
          <?php
            foreach($data_total as $row ){
               $active = $row->active ? $row->active : 0;                           
          ?>
              <tr>
                    <td><?= $active ?></td>
              </tr>
          <?php } ?>

But unfortunately it still returns 0, but I'm sure this is the solution I've to use. When I put the $data_total instead of $data_sum in if statement it gives correct output, but like this it gives wrong

Edited by JJM50
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.