View difference between Paste ID: u8VQLBRB and 122qTHuz
SHOW: | | - or go back to the newest paste.
1-
        /**
1+
/**
2
        * Query builder for getting most datatables
3
        *
4
        * @param string $table
5
        * @param string|array $fields
6
        * @param array $where
7
        * @param string $distinct_field
8
        * @param string $order
9
        * @param string $limit
10
        * @internal param bool $distinct
11
        * @internal param null|string $field
12
        * @return array $roles_array with rows from database
13-
        function getDatatable($table, $where = array(), $distinct_field = null, $order = null, $limit = null){
13+
14
        function getDataTables($table, $fields = '*', $where = array(), $distinct_field = null, $order = null, $limit = null){
15
            $where_sql = '';
16
            if(count($where) > 0)
17
            {
18
                foreach($where as $column => $value)
19
                {
20
                    $where_sql .= "`". datacheck($column) ."` = '". datacheck($value) ."' AND ";
21
                }
22
            }
23
            $where_sql .= " 1=1";
24
25
            $db = new db('default');
26
            if(!is_null($distinct_field))
27
                $select = "SELECT DISTINCT `".datacheck($distinct_field)."`";
28-
                $select = "SELECT * ";
28+
29-
            $sql = $select ."FROM `project_roles` WHERE ".$where_sql;
29+
30
                if(count($fields) > 0 && is_array($fields))
31
                {
32
                    $select = "SELECT ";
33
                    $total = count($fields);
34-
            $datatable = $this->query($sql); //This function is actually in a bigger DB class. this just executes the query and returns results
34+
                    $i = 1;
35
                    foreach($fields as $field){
36-
            while($datatable = $row->fetchrow())
36+
                        $select .= '`'.$field.'`';
37
                        if($total != $i)
38
                        {
39
                            $select .= ', ';
40
                            $i++;
41
                        }else{
42
                            $select .= " ";
43
                            $i = 0;
44
                        }
45
                    }
46
                }elseif($fields != '*'){
47
                    $select = "SELECT `".$fields."` ";
48
                }
49
                else{
50
                    $select = "SELECT * ";
51
                }
52
            }
53
            $sql = $select ."FROM `".$table."` WHERE ".$where_sql;
54
            if(!is_null($order))
55
                $sql .= "  ORDER BY `". datacheck($order) ."` ";
56
            if(!is_null($limit))
57
                $sql .= " LIMIT ".datacheck($limit);
58
            $datatable = $this->query($sql);
59
            $rows_array = array();
60
            while($row = $datatable->fetchrow())
61
            {
62
                array_push($rows_array, $row);
63
            }
64
            return $rows_array;
65
        }