dougou5844
2016-11-09 19:44
浏览 53
已采纳

在codeigniter中有计数记录的问题

I am having a syntax issue in the controller in the function index where the view is being loaded. it is saying that I have Severity: Parsing Error Message: syntax error, unexpected ';' I cannot figure out what the issue is I am trying to get the count of all my records in the database. Any help would be appreciated. controllers/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();
        }
    }

}

Model

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


}

view

  Total Records: <?php echo $total_records ?>
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dstd2129 2016-11-09 19:52
    已采纳

    In the class Test index function last statement ) is missing. Here is the correct one

    $this->load->view('test_view', array('total_records' => $total_records));
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题