Skip to content

Commit

Permalink
Fixed the request of multiple page results.
Browse files Browse the repository at this point in the history
  • Loading branch information
b4oshany committed Nov 14, 2015
1 parent f90a553 commit 7748ae7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,29 @@ echo "<br/>";
echo "Average price: ".(intval($total_price / $result_count)); # Prints 39239.94
```

## Other Examples

Get the list of makes from [Dubizzle] ([Live Demo](http://www.osoobe.com/devlabs/php/demo/dubizzle-php/demo/get_makes.php)):
```php

use Dubizzle\Category;

$category = new Category();
$makes = $category->get_makes(Category::$uae["categories"]["options"]['cars']);
```


Get the list of models from [Dubizzle] ([Live Demo](http://www.osoobe.com/devlabs/php/demo/dubizzle-php/demo/get_models.php)):
```php

use Dubizzle\Category;

$category = new Category();
$mmodels = $category->get_models(Category::$uae["makes"]["options"]['audi']);
```



## Search Parameters

### General
Expand Down
5 changes: 4 additions & 1 deletion demo/demo2.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"min_year"=>2007,
"num_results"=>'all'];

$uae = new Search($params, 50);
$uae = new Search($params, 120);

$query = $uae->search();
$query->fetch();
Expand All @@ -26,6 +26,9 @@
$total_price += $result["price"];
}

echo "URL: ".$uae->query_url();
echo "<br/>";
echo "<br/>";
echo "Num. Results: ".$result_count;
echo "<br/>";
echo "<br/>";
Expand Down
30 changes: 15 additions & 15 deletions src/Dubizzle/Results.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public static function get_more_results($html){
$clean_html = $purifier->purify($html);

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

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

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

$num_results_on_page = count($items);


# Get the total number of pages.
try{
$last_page_query = $this->dom->find('.paging_forward #last_page')->getAttribute("href");
preg_match("/page=(\d+)/", $last_page_query, $num_pages_query);
if(isset($num_pages_query[0][1])){
$this->num_pages = intval(explode("=", $num_pages_query[0])[1]);
}else{
$this->num_pages = 1;
}
}catch(Exception $e){
$last_page_query = $this->dom->find('.paging_forward #last_page')->getAttribute("href");
if(empty($last_page_query)){
$last_page_query = $this->dom->find('.paging_forward a')[1]->getAttribute("href");
}

preg_match("/page=(\d+)/", $last_page_query, $num_pages_query);
if(isset($num_pages_query[0][1])){
$this->num_pages = intval(explode("=", $num_pages_query[0])[1]);
}else{
$this->num_pages = 1;
}

Expand All @@ -163,7 +164,6 @@ public function fetch(){
if($this->num_results > $this->total_results || $this->num_results == "all"){
$this->num_results = $this->total_results;
}

if($num_results_on_page > 0){
$page_needed = intval($this->num_results / $num_results_on_page );
}else{
Expand All @@ -172,8 +172,8 @@ public function fetch(){

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

# Fetch additional pages.
Expand Down

0 comments on commit 7748ae7

Please sign in to comment.