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条)

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line