I'm new to PHP/CodeIgniter and am making a table that retrieves the count of values from Mysql within a particular timeframe. To achieve this I wrote the following for model class:
function get_listingstatus($fdate='',$tdate=''){
$this->db->select("count(*)");
$this->db->from("crm_logs as l");
if($fdate !='')
$this->db->where("date(l.logtime) >=",date('Y-m-d',strtotime($fdate)));
if($tdate !='')
$this->db->where("date(l.logtime) <=",date('Y-m-d',strtotime($tdate)));
$this->db->group_by("l.status_to");
$query = $this->db->get();
$results = $query->result_array();
return $results;
}
Controller Class:
public function totallistings($slug='')
{
$this->load->library('pagination');
$main['page_title']=$this->config->item('site_name').' - Listings Reports';
$main['header']=$this->adminheader();
$main['footer']=$this->adminfooter();
$main['left']=$this->adminleftmenu();
$content='';
$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'] ='';
}
$main['content']=$this->load->view('crm/reports/totallistings',$content,true);
$main['jsArray'] = array('public/assets/plugins/datatables/jquery.dataTables.min.js' );
$main['cssArray'] = array('public/assets/plugins/datatables/jquery.dataTables.min.css','public/assets/css/reports.css');
$this->load->view('crm/main',$main);
$content['groupedleads'] = $this->leads_model->get_listingstatus($fdate,$tdate);
}
And this in View class:
<form method='post' action="totallistings">
<input type="text" name="fdate" id="fdate" autocomplete="off" value="<?php echo $post['fdate']?date('d-m-Y',strtotime($post['fdate'])):''; ?>" class="form-control datepicker" placeholder="Lead From Date">
<input type="text" name="tdate" id="tdate" autocomplete="off" value="<?php echo $post['tdate']?date('d-m-Y',strtotime($post['tdate'])):''; ?>" class="form-control datepicker" placeholder="Lead To Date">
<button name="sdates" id="sdates" class="sbutton">Search</button>
</form>
<?php
$ls_arr = array('Draft','Unpublish','Publish','Action','Unlisted','Sold','Let');
foreach($groupedleads as $grplead){
$statuses[] = $status = $ls_arr[$grplead["status_to"]];
if($grplead["status_to"] == NULL || $grplead["status_to"] == '')
$grplead["status_to"] = "Unknown";
if(isset($grplead["status_to"]))
$titles[] = $title = $grplead["status_to"];
$leaddata[$status][$title] = $grplead["leadnum"];
}
if(count($titles) > 0)
$titles = array_unique($titles);
if(count($statuses) > 0)
$statuses = array_unique($statuses);
?>
<div class="tblcont">
<table id="statustbl" class="table-fill">
<thead>
<tr>
<?php
if(count($titles) > 0)
foreach($titles as $title){
?><th id=<?php echo $title; ?>><div><span><?php echo $title; ?></div></span></th>
<?php
}
?>
</tr>
</thead>
<tbody class="table-hover" class="table-hover">
<?php
if(is_array($statuses))
foreach($statuses as $status){
?>
<tr>
<?php
echo "<td>".$status."</td>";
foreach ($titles as $title) {
$num = $leaddata[$status][$title];
echo "<td>".$num."</td>";
}
?>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
});
</script>
But this gave me the a Invalid argument supplied for foreach() error on my webpage:
Basically I want to have it as something like this, where you just pass the start date and end date, and it returns a table with each status_to value count:
CodePudding user response:
You need to set the data into the array before you load the view or the value groupedleads
won't exist inside the view.
In your totallistings()
-method, move the last line:
$content['groupedleads'] = $this->leads_model->get_listingstatus($fdate,$tdate);
and put it before you load the view:
$content['groupedleads'] = $this->leads_model->get_listingstatus($fdate,$tdate);
$main['content'] = $this->load->view('crm/reports/totallistings',$content,true);
...