【发布时间】:2012-12-29 11:04:40
【问题描述】:
我正在尝试使用 these files 使用 CodeIgniter 创建 Datatable。
在我重命名为 datatable.php 的 data.php(controller) 中,我添加了“$this->getTable();”在函数 index() 中,仅根据我的需要定义了 $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 条记录,但它没有这样做。我不明白我哪里错了?
附加: getTable() 函数:
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;
【问题讨论】:
-
我测试了代码,它工作正常。你的数据库的结构是什么?您是否注意到根据函数,列是“id”、“first_name”和“last_name”?
-
我的专栏如下: $aColumns = array('student_id', 'exam_id', 'subject_id', 'marks_achieved');和表如下: $sTable = 'marks';
-
我用你指定的列修改了表格,我也修改了控制器。运行后,JSON输出很完美。请编辑您的问题以发布您的 SQL 代码和整个 getTable() 函数。
-
使用 phpMyAdmin,您可以点击“导出”来下载 SQL 文件。你的控制器看起来几乎和我的一模一样。尝试对变量进行故障排除。您可以在 getTable() 函数中使用 print_r() 来显示查询结果。您可以使用 $this->db->last_query(); 回显您的最后一个查询
-
我把你的 getTable() 函数换成了我的。您在循环中回显“123”,但除此之外,您的代码运行良好。你的数据库一定有问题。
标签: php codeigniter datatable