使用codeigniter创建数据表时出现问题


Issue in creating datatable with codeigniter

我正在尝试使用这些文件与CodeIgniter创建Datatable。

在data.php(控制器),我已经改名为datatable.php我添加了"$this->getTable();"在函数索引(),只定义$aColumns和$sTable根据我的需要。没有其他更改。

我使用这个URL运行代码:"localhost/codeigniter/index.php/datatable"我是新来的,所以仍然没有删除index.php,我不使用base_url,所以我相应地在加载脚本和css时对index.php进行了更改。此外,我已经将sAjaxSource更改为datatable/getTable,类名更改为datatable,因为我已经更改了文件名。

主要问题是执行没有进入foreach循环。不执行echo语句

foreach($rResult->result_array() as $aRow)
    {
        echo '123';
        $row = array();
        foreach($aColumns as $col)
        {
            $row[] = $aRow[$col];
        }
        $output['aaData'][] = $row;
    }

得到如下输出:

{"sEcho":0,"iTotalRecords":32,"iTotalDisplayRecords":"32","aaData":[]}

aaData应该以JSON格式显示32条记录,但它没有这样做。我没有得到我错在哪里?

附加:可以获得的()函数:

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);
}

关于SQL代码,我在phpmyadmin中手动添加了不使用查询的记录。

SQL代码有问题吗?

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;

创建如下表:

CREATE TABLE IF NOT EXISTS `marks` (
  `student_id` int(10) unsigned NOT NULL,
  `exam_id` int(10) unsigned NOT NULL,
  `subject_id` int(10) unsigned NOT NULL,
  `marks_achieved` varchar(50) NOT NULL DEFAULT '',
  PRIMARY KEY (`student_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
INSERT INTO `marks` (`student_id`, `exam_id`, `subject_id`, `marks_achieved`) VALUES
(1, 210, 340, 'really good'),
(2, 220, 440, 'super');

编辑getTables()函数来指定列:

$aColumns = array('student_id', 'exam_id', 'subject_id', 'marks_achieved');

将名称从表中删除,编辑这一行:

$sTable = 'marks';

你的桌子可能不完全像这样,但我没有问题,使它工作。以下是返回的JSON输出:

{"sEcho":0,"iTotalRecords":2,"iTotalDisplayRecords":"2","aaData":[["1","210","340","really good"],["2","220","440","super"]]}

修改如下:

if(isset($iDisplayStart) && $iDisplayLength != '-1')

:

if(( ! empty($iDisplayStart)) && $iDisplayLength != '-1')

应该在第35行或第35行左右