Skip to content

Commit 7748ae7

Browse files
committed
Fixed the request of multiple page results.
1 parent f90a553 commit 7748ae7

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,29 @@ echo "<br/>";
107107
echo "Average price: ".(intval($total_price / $result_count)); # Prints 39239.94
108108
```
109109

110+
## Other Examples
111+
112+
Get the list of makes from [Dubizzle] ([Live Demo](http://www.osoobe.com/devlabs/php/demo/dubizzle-php/demo/get_makes.php)):
113+
```php
114+
115+
use Dubizzle\Category;
116+
117+
$category = new Category();
118+
$makes = $category->get_makes(Category::$uae["categories"]["options"]['cars']);
119+
```
120+
121+
122+
Get the list of models from [Dubizzle] ([Live Demo](http://www.osoobe.com/devlabs/php/demo/dubizzle-php/demo/get_models.php)):
123+
```php
124+
125+
use Dubizzle\Category;
126+
127+
$category = new Category();
128+
$mmodels = $category->get_models(Category::$uae["makes"]["options"]['audi']);
129+
```
130+
131+
132+
110133
## Search Parameters
111134

112135
### General

demo/demo2.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"min_year"=>2007,
1515
"num_results"=>'all'];
1616

17-
$uae = new Search($params, 50);
17+
$uae = new Search($params, 120);
1818

1919
$query = $uae->search();
2020
$query->fetch();
@@ -26,6 +26,9 @@
2626
$total_price += $result["price"];
2727
}
2828

29+
echo "URL: ".$uae->query_url();
30+
echo "<br/>";
31+
echo "<br/>";
2932
echo "Num. Results: ".$result_count;
3033
echo "<br/>";
3134
echo "<br/>";

src/Dubizzle/Results.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public static function get_more_results($html){
4545
$clean_html = $purifier->purify($html);
4646

4747
# Build a HTML parser to search for items.
48-
$this->dom = new Dom;
49-
$this->dom->load($clean_html);
48+
$dom = new Dom;
49+
$dom->load($clean_html);
5050

5151
# Get result items from the HTML.
5252
$items = $dom->find(".listing-item");
@@ -125,7 +125,7 @@ private static function parse_items($items){
125125
* This function also parse the initial result page and fetch additional
126126
* pages to build the list of associated arrary for item information.
127127
*/
128-
public function fetch(){
128+
public function fetch($start_page = 2){
129129
# Track time;
130130
$this->time = time();
131131

@@ -145,16 +145,17 @@ public function fetch(){
145145

146146
$num_results_on_page = count($items);
147147

148+
148149
# Get the total number of pages.
149-
try{
150-
$last_page_query = $this->dom->find('.paging_forward #last_page')->getAttribute("href");
151-
preg_match("/page=(\d+)/", $last_page_query, $num_pages_query);
152-
if(isset($num_pages_query[0][1])){
153-
$this->num_pages = intval(explode("=", $num_pages_query[0])[1]);
154-
}else{
155-
$this->num_pages = 1;
156-
}
157-
}catch(Exception $e){
150+
$last_page_query = $this->dom->find('.paging_forward #last_page')->getAttribute("href");
151+
if(empty($last_page_query)){
152+
$last_page_query = $this->dom->find('.paging_forward a')[1]->getAttribute("href");
153+
}
154+
155+
preg_match("/page=(\d+)/", $last_page_query, $num_pages_query);
156+
if(isset($num_pages_query[0][1])){
157+
$this->num_pages = intval(explode("=", $num_pages_query[0])[1]);
158+
}else{
158159
$this->num_pages = 1;
159160
}
160161

@@ -163,7 +164,6 @@ public function fetch(){
163164
if($this->num_results > $this->total_results || $this->num_results == "all"){
164165
$this->num_results = $this->total_results;
165166
}
166-
167167
if($num_results_on_page > 0){
168168
$page_needed = intval($this->num_results / $num_results_on_page );
169169
}else{
@@ -172,8 +172,8 @@ public function fetch(){
172172

173173
$page_urls = [];
174174
$base_url = preg_replace("/page=(\d+)/", "", $last_page_query);
175-
for($i = 2; $i <= $page_needed; $i++){
176-
array_push($page_urls, $this->url.$base_url."&page=$i");
175+
for($i = $start_page; $i <= $page_needed && $i <= $this->num_pages; $i++){
176+
array_push($page_urls, $this->url."&page=$i");
177177
}
178178

179179
# Fetch additional pages.

0 commit comments

Comments
 (0)