Here is my controller code:
public function search_records() {
if($this->session->userdata('logged_in'))
{
$data = array (
'employeeName' => $this->input->post('employeeName'),
'userID' => $this->input->post('userID'),
'date' => $this->input->post('date'),
'timeSelect' => $this->input->post('timeSelect'),
'error_message' => 'Sorry there is no record !'
);
if($data['employeeName'] !== '' OR $data['userID'] !== '')
{
if(isset($data['date']))
{
if($data['timeSelect'] == 1)
{
$data['time1'] = '00:00:00';
$data['time2'] = '23:59:00';
$result_from_db= $this->mrecord->search_by_id_or_name_with_date($data);
if($result_from_db == false)
{
$data['show_search_error_message'] = 'Sorry, there is no record !';
$this->load->view('vhome', $data);
}else {
$data['result_display'] = $result_from_db->result();
$data['row_count'] = count($result_from_db->result());
$this->session->set_userdata("excel_data", $result_from_db->result());
$this->pagi($data);
}
}else {
$data['time1'] = '12:30:00';
$data['time2'] = '15:30:00';
$result_from_db = $this->mrecord->search_by_id_or_name_with_date($data);
if($result_from_db == false)
{
$data['show_search_error_message'] = 'Sorry, there is no record !';
$this->load->view('vhome', $data);
}else {
$data['result_display'] = $result_from_db->result();
$data['row_count'] = count($result_from_db->result());
$this->session->set_userdata("excel_data", $result_from_db->result());
$this->pagi($data);
}
}
}else {
$data['error_message'] = 'Something is wrong ! Please check your date .';
$this->load->view('vhome', $data);
}
}else if(isset($data['date']))
{
if($data['timeSelect'] == 1)
{
$data['time1'] = '07:00:00';
$data['time2'] = '19:00:00';
$result_from_db = $this->mrecord->search_by_date($data);
if($result_from_db == false)
{
$data['error_message'] = 'Sorry, there is no record !';
$this->load->view('vhome', $data);
}else {
$data['result_display'] = $result_from_db->result();
$data['row_count'] = count($result_from_db->result());
$this->session->set_userdata("excel_data", $result_from_db->result());
$this->pagi($data);
}
}else {
$data['time1'] = '12:30:00';
$data['time2'] = '15:30:00';
$result_from_db = $this->mrecord->search_by_date($data);
if($result_from_db == false)
{
$data['error_message'] = 'Sorry, there is no record !';
$this->load->view('vhome', $data);
}else {
$data['result_display'] = $result_from_db->result();
$data['row_count'] = count($result_from_db->result());
$this->session->set_userdata("excel_data", $result_from_db->result());
$this->pagi($data);
}
}
} else {
$data['error_message'] = 'Oops ... you can not search without input !';
$this->load->view('vhome', $data);
}
}else {
redirect ('welcome', 'refresh');
}
Here is my model:
public function search_by_id_or_name_with_date($data) {
if($data['userID'] == '')
{
$condition = "employeeName =" . "'" . $data['employeeName'] . "'";
}else {
$condition = "employeeID =" . "'" . $data['userID'] . "'";
}
$condition2 = "date =" . "'" . $data['date'] . "'";
$condition1 = "time BETWEEN " . "'" . $data['time1'] . "'" . "AND" . "'" . $data['time2'] . "'";
$this->db->select('employeeID, employeeName, departmentName, date, time, tb_inout.doorStatus');
$this->db->from('tb_access_logs');
$this->db->join('tb_inout', 'tb_access_logs.doorID = tb_inout.doorID');
$this->db->where($condition);
$this->db->where($condition1);
$this->db->where($condition2);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query;
} else {
return false;
}
}
public function search_by_date($data) {
$condition = "date =" . "'" . $data['date'] . "'";
$condition1 = "time BETWEEN " . "'" . $data['time1'] . "'" . "AND" . "'" . $data['time2'] . "'";
$this->db->select('employeeID, employeeName, departmentName, date, time, tb_inout.doorStatus');
$this->db->from('tb_access_logs');
$this->db->join('tb_inout', 'tb_access_logs.doorID = tb_inout.doorID');
$this->db->where($condition);
$this->db->where($condition1);
$query = $this->db->get();
if($query->num_rows() > 0) {
return $query;
}else {
return false;
}
}
Here is my view:
<div class="jumbotron">
<?php
$this->load->helper('array');
if (isset($result_display)) {
echo "<div class=container>";
echo "<h2>Result</h2>";
echo "<p>All result was generated from BioStar Database</p>";
echo "<table class='table table-hover' data-toggle='table'
data-show-export='true'
data-pagination='true'
data-click-to-select='true'
data-toolbar='#toolbar'
data-url='../json/data1.json'>";
echo "<thead>";
echo "<tr><th>Employee ID</th><th>Employee Name</th><th>Department Name</th><th>Date</th><th>Time</th><th>State</th></tr>";
echo "</thead>";
echo "<tbody>";
foreach ($result_display as $value) {
echo '<tr>' . '<td>' . $value->employeeID . '</td>' . '<td>' . $value->employeeName . '</td>' . '<td>' . $value->departmentName . '</td>' . '<td>' . $value->date . '</td>' . '<td>' . $value->time . '</td>' . '<td>' . $value->doorStatus . '</td>' . '</tr>';
}
echo "</tbody>";
echo "</table>";
}else {
echo '<div class="row">';
echo '<center>' . $error_message . '</center>';
echo '</div>';
$this->output->set_header('refresh:3; url='. base_url("crecord"));
}
?>
Here is my Excel export controller:
class Excel extends CI_Controller {
public function index()
{
$this->load->library('session');
if($this->session->userdata('logged_in'))
{
redirect('crecord', 'refresh');
}else {
redirect('welcome', 'refresh');
}
}
public function report()
{
$this->load->helper('download');
$this->load->model('mrecord');
$result_data = $this->session->userdata("excel_data");
$headers = ''; // just creating the var for field headers to append to below
$data = ''; // just creating the var for field data to append to below
$fields = array('Employee ID', 'Employee Name', 'Department Name', 'Date', 'Time', 'State');
if ($result_data == 0) {
echo '<p>The table appears to have no data.</p>';
} else {
foreach ($fields as $field) {
$headers .= $field . "\t";
}
foreach ($result_data as $row) {
$line = '';
foreach($row as $value) {
if (( ! isset($value)) OR ($value == "")) {
$value = "\t";
} else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}
$data = str_replace("\r","",$data);
force_download("Report.xls", $headers . "\n" . $data);
}
} }
When I try to export by choosing first option to Excel, it is ok. But when I tried to export second option (date only) it does not work.