duanli8391 2015-01-17 06:43
浏览 11
已采纳

如何通过仅在codeigniter中发送参数来编写用于获取记录的函数?

I have 100 tables or can be more than that,I always need to fetch the record all the time in my application from various table.So writing functions for each and every query its not good coding standard.

 $this->db->select();
    $this->db->from("table1");
    $query = $this->db->get();
    return $query->result_array();


    $this->db->select();
    $this->db->from("table2");
    $query = $this->db->get();
    return $query->result_array();


    $this->db->select();
    $this->db->from("table1");
    $this->db->where("id",10);
    $query = $this->db->get();
    return $query->result_array();

I want the good coding standard for this.

  • 写回答

2条回答 默认 最新

  • dongzhenyin2001 2015-01-17 07:02
    关注

    Write this code in controller.

    $data = $this->common_model->getRecords("table_name", "*", array("field1" => $this->input->post('user_name')));
    

    This will be your function in model (that is common_model).

    public function getRecords($table, $fields = '', $condition = '', $order_by = '', $limit = '', $debug = 0) {
    
                $str_sql = '';
                if (is_array($fields)) {  #$fields passed as array
                    $str_sql.=implode(",", $fields);
                } elseif ($fields != "") {   #$fields passed as string
                    $str_sql .= $fields;
                } else {
                    $str_sql .= '*';  #$fields passed blank
                }
    
                $this->db->select($str_sql, FALSE);
    
                if (is_array($condition)) {  #$condition passed as array
                    if (count($condition) > 0) {
                        foreach ($condition as $field_name => $field_value) {
                            if ($field_name != '' && $field_value != '') {
                                $this->db->where($field_name, $field_value);
                            }
                        }
                    }
                } else if ($condition != "") { #$condition passed as string
                    $this->db->where($condition);
                }
    
                if ($limit != "")
                    $this->db->limit($limit);#limit is not blank
    
                if (is_array($order_by)) {
                    $this->db->order_by($order_by[0], $order_by[1]);  #$order_by is not blank
                } else if ($order_by != "") {
                    $this->db->order_by($order_by);  #$order_by is not blank
                }
    
    
                $this->db->from($table);  #getting record from table name passed
                $query = $this->db->get();
    
                if ($debug) {
                    die($this->db->last_query());
                }
                $error = $this->db->_error_message();
                $error_number = $this->db->_error_number();
                if ($error) {
                    $controller = $this->router->fetch_class();
                    $method = $this->router->fetch_method();
                    $error_details = array(
                        'error_name' => $error,
                        'error_number' => $error_number,
                        'model_name' => 'common_model',
                        'model_method_name' => 'getRecords',
                        'controller_name' => $controller,
                        'controller_method_name' => $method
                    );
                    $this->common_model->errorSendEmail($error_details);
                    redirect(base_url() . 'page-not-found');
                }
                return $query->result_array();
            }
    

    I used this code for fetching the record. you just need to pass the table name ,fields name ,the condition and limit.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?