嘿伙计们试图在cakephp中创建一个高级搜索表单,
表格包括下拉列表,复选框(HABTM关系),文本字段和价格游侠(滑块), 您可以看到以下表格:
如您所见,我有多个下拉列表,多个复选框等。
搜索效果很好,但我想要的是如何在网址上保存这些更改或过滤器,例如在用户选择了几个过滤器之后:
我希望网址如下: http://localhost/website/cakephpapplication/search?location=something&room_type=sth&accommodation_type=something& .... 所以一个
我使用的动作就是这个:
////////////////////////////////////////////////////////////////////////////////////
/**
* view method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function search() {
$this->layout = 'frontend_search';
$default_lang_code = Configure::read('Config.language');
if($this->Session->check('Config.language')) { // Check for existing language session
$language_code = $this->Session->read('Config.language'); // Read existing language
$languageData = $this->Language->find('first', array('conditions' => array('Language.language_code'=>$language_code),'recursive' => -1,'fields' => array('Language.id'),'limit' => 1));
if (!empty($languageData)) {
$language_id = $languageData['Language']['id'];
} else {
$language_id = $this->Language->findLanguageIdByCode($default_lang_code);
}
} else {
$language_code = $default_lang_code; // Read existing language
$languageData = $this->Language->find('first', array('conditions' => array('Language.language_code'=>$default_lang_code),'recursive' => -1,'fields' => array('Language.id'),'limit' => 1));
$language_id = $languageData['Language']['id'];
}
$localeCurrency = 'EUR';
if ($this->Session->check('LocaleCurrency')) {
$localeCurrency = $this->Session->read('LocaleCurrency');
}
$currencies = $this->Currency->find('all', array( 'recursive'=>-1));
if ($this->request->is('post')) {
if(!empty($this->request->data)){
/*THIS IS THE MOS REQUESTED PROPERTIES */
$searchQuery =" SELECT Property.* , PropertyTranslation.*, Wishlist.*, Currency.*";
$searchQuery .=" FROM properties as Property";
$searchQuery .=" JOIN property_translations as PropertyTranslation";
$searchQuery .=" ON Property.id = PropertyTranslation.property_id";
$searchQuery .=" JOIN users as User";
$searchQuery .=" ON User.id = Property.user_id";
$searchQuery .=" LEFT JOIN wishlists as Wishlist";
$searchQuery .=" ON Wishlist.property_id = Property.id ";
$searchQuery .=" LEFT JOIN currencies as Currency";
$searchQuery .=" On Currency.id = Property.currency_id";
if (isset($this->request->data['Safety']) && !empty($this->request->data['Safety']) && $this->request->data['Safety']!='') {
$searchQuery .=" JOIN properties_safeties as PropertiesSafety";
$searchQuery .=" ON Property.id = PropertiesSafety.property_id";
foreach ($this->request->data['Safety'] as $key => $value) {
$searchQuery .= " AND PropertiesSafety.safety_id = '".$value."'";
}
}
if (isset($this->request->data['Extra']) && !empty($this->request->data['Extra']) && $this->request->data['Extra']!='') {
$searchQuery .=" JOIN extras_properties as ExtrasProperty";
$searchQuery .=" ON Property.id = ExtrasProperty.property_id";
foreach ($this->request->data['Extra'] as $key => $value) {
$searchQuery .= " AND ExtrasProperty.extra_id = '".$value."'";
}
}
if (isset($this->request->data['Service']) && !empty($this->request->data['Service']) && $this->request->data['Service']!='') {
$searchQuery .=" JOIN properties_services as PropertiesService";
$searchQuery .=" ON Property.id = PropertiesService.property_id";
foreach ($this->request->data['Service'] as $key => $value) {
$searchQuery .= " AND PropertiesService.service_id = '".$value."'";
}
}
if (isset($this->request->data['Characteristic']) && !empty($this->request->data['Characteristic']) && $this->request->data['Characteristic']!='') {
$searchQuery .=" JOIN characteristics_properties as CharacteristicsProperty";
$searchQuery .=" ON Property.id = CharacteristicsProperty.property_id";
foreach ($this->request->data['Characteristic'] as $key => $value) {
$searchQuery .= " AND CharacteristicsProperty.characteristic_id = '".$value."'";
}
}
$searchQuery .=" WHERE Property.property_status=1 AND Property.publish_status=1 AND User.is_first_post=0 AND PropertyTranslation.language_id='".$language_id."' ";
if (isset($this->request->data['Property']['capacity']) && !empty($this->request->data['Property']['capacity']) && $this->request->data['Property']['capacity']!='') {
$searchQuery .= " AND Property.capacity ='".$this->request->data['Property']['capacity']."'";
}
if (isset($this->request->data['Property']['address']) && !empty($this->request->data['Property']['address']) && trim($this->request->data['Property']['address'])!='') {
$searchQuery .= " AND Property.address LIKE '%".$this->request->data['Property']['address']."%' ";
}
if (isset($this->request->data['Property']['capacity']) && !empty($this->request->data['Property']['capacity']) && $this->request->data['Property']['capacity']!='') {
$searchQuery .= " AND Property.capacity ='".$this->request->data['Property']['capacity']."'";
}
if (isset($this->request->data['Property']['room_number']) && !empty($this->request->data['Property']['room_number']) && $this->request->data['Property']['room_number']!='') {
$searchQuery .= " AND Property.room_number ='".$this->request->data['Property']['room_number']."'";
}
if (isset($this->request->data['Property']['bed_number']) && !empty($this->request->data['Property']['bed_number']) && $this->request->data['Property']['bed_number']!='') {
$searchQuery .= " AND Property.bed_number ='".$this->request->data['Property']['bed_number']."'";
}
if (isset($this->request->data['Property']['bathroom_number']) && !empty($this->request->data['Property']['bathroom_number']) && $this->request->data['Property']['bathroom_number']!='') {
$searchQuery .= " AND Property.bathroom_number ='".$this->request->data['Property']['bathroom_number']."'";
}
if (isset($this->request->data['Property']['property_type'])!='' && !empty($this->request->data['Property']['property_type']) && trim($this->request->data['Property']['property_type'])!='') {
$searchQuery .= " AND Property.property_type LIKE '%".$this->request->data['Property']['property_type']."%'";
}
if (isset($this->request->data['Property']['property_type'])!='' && !empty($this->request->data['Property']['property_type']) && $this->request->data['Property']['property_type']=='sale') {
if (isset($this->request->data['Property']['surface_area'])!='' && !empty($this->request->data['Property']['surface_area'])) {
$searchQuery .= " AND Property.surface_area = '".$this->request->data['Property']['surface_area']."'";
}
if (isset($this->request->data['Property']['sale_price'])!='' && !empty($this->request->data['Property']['sale_price'])) {
$searchQuery .= " AND Property.sale_price = '".$this->request->data['Property']['sale_price']."'";
}
}
if (isset($this->request->data['Property']['room_type_id']) && !empty($this->request->data['Property']['room_type_id']) && $this->request->data['Property']['room_type_id']!='') {
$searchQuery .= " AND Property.room_type_id = '".$this->request->data['Property']['room_type_id']."'";
}
if (isset($this->request->data['Property']['accommodation_type_id']) && !empty($this->request->data['Property']['accommodation_type_id']) && $this->request->data['Property']['accommodation_type_id']!='') {
$searchQuery .= " AND Property.accommodation_type_id = '".$this->request->data['Property']['accommodation_type_id']."'";
}
if (isset($this->request->data['Property']['price']) && !empty($this->request->data['Property']['price']) && $this->request->data['Property']['price']!='') {
$price = $this->request->data['Property']['price'];
list($minPrice, $maxPrice) = explode(";", $price);
$searchQuery .= " AND Property.rent_daily_price BETWEEN '".$minPrice."' AND '".$maxPrice."'";
}
$searchQuery .=" ORDER BY Property.id";
$searchResults = $this->Property->query($searchQuery);
}
} else {
$searchQuery =" SELECT Property.*, PropertyTranslation.*, Wishlist.*, Currency.* ";
$searchQuery .=" FROM properties as Property";
$searchQuery .=" JOIN property_translations as PropertyTranslation";
$searchQuery .=" ON Property.id = PropertyTranslation.property_id";
$searchQuery .=" JOIN users as User";
$searchQuery .=" ON User.id = Property.user_id";
$searchQuery .=" LEFT JOIN wishlists as Wishlist";
$searchQuery .=" ON Wishlist.property_id = Property.id ";
$searchQuery .=" LEFT JOIN currencies as Currency";
$searchQuery .=" On Currency.id = Property.currency_id";
$searchQuery .=" WHERE Property.property_status=1 AND Property.publish_status=1 AND User.is_first_post=0 AND PropertyTranslation.language_id='".$language_id."'";
$searchQuery .=" ORDER BY Property.id";
$searchResults = $this->Property->query($searchQuery);
}
$this->set(compact('searchResults','roomTypes','accommodationTypes','safeties','extras','services','characteristics','currencies'));
}
////////////////////////////////////////////////////////////////////////////////////
我真的很感谢你的帮助。 提前谢谢。
答案 0 :(得分:1)
搜索效果很好,但我想要的是如何在网址上保存这些更改或过滤器,例如在用户选择了几个过滤器之后:
您希望实现将POST变为GET的the PRG pattern。
自己动手,wiki文章解释了模式,或使用现有的插件: