2016-10-13 21 views
0

こんにちは私はPHPアプリケーションを作成していますが、現在、私はデータベースからのフライトを取得してそれに異なるフィルタを適用する方法に固執しています。最初にフィルタを適用せずにページをロードするときに問題はありません.DBからのすべてのレコードが期待どおりに読み込まれます。その後、「出発空港」または「到着空港」フィルタと「予約可能な」フィルタを使用すると、予想どおりにすべてが再び有効になります。Codeigniterカスタム検索機能の問題

"Bookable Only"フィルタを単独で使用しようとすると、データベースからのレコードがロードされません。それは、「航空機」フィルタと同じだ、独自に取り組み、「予約可能なだけ」フィルタとしませんが、Schedules_model.phpの両方またはいずれかの空港フィルタの1 +「予約可能な唯一の」フィルタ

と組み合わせたときに動作します

public function getFilteredSchedule($available, $departureICAO, $arrivalICAO, $specificAircraftId) 
{ 
    $this->db->select('*'); 

    if($departureICAO != FALSE) { 
     $this->db->where('departureICAO', $departureICAO); 
    } 
    if($arrivalICAO != FALSE) { 
     $this->db->where('arrivalICAO', $arrivalICAO); 
    } 
    if($specificAircraftId != FALSE) { 
     $this->db->where('aircraftId', $specificAircraftId); 
    } 

    $schedules = $this->db->where('active', 1) 
        ->order_by('id', 'asc') 
        ->get('schedules') 
        ->result_array(); 

    $schedulesAvailable = array(); 

    if($available === TRUE) { 

     echo 'work'; 

     foreach($schedules as $key => $schedule) { 

      if($this->RebuildVA->mustBeAtDepartureAirport()) { 
       if($this->Aircrafts->isAtAirport($schedule['aircraftId'], $schedule['departureICAO'])) { 
        $schedulesAvailable[$key] = $schedule; 
       } else { 
        break; 
       } 
      } else { 
       $schedulesAvailable[$key] = $schedule; 
      } 

      if(!$this->RebuildVA->allowMultipleAircraftBookings()) { 
       if(!$this->Aircrafts->isBooked($schedule['aircraftId'])) { 
        $schedulesAvailable[$key] = $schedule; 
       } else { 
        break; 
       } 
      } else { 
       $schedulesAvailable[$key] = $schedule; 
      } 

      if(!$this->RebuildVA->allowMultiplePilotBookings()) { 
       if(!$this->Pilots->hasBookedFlight($this->session->userdata('pilotId'))) { 
        $schedulesAvailable[$key] = $schedule; 
       } else { 
        break; 
       } 
      } else { 
       $schedulesAvailable[$key] = $schedule; 
      } 
     } 
    } else { 
     $schedulesAvailable = $schedules; 
    }  

    return $schedulesAvailable; 
} 

schedules.php

public function search() 
{ 

    $this->data['pageTitle'] = 'Schedule Search'; 
    $this->data['pageDisplayedTitle'] = 'Schedule Search'; 

    $available = (bool) $this->input->post('available'); 

    $this->data['schedules'] = $this->Schedules->getFilteredSchedule($available, $this->input->post('departureICAO'), $this->input->post('arrivalICAO'), $this->input->post('aircraftId')); 

    $airportsList = $this->Airports->getAllAirports(TRUE, TRUE); // Get set of all active airports 
    $aircraftsList = $this->Aircrafts->getAllAircrafts(TRUE, TRUE); // Get set of all active airports 

    // Prepare form inputs 
    $this->data['departureICAO'] = array(
     'name' => 'departureICAO', 
     'id' => 'departureICAO', 
     'selected' => $this->input->post('departureICAO'), 
     'options' => $airportsList, 
    );  
    $this->data['arrivalICAO'] = array(
     'name' => 'arrivalICAO', 
     'id' => 'arrivalICAO', 
     'selected' => $this->input->post('arrivalICAO'), 
     'options' => $airportsList, 
    ); 
    $this->data['aircraftId'] = array(
     'name' => 'aircraftId', 
     'id' => 'aircraftId', 
     'selected' => $this->input->post('aircraftId'), 
     'options' => $aircraftsList, 
    ); 
    $this->data['available'] = array(
     'name' => 'available', 
     'id' => 'available', 
     'checked' => set_checkbox('available', $this->input->post('available'), FALSE), 
     'value' => TRUE, 
    ); 

    $this->load->view('schedules/scheduleSearch', $this->data); 
} 

