2012-07-16 119 views
2

我可能只是在做一些愚蠢的事情,但我看不到錯誤的原因。我在使用PHP編寫的分頁函數的for循環中出現Parse錯誤;繼承人的完整錯誤。PHP for`loop解析錯誤

PHP Parse error: parse error, expecting `')'' in /Users/danielsgroves/Sites/Gouland/search.php on line 95 

而全功能的錯誤是發生在如下:

private function pagination($scope, $keyword) 
{ 
    $pages = 15; 

    $pagination = '<ul>'; 
    for ($i = 0; $i < $pages; i++) { 
     $pagination .= "<li><a id=\"a$i\" class=\"na\" href=\"?q=$keyword&scope=$scope&page=$i\">$i</a></li>"; 
    } 
    $pagination .= '</ul>'; 

    return $pagination; 
} 

正如你可以從我說的錯誤中提到的線95 for ($i = 0; $等已經建立下面是全班文件。我已經包含評論,只是爲了幫助它。

Class BingSearch 
{ 
private $APPID  = "SOME API KEY"; 

private $keyword; 
private $scope; 
private $page; 
private $count; 
private $offset; 

private $response; 
private $results; 

/** 
* Set the object up with required information to perform the search. Once 
* the object is setup the getResults() method should be called to display 
* the results where required. 
* @param (String) $keyword = The search string 
* @param (String) $scope = The scope of the search; web, ticket, image, 
* video, news 
* @param (Int) $page = The required results page. 
* @public 
*/ 
public function __construct($keyword, $scope, $page) 
{ 
    $this->keyword = urlencode($keyword); 
    $this->scope = $scope; 
    $this->page  = $page; 
} 

/** 
* Runs the necessary internal methods to fetch and display the required 
* results. Required information should be provided into the constructor 
* on object creation. This function will not return any information, 
* but 'echo' is straight out onto the page. Each returned result will 
* be placed inside it's own HTML5 <article> tag. Simply call this 
* method from the location in your templates of where you would like 
* the search results to be placed. 
* @public 
*/ 
public function getResults() 
{ 
    $this->fetchFeed(); 
    $this->generateWebResults(); 
} 

/** 
* Fetches the JSON feed from the Google bing API. Uses private class 
* variables for the required information. 
* @private 
*/ 
private function fetchFeed() 
{ 
    $request  = "http://api.search.live.net/json.aspx?Appid=$this->APPID&query=$this->keyword&sources=$this->scope&$this->count&$this->offset"; 
    $this->response = file_get_contents($request); 
    $this->results = json_decode($this->response); 
} 

/** 
* Formats the returned JSON feed by adapting it for HTML. Each result is 
* placed within an <article> tag. The title is an <h3> which also links 
* to the actual result. <span class="cached"> is the cache link and 
* the description is placed within a <p>. Finally, <span class="fullLink"> 
* contains the URL for the content, which is also linked. 
* @private 
*/ 
private function generateWebResults() 
{ 
    if($this->results->SearchResponse->Web->Total != 0) { 

     foreach($this->results->SearchResponse->Web->Results as $value) { 

      if (!isset($value->Description)) 
       $value->Description = ''; 

      echo "<article>"; 
      echo "<h3><a href=\"$value->Url\" title=\"Go to $value->Title\">$value->Title</a></h3>"; 
      echo "<span class=\"cached\"><a href=\"$value->CacheUrl\" title=\"View Cached Version\">Cached</a></span>"; 
      echo "<p>" . strip_tags($value->Description) . "</p>"; 
      echo "<span class=\"fullLink\"><a href=\"$value->Url\" title=\"Go to $value->Title\">$value->Url</a></span>"; 
      echo "</article>"; 

     } 

    } 
} 

private function pagination($scope, $keyword) 
{ 
    $pages = 15; 

    $pagination = '<ul>'; 
    for ($i = 0; $i < $pages; i++) { 
     $pagination .= "<li><a id=\"a$i\" class=\"na\" href=\"?q=$keyword&scope=$scope&page=$i\">$i</a></li>"; 
    } 
    $pagination .= '</ul>'; 

    return $pagination; 
} 
} 

要使用該功能,這是運行的PHP。

require 'search.php'; 
$search = new BingSearch($query, $source, $query); 
$search->getResults(); 

有誰看上我指明瞭正確的方向,這可能是一些非常小的,我已經只是錯過看它太久之後。

謝謝,

丹。

回答

6
for ($i = 0; $i < $pages; i++) 

你忘了一個$,代替我++與$ I ++。

+0

非常感謝,知道它會是這樣的小事! – 2012-07-16 09:14:43

2

你的循環

for ($i = 0; $i < $pages; i++) { 
     $pagination .= "<li><a id=\"a$i\" class=\"na\" href=\"?q=$keyword&scope=$scope&page=$i\">$i</a></li>"; 
    } 

你已經錯過了$在循環

for ($i = 0; $i < $pages; $i++) { 
     $pagination .= "<li><a id=\"a$i\" class=\"na\" href=\"?q=$keyword&scope=$scope&page=$i\">$i</a></li>"; 
    } 
2

您忘記了遞增$i的美元符號。

private function pagination($scope, $keyword) 
{ 
    $pages = 15; 

    $pagination = '<ul>'; 
    for ($i = 0; $i < $pages; $i++) { 
     $pagination .= "<li><a id=\"a$i\" class=\"na\" href=\"?q=$keyword&scope=$scope&page=$i\">$i</a></li>"; 
    } 
    $pagination .= '</ul>'; 

    return $pagination; 
} 
0

在該行,以$i++更換i++。它缺少$字符。