我在搜索引擎中显示结果时遇到问题。搜索结果需要8秒才能显示。它很长,没有意义。
有没有办法立即显示一小部分结果(例如,分别显示第1页和第2页),其余几秒后显示?
这是我的代码:
public function index($destination_main_id=null,$accommod_main_id=null,$search_period_code=null,$total_person=null,$pageNum=null, $order=null, $range_days=null){
//post podaci filtriranja
//print_r($_POST['filter']);
if(!empty($_POST['filter'])){
$this->set('ajax_load',1);
}else{
$this->set('ajax_load',0);
}
//inicijacija varijabli
if(!empty($_POST['radio1'])){
$this->request->data['SearchEngine']['accommod_main_id']=$_POST['radio1'];
}
if($order==''){
$order='asc';
}
if($range_days==''){
$range_days=0;
}
//search period
if(!$search_period_code){
$search_period=$this->Calendar->getDatesBetweenDates($this->request->data['SearchEngine']['arrival'], $this->request->data['SearchEngine']['departure']);
$search_period_code = base64_encode(serialize($search_period));
}else{
$search_period = @unserialize(base64_decode($search_period_code));
}
$count_search_period=count($search_period);
//destination
if(!empty($this->request->data['SearchEngine']['destination_main_id'])){
$destination_main_id=$this->request->data['SearchEngine']['destination_main_id'];
}
//accommodation type
if(!empty($this->request->data['SearchEngine']['accommod_main_id'])){
$accommod_main_id=$this->request->data['SearchEngine']['accommod_main_id'];
}
//person calculation
if(!empty($this->request->data['SearchEngine']['adults']) OR !empty($this->request->data['SearchEngine']['children1']) OR !empty($this->request->data['SearchEngine']['children2']) OR !empty($this->request->data['SearchEngine']['baby']) ){
if($this->request->data['SearchEngine']['baby']>=3){
$total_baby=round($this->request->data['SearchEngine']['baby']/3);
}else{
$total_baby=0;
}
$total_person=$this->request->data['SearchEngine']['adults']+$this->request->data['SearchEngine']['children1']+$this->request->data['SearchEngine']['children2']+$total_baby;
}
//+/- radio raspon
if(!empty($this->request->data['SearchEngine']['days'])){
$range_days=$this->request->data['SearchEngine']['days'];
}
//display accommodation units
if(!empty($_POST['filter']))
{
$AccommodUnits=$this->AccommodUnit->getAllFilterUnits($total_person,$destination_main_id,$_POST['filter'],$this->lang);
}else{
$AccommodUnits=$this->AccommodUnit->getAllUnits($total_person,$destination_main_id,$this->lang);
}
//display accommodation objects
$AccommodObjectIds1=array();
$AccommodMainAccommodObjects=$this->AccommodMainAccommodObject->find('all',array('conditions' => array('AccommodMainAccommodObject.accommod_main_id' => $accommod_main_id),'order' => array('AccommodMainAccommodObject.accommod_object_id')));
foreach($AccommodMainAccommodObjects as $current1){
$this->AccommodObject->locale = $this->lang;
$this->AccommodObject->recursive = -1;
$AccommodObject=$this->AccommodObject->find('first',array('conditions' => array('AccommodObject.id' => $current1['AccommodMainAccommodObject']['accommod_object_id'],'AccommodObject.active' => '1')));
if(!empty($AccommodObject['AccommodObject']['id'])){
$AccommodObjectIds1[]=$AccommodObject['AccommodObject']['id'];
}
}
//filtriranje by objects
$AccommodObjectIds3=array();
if( !empty($_POST['filter']['benefit']) ){
foreach($AccommodObjectIds1 as $current10){
$AccommodObject=$this->AccommodObject->getFilterObject($current10,$_POST['filter'],$this->lang);
if(!empty($AccommodObject)){
$AccommodObjectIds3[]=$AccommodObject['AccommodObject']['id'];
}
}
}else{
$AccommodObjectIds3=$AccommodObjectIds1;
}
//filtering by plyce
$AccommodObjectIds2=array();
if(!empty($_POST['filter']['place']))
{
$AccommodObjectDestinationArounds=$this->AccommodObjectDestinationAround->find('all',array('conditions' => array('AccommodObjectDestinationAround.destination_around_id' => $_POST['filter']['place']),'order' => array('AccommodObjectDestinationAround.accommod_object_id')));
foreach($AccommodObjectDestinationArounds as $current8){
$AccommodObjectIds2[]=$current8['AccommodObjectDestinationAround']['accommod_object_id'];
}
}
$AccommodObjectIdsFinale=array();
if(!empty($AccommodObjectIds2)){
foreach($AccommodObjectIds2 as $current9){
if(in_array($current9, $AccommodObjectIds3)) {
$AccommodObjectIdsFinale[]=$current9;
}
}
}else{
$AccommodObjectIdsFinale=$AccommodObjectIds3;
}
$arrival1=date('d.m.Y.',strtotime($search_period[0]));
$departure1=date('d.m.Y.',strtotime($search_period[count($search_period)-1]));
$arrival2=date('Y-m-d',strtotime($search_period[0]));
$departure2=date('Y-m-d',strtotime($search_period[count($search_period)-1]));
$AccommodUnitCounts=array();
$i=0;
foreach($AccommodUnits as $current2){
if(in_array($current2['AccommodUnit']['accommod_object_id'], $AccommodObjectIdsFinale)) {
$AccommodUnitCounts[$i]=$current2;
$AccommodUnitCounts[$i]['AccommodUnit']['arrival']=$arrival1;
$AccommodUnitCounts[$i]['AccommodUnit']['departure']=$departure1;
}
$i++;
}
$this->set('destination_main_id', $destination_main_id);
$this->set('accommod_main_id', $accommod_main_id);
$this->set('search_period_code', $search_period_code);
$this->set('total_person', $total_person);
$this->set('order',$order);
$this->set('range_days',$range_days);
$this->set('arrival',$arrival1);
$this->set('departure',$departure1);
//danas i tekuća godina
$today=date("Y-m-d");
$year=date("Y");
///////////////definition of free units (booking)
$SearchUnits=array();
$SearchUnitsIDs=array();
if(!empty($AccommodUnitCounts)){
$i=0;
foreach($AccommodUnitCounts as $current3){
//booking
$this->AccommodUnitBooking->locale = $this->lang;
$this->AccommodUnitBooking->recursive = -1;
$AccommodUnitBooking=$this->AccommodUnitBooking->find('first',array('conditions' => array('AccommodUnitBooking.accommod_unit_id' => $current3['AccommodUnit']['id'])));
if(!empty($AccommodUnitBooking)){
$this->AccommodUnitTablebooking->locale = $this->lang;
$this->AccommodUnitTablebooking->recursive = -1;
$AccommodUnitTablebookings=$this->AccommodUnitTablebooking->find('all',array('conditions' => array('AccommodUnitTablebooking.accommod_unit_id' => $current3['AccommodUnit']['id'], 'AccommodUnitTablebooking.accommod_unit_booking_id' => $AccommodUnitBooking['AccommodUnitBooking']['id'],'AccommodUnitTablebooking.booking' => $search_period ),'order' => array('AccommodUnitTablebooking.booking' => 'asc')));
if(!empty($AccommodUnitTablebookings)){
unset($current3);
}else{
$SearchUnits[]=$current3;
$SearchUnitsIDs[]=$current3['AccommodUnit']['id'];
}
}else{
$SearchUnits[]=$current3;
$SearchUnitsIDs[]=$current3['AccommodUnit']['id'];
}
$i++;
}
}
///////////////price calculation
if(!empty($SearchUnits)){
$i=0;
foreach($SearchUnits as $current6){
$booking_interval=count($this->Calendar->getDatesBetweenDates($arrival2, $departure2))-1;
$booking_interval_dates=array_slice($this->Calendar->getDatesBetweenDates($arrival2, $departure2),0,-1);
$accommod_booking=0;
if(!empty($current6['AccommodUnit']['fl_minute_start']) AND !empty($current6['AccommodUnit']['fl_minute_end']) AND !empty($current6['AccommodUnit']['fl_minute_price']) AND $current6['AccommodUnit']['fl_minute_start']==$arrival2 AND $current6['AccommodUnit']['fl_minute_end']==$departure2 ){ //LAST/FIRST MINUTE CIJENA
$accommod_booking=$current6['AccommodUnit']['fl_minute_price'];
$total=$accommod_booking*$booking_interval;
}else{
$this->AccommodUnitPrice->locale = $this->lang;
$this->AccommodUnitPrice->recursive = -1;
$AccommodUnitPrice=$this->AccommodUnitPrice->find('first',array('conditions' => array('AccommodUnitPrice.accommod_unit_id' => $current6['AccommodUnit']['id'])));
$period=array();
$total=0;
foreach($booking_interval_dates as $interval_date){
for($j=1; $j<=7; $j++){
if(!empty($AccommodUnitPrice['AccommodUnitPrice']['period'.$j])){
$period[$j]=$AccommodUnitPrice['AccommodUnitPrice']['period'.$j];
$date[$j]=split('[/.-]', $period[$j]);
$date_start[$j]=$year.'-'.$date[$j][1].'-'.$date[$j][0];
$date_end[$j]=$year.'-'.$date[$j][4].'-'.$date[$j][3];
if($date_start[$j]<=$interval_date AND $date_end[$j]>=$interval_date){//ako interval rezervacije pada u razdoblje cjenika
$this->AccommodUnitTableprice->locale = $this->lang;
$this->AccommodUnitTableprice->recursive = -1;
$AccommodUnitTableprice=$this->AccommodUnitTableprice->find('first',array('conditions' => array('AccommodUnitTableprice.accommod_unit_id' => $current6['AccommodUnit']['id'],'AccommodUnitTableprice.accommod_unit_price_id' => $AccommodUnitPrice['AccommodUnitPrice']['id'],'AccommodUnitTableprice.person_no' => $total_person,'AccommodUnitTableprice.day_no <=' => $booking_interval),'order' => array('AccommodUnitTableprice.person_no' => 'asc') ));
if(empty($AccommodUnitTableprice)){
$AccommodUnitTableprice=$this->AccommodUnitTableprice->find('first',array('conditions' => array('AccommodUnitTableprice.accommod_unit_id' => $current6['AccommodUnit']['id'],'AccommodUnitTableprice.accommod_unit_price_id' => $AccommodUnitPrice['AccommodUnitPrice']['id'],'AccommodUnitTableprice.day_no <=' => $booking_interval),'order' => array('AccommodUnitTableprice.person_no' => 'asc') ));
}
if(!empty($AccommodUnitTableprice)){
$accommod_booking=$AccommodUnitTableprice['AccommodUnitTableprice']['price'.$j];
$total=$total+$accommod_booking;
}
}
}
}
}
}
//destination
$this->DestinationMain->locale = $this->lang;
$this->DestinationMain->recursive = -1;
$DestinationMain=$this->DestinationMain->find('first',array('conditions' => array('DestinationMain.id' => $current6['AccommodUnit']['destination_main_id'],'DestinationMain.active' => '1')));
$SearchUnits[$i]['DestinationMain']=$DestinationMain['DestinationMain'];
//accommodation object
$this->AccommodObject->locale = $this->lang;
$this->AccommodObject->recursive = -1;
$AccommodObject=$this->AccommodObject->find('first',array('conditions' => array('AccommodObject.id' => $current6['AccommodUnit']['accommod_object_id'],'AccommodObject.active' => '1')));
$SearchUnits[$i]['AccommodObject']=$AccommodObject['AccommodObject'];
//url-ovi kod tražilice za smještajnu jedinicu
$SearchUnits[$i]['AccommodUnit']['arrival_url']=date('Y-m-d',strtotime($current6['AccommodUnit']['arrival']));
$SearchUnits[$i]['AccommodUnit']['departure_url']=date('Y-m-d',strtotime($current6['AccommodUnit']['departure']));
//kalkulacija
if(!empty($AccommodObject['AccommodObject']['info_cleaning']) AND !empty($AccommodObject['AccommodObject']['supplement_cleaning'])){
$SearchUnits[$i]['AccommodUnit']['total_price']=$total+$AccommodObject['AccommodObject']['supplement_cleaning'];
}else{
$SearchUnits[$i]['AccommodUnit']['total_price']=$total;
}
$i++;
}
}
////filtriranje po cijeni
$max_value=800; //max price
if(!empty($_POST['filter']['price1']) OR (!empty($_POST['filter']['price2']) AND !empty($_POST['filter']['price2'])!=$max_value)){
$i=0;
$FilterPriceUnits=array();
foreach($SearchUnits as $current11){
if($current11['AccommodUnit']['total_price']>$_POST['filter']['price1'] AND $current11['AccommodUnit']['total_price']<=$_POST['filter']['price2'] ){
$FilterPriceUnits[]=$current11;
}
$i++;
}
$SearchUnits=array();
$SearchUnits=$FilterPriceUnits;
}
//pageing
$limit=15;
if($pageNum=='' OR !is_numeric($pageNum)){
$pageNum=1;
}
$nOfItems=count($SearchUnits);
$pages=$this->Pagination->makePagination($nOfItems, $pageNum, $limit,2, 2, '...');
$this->set('pages',$pages);
$this->set('pageNum',$pageNum);
$SearchUnitFinales = Set::sort($SearchUnits, '{n}.AccommodUnit.total_price', $order);
$offset=($pageNum-1)*$limit;
$SearchUnitSlices = array_slice($SearchUnitFinales, $offset, $limit);
$this->set('accommod_unit_slices', $SearchUnitSlices);
}
答案 0 :(得分:0)
由于您的标记显示 php ,我建议您查看memcached
。此外,您还需要在页面更改时使用分页和加载数据,这是一个示例:
//user clicked page number, 1,2,3 etc...
$currentPage = isset($_POST['page']) ? (int)$_POST['page'] : 1;
// get 25 rows per page
$startLimit = ($currentPage-1) * 25;
$endLimit = $startLimit + 25;
然后您可以在查询中使用这些限制,每页只能抓取25条记录。此外,我会使用缓存,所以每次更改页面时都不必执行查询。