Wszystko działa teraz świetnie (wyszukiwanie i przekierowywanie na kolejne podstrony), z tym, że w każdym przypadku wyszukiwania obcina ilość ogłoszeń do 14. Na pierwszej stronie jest ich 10, a na drugiej już 4, pojawia się również link do kolejnej strony (na drugiej stronie), ale po kliknięciu w niego (przejściu na stronę trzecią i wyżej) pojawia się komunikat, że nie ma już ogłoszeń. Przykład działania można zobaczyć na: domowisko.pl i wybrać kryteria wg. województwa np. "mazowieckie" i kliknąć "Szukaj".
Obecny wygląd funkcji z kontrolera odpowiadająca za wyszukiwanie:
function searchAction() {
// get db adapter and session data
$db = Zend_Registry::get('db');
$db->setFetchMode(Zend_Db::FETCH_OBJ);
$search = new Zend_Session_Namespace('search');
$settings = new SettingsTable;
$pageLimit = $settings->fetchRow('title = "pageLimit"');
// if isset post data then set new search variables
if($this->_request->isGet()) {
$search->where = NULL;
// set where
foreach($this->_request->getParams() as $name => $value)
$post[$name] = $value;
if(!empty($post['advertisementType'])) $search->where .= ('(advertisements.advertisementType = '.intval($post['advertisementType'])).') AND '; if(!empty($post['propertyType'])) $search->where .= ('(advertisements.propertyType = '.intval($post['propertyType'])).') AND '; if(!empty($post['province'])) $search->where .= ('(advertisements.province = '.intval($post['province'])).') AND '; if(!empty($post['city'])) $search->where .= ('(advertisements.city LIKE "%'.$post['city'].'%"').') AND ';
if(!empty($post['priceFrom'])) $search->where .= ('(advertisements.price > '.floatval($post['priceFrom'])).') AND '; if(!empty($post['priceFor'])) $search->where .= ('(advertisements.price < '.floatval($post['priceFor'])).') AND '; if(!empty($post['areaSizeFrom'])) $search->where .= ('(advertisements.areaSize > '.intval($post['areaSizeFrom'])).') AND '; if(!empty($post['areaSizeFor'])) $search->where .= ('(advertisements.areaSize < '.intval($post['areaSizeFor'])).') AND '; if(!empty($post['keyword'])) $search->where .= ('(advertisements.otherInfo LIKE "%'.$post['keyword'].'%"').') AND ';
$search->where = substr($search->where, 0
, (strlen($search->where)-4
));
}
if(!empty($search->where) && $this->_request
->getParam('page') >= 1) {
// get count finded advertisements
$advertisements = new AdvertisementsTable;
$select = $advertisements->select();
$select->from($advertisements, 'count(id) as count')
->where($search->where);
$row = $advertisements->fetchRow($select);
$this->view->search = true;
if($row->count > 0) {
// create final query
$select = $db->select();
$select->from('advertisements', array('id', 'areaSize', 'price', 'description', 'city', 'announceDate', 'promo')) ->joinInner('propertyTypes', 'propertyTypes.id = advertisements.propertyType', 'type as propertyType')
->joinInner('advertisementTypes', 'advertisementTypes.id = advertisements.advertisementType', 'type as advertisementType')
->joinInner('provinces', 'provinces.id = advertisements.province', 'province')
->joinLeft('images', 'images.advertisementId = advertisements.id', 'image')
->where($search->where)
->where('dayCounter > 0')
->group('id')
->order(array('promo DESC', 'id DESC')) ->limit($pageLimit->value, ($this->_request->getParam('page')-1)*$pageLimit->value);
$result = $db->query($select);
$this->view->advertisements = $result->fetchAll();
// set pages parameters
$this->view->pagesLink = 'adv/search/';
$this->view->pagesCount = $row->count;
$this->view->pagesLimit = $pageLimit->value;
$this->view->pagesCurrent = $this->_request->getParam('page');
$this->view->province = $this->_request->getParam('province');
$this->view->advertisementType = $this->_request->getParam('advertisementType');
$this->view->propertyType = $this->_request->getParam('propertyType');
$this->view->areaSizeFrom = $this->_request->getParam('areaSizeFrom');
$this->view->areaSizeFor = $this->_request->getParam('areaSizeFor');
$this->view->city = $this->_request->getParam('city');
$this->view->priceFor = $this->_request->getParam('priceFor');
$this->view->keyword = $this->_request->getParam('keyword');
}
}
}
Plik odpowiadający za wyświetlenie linków
<?php
if(!empty($this->advertisements)) {
// calculate count of all pages
$pages = ceil($this->pagesCount / $this->pagesLimit);
// calculage advertisements to number
if($this->pagesCurrent < $pages)
$to = $this->pagesCurrent * $this->pagesLimit;
else
$to = $this->pagesCount;
// show back link / non link
if($this->pagesCurrent > 1)
echo '<a href="'.$this->pagesLink.($this->pagesCurrent - 1).'.html?advertisementType='.$this->advertisementType.'&propertyType='.$this->propertyType.'&province='.$this->province.'&areaSizeFrom='.$this->areaSizeFrom.'&areaSizeFor='.$this->areaSizeFor.'&city='.$this->city.'&priceFrom='.$this->priceFrom.'&priceFor='.$this->priceFor.'&keyword='.$this->keyword.'"><b>Ť poprzednie</b></a>';
else
echo '<b>Ť poprzednie</b>';
// show advertisements stats
echo ' | Wyświetlone <b>'.(($this->pagesCurrent - 1
)*$this->pagesLimit + 1).'</b>-<b>'.$to.' </b>z<b> '.$this->pagesCount.'</b> | ';
// show next link / non link
if($this->pagesCurrent < $pages)
echo '<a href="'.$this->pagesLink.($this->pagesCurrent + 1).'.html?advertisementType='.$this->advertisementType.'&propertyType='.$this->propertyType.'&province='.$this->province.'&areaSizeFrom='.$this->areaSizeFrom.'&areaSizeFor='.$this->areaSizeFor.'&city='.$this->city.'&priceFrom='.$this->priceFrom.'&priceFor='.$this->priceFor.'&keyword='.$this->keyword.'"><b>następne ť</b></a>'; else
echo '<b>następne ť</b>';
}
?>
Routing z wyszukiwania:
routes.search.type = "Zend_Controller_Router_Route_Regex"
routes.search.route = "adv/search/(\d+).html"
routes.search.map.page = 1
routes.search.defaults.controller = adv
routes.search.defaults.action = search
Ten post edytował Ravik 19.03.2011, 15:48:44