Jump to content

Making Table head and its data dynamic in CodeIgniter


Go to solution Solved by Barand,

Recommended Posts

Currently I'm using CodeIgniter and Mysql to fetch my data. Here I'm using the following model to get a count of each status from my database:

function get_total(){
        $this->db->select("SUM(CASE WHEN status_to = 'Draft' THEN 1 END) AS draft,
        SUM(CASE WHEN status_to = 'Unpublish' THEN 1 END) AS unpublish,
        SUM(CASE WHEN status_to = 'Publish' THEN 1 END) AS publish,
        SUM(CASE WHEN status_to = 'Action' THEN 1 END) AS action,
        SUM(CASE WHEN status_to = 'Unlisted' THEN 1 END) AS unlisted,
        SUM(CASE WHEN status_to = 'Sold' THEN 1 END) AS sold,
        SUM(CASE WHEN status_to = 'Let' THEN 1 END) AS let");
        $this->db->from('crm_logs');
        $results = $this->db->get();
        return $results;
     }  

Then to get this model I've used the following controller class:

public function totallistings($slug='')
    {  
        $fdate = $this->input->post("fdate");
        $tdate = $this->input->post("tdate");

        if ($fdate) {
        $content['fdate'] = $fdate;
        } else {
        $content['fdate'] = '';
        }
        if ($tdate) {
        $content['tdate'] = $tdate;
        } else {
        $content['tdate'] = '';
        }
        $content['data_total'] = $this->leads_model->get_total()->result();
        $main['content']=$this->load->view('crm/reports/totallistings',$content,true);
        $this->load->view('crm/main',$main);    
    }

And this is my View class:

<?php
         if(isset($data_sum) && count($data_sum) > 0)
           {
            foreach($data_sum as $row ){
               $draft = $row->draft ? $row->draft : 0;
               $unpublish = $row->unpublish ? $row->unpublish : 0;
               $publish = $row->publish ? $row->publish : 0;
               $action = $row->action ? $row->action : 0;
               $unlisted = $row->unlisted ? $row->unlisted : 0;
               $sold = $row->sold ? $row->sold : 0;
               $let = $row->let ? $row->let : 0;                              
          ?>
              <tr>
                    <td><?= $draft ?></td>
                    <td><?= $unpublish ?></td>
                    <td><?= $publish ?></td>
                    <td><?= $action ?></td>
                    <td><?= $unlisted ?></td>
                    <td><?= $sold ?></td>
                    <td><?= $let ?></td>
              </tr>
          <?php }  }  else { ?>
           
          <?php }  ?>

I now found out that there are more status words other than draft, publish, etc in my database. So I wanted a way to make this `<th>` and `<td>` dynamic but still having the same functionality it is currently having.

  • Solution

I'd do it this way

$res = $db->query("SELECT status_to
                        , COUNT(*) as tot
                   FROM crm_log
                   GROUP BY status_to     
                  ");
$totals = array_column($res->fetchAll(), 'tot', 'status_to');

$heads = "<tr><th>" . join('</th><th>', array_keys($totals)) . "</th></tr>\n";
$vals = "<tr><td>" . join('</td><td>', $totals) . "</td></tr>\n";
?> 
<table border='1'>
    <?=$heads?>
    <?=$vals?>
</table>                           
      

image.png.ce5fd60e9c2014c4b7f5257df111cf40.png

  • Like 1
14 hours ago, Barand said:

I'd do it this way

$res = $db->query("SELECT status_to
                        , COUNT(*) as tot
                   FROM crm_log
                   GROUP BY status_to     
                  ");
$totals = array_column($res->fetchAll(), 'tot', 'status_to');

$heads = "<tr><th>" . join('</th><th>', array_keys($totals)) . "</th></tr>\n";
$vals = "<tr><td>" . join('</td><td>', $totals) . "</td></tr>\n";
?> 
<table border='1'>
    <?=$heads?>
    <?=$vals?>
</table>                           
      

image.png.ce5fd60e9c2014c4b7f5257df111cf40.png

Thanks! Simple and really easy to understand

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.