使用codeigniter创建datatable的问题 [英] Issue in creating datatable with codeigniter

查看:189
本文介绍了使用codeigniter创建datatable的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用这些文件创建Datatable with CodeIgniter。

I am trying to create Datatable with CodeIgniter using these files.

在我重命名为datatable.php的data.php(controller)中,我添加了$ this-> getTable();在函数index()中,只根据我的需要定义$ aColumns和$ sTable。不进行其他更改。

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.

我使用以下URL运行代码:localhost / codeigniter / index.php / datatable
我是新的,所以仍然没有删除索引。 php和我不使用base_url所以我相应地做了更改index.php同时加载脚本和CSS。我已经改变了sAjaxSource为datatable / getTable和类名为Datatable,因为我改变了文件名。

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.

主要的问题是执行不输入foreach循环。

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

我得到的输出如下:

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

aaData应该以JSON格式显示32条记录,但不是这样。

The aaData should display the 32 records in JSON format but it is not doing so. I am not getting where am I wrong ?

附加:
getTable()函数:

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

关于SQL代码,我已经在phpmyadmin中手动添加记录, 。

Regarding SQL code, I've added the records manually in phpmyadmin not using queries.

SQL代码中的任何问题?

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;


推荐答案

创建如下表格:

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()函数以指定列:

Edit the getTables() function to specify the columns:

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

更改表格名称,编辑此行:

Change the name off the table, editing this line:

$sTable = 'marks';

你的表可能不是这样,但是我没有问题。这里是回传的JSON输出:

Your table probably isn't exactly like this, but I had no problem getting this to work. Here's the JSON output that was echoed:

{"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行或其后。

It should be on or around line 35.

这篇关于使用codeigniter创建datatable的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