dongxinxin7809 2016-10-13 03:30
浏览 25
已采纳

Codeigniter自定义搜索功能问题

Hello I am writing an php application and currently I'm stuck at a method that retrives flights from the database and applies diffrent filters to it. There are no problems when I initially load the page without any filters applied, all records from DB are loaded as expected. Then again everything as expected when I use "Departure Airport" or "Arrival Airport" filters along with "Bookable Only" filter.

It is whole of another story when you try to use "Bookable Only" filter on its own, it doesn't load any records from database. That's the same with "Aircraft" filter, doesn't work on its own and with "Bookable Only" filter but works when combined with both or either one of Airport filters + "Bookable Only" filter

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

I tried debugging everything and following the process step by step as well as trial and error method but none give expected effects. Any ideas?

  • 写回答

1条回答 默认 最新

  • duanbei8904 2016-10-14 02:00
    关注

    By trial and error method I have found some kind of a work around that some how does the job. If anyone has any suggestions regarding it or how I could improve it, please feel free, as I am looking for performance in the app.

    The required changes were in the Model:

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM