【问题标题】:Issue in creating datatable with codeigniter使用 codeigniter 创建数据表的问题
【发布时间】: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


【解决方案1】:

创建如下表:

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 行或附近。

【讨论】:

  • 非常感谢!问题解决了。你的代码是如何工作的 isset() 函数而我的不工作?
  • 您必须运行最新版本的 Codeigniter。根据此链接,他们更改了限制功能的工作方式:stackoverflow.com/questions/12365432/…
  • 是的,限制仍然会导致问题。当我进行编辑时,您说有问题。在第一次绘制时,所有记录都会显示,而不是我定义的 10 条。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多