私はすべてをデバッグし、ステップバイステップのプロセスだけでなく、試行錯誤の方法が、どれも弾力を以下試してみました期待される効果。何か案は?

答えて

0

試行錯誤の方法で私はいくつかの仕事をどのようにして回避するかを見つけました。誰かがそれに関する提案や改善方法を知っていれば、私はアプリでのパフォーマンスを探しているので、気軽にお気軽に。

public function getFilteredSchedule($available, $departureICAO, $arrivalICAO, $specificAircraftId) 
{ 
    $this->db->select('*'); 

    if($departureICAO != FALSE) { 
     $this->db->where('departureICAO', $departureICAO); 
    } 
    if($arrivalICAO != FALSE) { 
     $this->db->where('arrivalICAO', $arrivalICAO); 
    } 
    if($specificAircraftId != FALSE) { 
     $this->db->where('aircraftId', $specificAircraftId); 
    } 

    $schedules = $this->db->where('active', 1) 
        ->order_by('id', 'asc') 
        ->get('schedules') 
        ->result_array(); 

    $schedulesAvailable = array(); 

    // Check if any of the filters is required 
    if(!$this->RebuildVA->mustBeAtDepartureAirport() && $this->RebuildVA->allowMultipleAircraftBookings() && $this->RebuildVA->allowMultiplePilotBookings()) { 
     $schedulesAvailable = $schedules; 
    // Check if only bookable flights has been checked 
    } elseif($available === TRUE) { 
     foreach($schedules as $key => $schedule) { 

      // Allow multiple schedule bookings 

      // Check if the aircraft must be at departure airport 
      if($this->RebuildVA->mustBeAtDepartureAirport()) { 
       if($this->Aircrafts->isAtAirport($schedule['aircraftId'], $schedule['departureICAO'])) { 
        $schedulesAvailable[$key] = $schedule; 
       } else { 
        // Check if use of other aircraft of same type is allowed 
        if($this->RebuildVA->allowOtherAircraftUse()) { 
         if($this->Aircrafts->aircraftTypeAtAirport($schedule['aircraftId'], $schedule['departureICAO'])) { 
          $schedulesAvailable[$key] = $schedule; 
         } else { 
          unset($schedulesAvailable[$key]); 
          continue; 
         } 
        } else { 
         unset($schedulesAvailable[$key]); 
         continue; 
        } 
       } 
      } else { 
       if(isset($schedulesAvailable[$key])) { 
        $schedulesAvailable[$key] = $schedule; 
       } 
      } 

      // Check if there is a limit of only one booking at time per aircraft 
      if(!$this->RebuildVA->allowMultipleAircraftBookings()) { 
       if(!$this->Aircrafts->isBooked($schedule['aircraftId'])) { 
        $schedulesAvailable[$key] = $schedule; 
       } else { 
        unset($schedulesAvailable[$key]); 
        continue; 
       } 
      } else { 
       if(isset($schedulesAvailable[$key])) { 
        $schedulesAvailable[$key] = $schedule; 
       } 
      } 

      // Check if there is a limit of only one booking at time per pilot 
      if(!$this->RebuildVA->allowMultiplePilotBookings()) { 
       if(!$this->Pilots->hasBookedFlight($this->session->userdata('pilotId'))) { 
        $schedulesAvailable[$key] = $schedule; 
       } else { 
        unset($schedulesAvailable[$key]); 
        continue; 
       } 
      } else { 
       if(isset($schedulesAvailable[$key])) { 
        $schedulesAvailable[$key] = $schedule; 
       } 
      } 
     } 
    } else { 
     $schedulesAvailable = $schedules; 
    }  

    return $schedulesAvailable; 
} 

必要な変更をモデルにしました