I am trying to create Datatable with CodeIgniter using these files.
In data.php(controller) which I've renamed to datatable.php I added "$this->getTable();" in function index() and only defined $aColumns and $sTable according to my need. No other changes are made.
I run the code using this URL : "localhost/codeigniter/index.php/datatable" I am new to this so still not removed the index.php and I don't use base_url so I accordingly made changes in index.php while loading scripts and css. Also I've changed the sAjaxSource to datatable/getTable and class name to Datatable as I've changed the file name.
The main problem is the execution is not entering this foreach loop. The echo statement is not executed.
foreach($rResult->result_array() as $aRow)
{
echo '123';
$row = array();
foreach($aColumns as $col)
{
$row[] = $aRow[$col];
}
$output['aaData'][] = $row;
}
And I get the output as follows:
{"sEcho":0,"iTotalRecords":32,"iTotalDisplayRecords":"32","aaData":[]}
The aaData should display the 32 records in JSON format but it is not doing so. I am not getting where am I wrong ?
Appended: getTable() function:
public function getTable()
{
$aColumns = array('student_id', 'exam_id', 'subject_id', 'marks_achieved');
/* Indexed column (used for fast and accurate table cardinality) */
$sIndexColumn = "student_id";
// DB table to use
$sTable = 'marks';
//
$iDisplayStart = $this->input->get_post('iDisplayStart', true);
$iDisplayLength = $this->input->get_post('iDisplayLength', true);
$iSortCol_0 = $this->input->get_post('iSortCol_0', true);
$iSortingCols = $this->input->get_post('iSortingCols', true);
$sSearch = $this->input->get_post('sSearch', true);
$sEcho = $this->input->get_post('sEcho', true);
// Paging
if(isset($iDisplayStart) && $iDisplayLength != '-1')
{
$this->db->limit($this->db->escape_str($iDisplayLength), $this->db->escape_str($iDisplayStart));
}
// Ordering
if(isset($iSortCol_0))
{
for($i=0; $i<intval($iSortingCols); $i++)
{
$iSortCol = $this->input->get_post('iSortCol_'.$i, true);
$bSortable = $this->input->get_post('bSortable_'.intval($iSortCol), true);
$sSortDir = $this->input->get_post('sSortDir_'.$i, true);
if($bSortable == 'true')
{
$this->db->order_by($aColumns[intval($this->db->escape_str($iSortCol))], $this->db->escape_str($sSortDir));
}
}
}
if(isset($sSearch) && !empty($sSearch))
{
for($i=0; $i<count($aColumns); $i++)
{
$bSearchable = $this->input->get_post('bSearchable_'.$i, true);
// Individual column filtering
if(isset($bSearchable) && $bSearchable == 'true')
{
$this->db->or_like($aColumns[$i], $this->db->escape_like_str($sSearch));
}
}
}
// Select Data
$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
$rResult = $this->db->get($sTable);
// Data set length after filtering
$this->db->select('FOUND_ROWS() AS found_rows');
$iFilteredTotal = $this->db->get()->row()->found_rows;
// Total data set length
$iTotal = $this->db->count_all($sTable);
// Output
$output = array(
'sEcho' => intval($sEcho),
'iTotalRecords' => $iTotal,
'iTotalDisplayRecords' => $iFilteredTotal,
'aaData' => array()
);
foreach($rResult->result_array() as $aRow)
{
echo '123';
$row = array();
foreach($aColumns as $col)
{
$row[] = $aRow[$col];
}
$output['aaData'][] = $row;
}
echo json_encode($output);
}
Regarding SQL code, I've added the records manually in phpmyadmin not using queries.
Any issues in the SQL code ?
CREATE TABLE IF NOT EXISTS `marks` (
`student_id` int(10) NOT NULL,
`exam_id` varchar(10) NOT NULL,
`subject_id` int(10) NOT NULL,
`marks_achieved` int(10) NOT NULL,
KEY `student_id` (`student_id`),
KEY `exam_id` (`exam_id`),
KEY `subject_id` (`subject_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;