Codeigniter自定义搜索功能问题

时间:2016-10-13 03:30:44

标签: php codeigniter search filter

您好我正在编写一个php应用程序,目前我遇到了一种从数据库中重新启动航班并对其应用不同过滤器的方法。当我最初加载页面时没有应用任何过滤器没有问题,来自DB的所有记录都按预期加载。当我使用“Departure Airport”或“Arrival Airport”过滤器以及“Bookable Only”过滤器时,所有内容都会如预期一样。

当您尝试单独使用“仅可预订”过滤器时,它是另一个故事,它不会从数据库加载任何记录。这与“飞机”过滤器相同,不能单独使用和“仅可预订”过滤器,但与机场过滤器+“仅可预订”过滤器中的任何一个或任何一个结合使用

Schedules_model.php

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); 
}

我尝试调试所有内容并按照一步一步的过程以及试错法进行,但没有给出预期的效果。有什么想法吗?

1 个答案:

答案 0 :(得分: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;
}