在codeigniter中有计数记录的问题

时间:2016-11-09 19:44:02

标签: php codeigniter

我在加载视图的函数索引中的控制器中遇到语法问题。它说我有严重性:解析错误 消息:语法错误,意外';'我无法弄清楚问题是什么我试图获取数据库中所有记录的计数。任何帮助,将不胜感激。 控制器/ test.php的

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Test extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('test_model','test_table');
    }

    public function index()
    {


        $this->load->view('includes/header');
        $this->load->view('includes/table_main');
        $this->load->view('includes/ajax_functions');
        $this->load->view('includes/add_employee_form');

        $total_records = $this->test_model->count_all_records();

        //$this->load->view('test_view');

        $this->load->view('test_view', array('total_records' => $total_records);

    }


    public function ajax_list()
    {
        $list = $this->test_table->get_datatables();
        $data = array();
        $no = $_POST['start'];
        foreach ($list as $test_table) {
            $no++;
            $row = array();
            $row[] = $test_table->Name;
            $row[] = $test_table->Department;
            $row[] = $test_table->Manager;


            //add html for action
            $row[] = '<a class="btn btn-sm btn-link " href="javascript:void()" title="Edit" onclick="edit_test('."'".$test_table->id."'".')"><i class="glyphicon glyphicon-pencil"></i> Edit</a>
                  <a class="btn btn-sm text-warning" href="javascript:void()" title="Hapus" onclick="delete_test('."'".$test_table->id."'".')"><i class="glyphicon glyphicon-trash"></i> Delete</a>';

            $data[] = $row;
        }

        $output = array(
                        "draw" => $_POST['draw'],
                        "recordsTotal" => $this->test_table->count_all(),
                        "recordsFiltered" => $this->test_table->count_filtered(),
                        "data" => $data,
                );
        //output to json format
        echo json_encode($output);
    }




    public function ajax_edit($id)
    {
        $data = $this->test_table->get_by_id($id);
        echo json_encode($data);
    }

    public function ajax_add()
    {
        $this->_validate();
        $data = array(
                'Name' => $this->input->post('Name'),
                'Department' => $this->input->post('Department'),
                'Manager' => $this->input->post('Manager'),

            );
        $insert = $this->test_table->save($data);
        echo json_encode(array("status" => TRUE));
    }

    public function ajax_update()
    {
        $this->_validate();
        $data = array(
                'Name' => $this->input->post('Name'),
                'Department' => $this->input->post('Department'),
                'Manager' => $this->input->post('Manager'),

            );
        $this->test_table->update(array('id' => $this->input->post('id')), $data);
        echo json_encode(array("status" => TRUE));
    }

    public function ajax_delete($id)
    {
        $this->test_table->delete_by_id($id);
        echo json_encode(array("status" => TRUE));
    }

//validation section were user must enter data in all fields 
    private function _validate()
    {
        $data = array();
        $data['error_string'] = array();
        $data['inputerror'] = array();
        $data['status'] = TRUE;

        if($this->input->post('Name') == '')
        {
            $data['inputerror'][] = 'Name';
            $data['error_string'][] = 'Name is  required';
            $data['status'] = FALSE;
        }

        if($this->input->post('Department') == '')
        {
            $data['inputerror'][] = 'Department';
            $data['error_string'][] = 'Department is required';
            $data['status'] = FALSE;
        }


        if($data['status'] === FALSE)
        {
            echo json_encode($data);
            exit();
        }
    }

}

模型

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class test_model extends CI_Model {

    var $table = 'test_table';
    var $column = array('Name','Department','Manager'); //set column field database for order and search
    var $order = array('id' => 'desc'); // default order 

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    private function _get_datatables_query()
    {

        $this->db->from($this->table);

        $i = 0;

        foreach ($this->column as $item) // loop column 
        {
            if($_POST['search']['value']) // if datatable send POST for search
            {

                if($i===0) // first loop
                {
                    $this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND. 
                    $this->db->like($item, $_POST['search']['value']);
                }
                else
                {
                    $this->db->or_like($item, $_POST['search']['value']);
                }

                if(count($this->column) - 1 == $i) //last loop
                    $this->db->group_end(); //close bracket
            }
            $column[$i] = $item; // set column array variable to order processing
            $i++;
        }

        if(isset($_POST['order'])) // here order processing
        {
            $this->db->order_by($column[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
        } 
        else if(isset($this->order))
        {
            $order = $this->order;
            $this->db->order_by(key($order), $order[key($order)]);
        }
    }
     function count_all_records(){
        $this->db->select('id');
        $this->db->distinct();
        $this->db->from('test_table');
        $query = $this->db->get();
        return $query->num_rows();
    }

    function get_datatables()
    {
        $this->_get_datatables_query();
        if($_POST['length'] != -1)
        $this->db->limit($_POST['length'], $_POST['start']);
        $query = $this->db->get();
        return $query->result();
    }

    function count_filtered()
    {
        $this->_get_datatables_query();
        $query = $this->db->get();
        return $query->num_rows();
    }

    public function count_all()
    {
        $this->db->from($this->table);
        return $this->db->count_all_results();
    }

    public function get_by_id($id)
    {
        $this->db->from($this->table);
        $this->db->where('id',$id);
        $query = $this->db->get();

        return $query->row();

    }


    public function save($data)
    {
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    public function update($where, $data)
    {
        $this->db->update($this->table, $data, $where);
        return $this->db->affected_rows();
    }

    public function delete_by_id($id)
    {
        $this->db->where('id', $id);
        $this->db->delete($this->table);
    }


}

查看

  Total Records: <?php echo $total_records ?>

2 个答案:

答案 0 :(得分:0)

class Test index function最后一句话)遗失。这是正确的

$this->load->view('test_view', array('total_records' => $total_records));

答案 1 :(得分:0)

可能是由于脚本包含在视图中,在htaccess文件中设置short_open_tag 0并检查是否解决

  • &lt; IfModule mod_php5.c&gt;

    php_value short_open_tag 0

    &lt; / IfModule&gt;