Advertisement
Guest User

query

a guest
Jul 23rd, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.44 KB | None | 0 0
  1. $parcelsTable = 'parcels_short';
  2.         $personTable = 'personal_data';
  3.         $contactTable = 'contact_data';
  4.         $addressTable = 'address';
  5.         $transporterTable = 'transporters';
  6.  
  7.         $personJoinCond = ' ( '.$parcelsTable.'.person_info = '.$personTable.'.person_id ) ';
  8.         $contactJoinCond = ' ( '.$parcelsTable.'.contact = '.$contactTable.'.contact_id ) ';
  9.         $addressJoinCond = ' ( '.$parcelsTable.'.address = '.$addressTable.'.address_id ) ';
  10.         $transporterJoinCond = ' ( ' . $parcelsTable . '.transporter_code =  ' . $transporterTable . '.code )';
  11.  
  12.         $personJoin = ' JOIN '.$personTable.' ON '.$personJoinCond;
  13.         $contactJoin = ' JOIN '.$contactTable.' ON '.$contactJoinCond;
  14.         $addressJoin = ' JOIN '.$addressTable.' ON '.$addressJoinCond;
  15.         $transporterJoin = ' JOIN ' . $transporterTable . ' ON ' . $transporterJoinCond;
  16.  
  17.         $query = 'SELECT * FROM '.$parcelsTable.$personJoin.$contactJoin.$addressJoin.$transporterJoin;
  18.  
  19.         if ( $condition !== NULL ) {
  20.  
  21.             $date = new \DateTime();
  22.  
  23.             if( isset($condition['greater_date']) ) {
  24.                 $date->setDate(substr($condition['greater_date'], 8, 4), substr($condition['greater_date'], 4, 2), substr($condition['greater_date'], 0, 2));
  25.                 $condition['greater_date'] = $date->format('Y-m-d');
  26.             }
  27.  
  28.             if( isset($condition['lower_date']) ) {
  29.                 $date->setDate(substr($condition['lower_date'], 8, 4), substr($condition['lower_date'], 4, 2), substr($condition['lower_date'], 0, 2));
  30.                 $condition['lower_date'] = $date->format('Y-m-d');
  31.             }
  32.  
  33.             $conditionText = '';
  34.             foreach ($condition as $key => $value) {
  35.  
  36.                 if (substr($key, 0, 8) == 'greater_' ) {
  37.                     $conditionText .= ' '.substr($key, 8, strlen($key) - 8).' >= "'.$value.'" AND ';
  38.                 }
  39.                 elseif (substr($key, 0, 6) == 'lower_' ) {
  40.                     $conditionText .= ' '.substr($key, 6, strlen($key) - 6).' <= "'.$value.'" AND ';
  41.                 }
  42.                 else {
  43.                     $conditionText .= ' '.$key.' = "'.$value.'" AND ';
  44.                 }
  45.             }
  46.             $conditionText = substr($conditionText, 0, strlen($conditionText) - 5);
  47.             $query .= 'WHERE '.$conditionText.' ';
  48.         }
  49.         $query .= 'ORDER BY '.$column;
  50.         return $this->database->query($query);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement