Advertisement
koen_hendriks

Universal function for datatables

Sep 18th, 2013
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.63 KB | None | 0 0
  1.         /**
  2.         * Query builder for getting most datatables
  3.         *
  4.         * @param string $table
  5.         * @param array $where
  6.         * @param string $distinct_field
  7.         * @param string $order
  8.         * @param string $limit
  9.         * @internal param bool $distinct
  10.         * @internal param null|string $field
  11.         * @return array $roles_array with rows from database
  12.         */
  13.         function getDatatable($table, $where = array(), $distinct_field = null, $order = null, $limit = null){
  14.             $where_sql = '';
  15.             if(count($where) > 0)
  16.             {
  17.                 foreach($where as $column => $value)
  18.                 {
  19.                     $where_sql .= "`". datacheck($column) ."` = '". datacheck($value) ."' AND ";
  20.                 }
  21.             }
  22.             $where_sql .= " 1=1";
  23.  
  24.             $db = new db('default');
  25.             if(!is_null($distinct_field))
  26.                 $select = "SELECT DISTINCT `".datacheck($distinct_field)."`";
  27.             else
  28.                 $select = "SELECT * ";
  29.             $sql = $select ."FROM `project_roles` WHERE ".$where_sql;
  30.             if(!is_null($order))
  31.                 $sql .= "  ORDER BY `". datacheck($order) ."` ";
  32.             if(!is_null($limit))
  33.                 $sql .= " LIMIT ".datacheck($limit);
  34.             $datatable = $this->query($sql); //This function is actually in a bigger DB class. this just executes the query and returns results
  35.             $rows_array = array();
  36.             while($datatable = $row->fetchrow())
  37.             {
  38.                 array_push($rows_array, $row);
  39.             }
  40.             return $rows_array;
  41.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement