2016-05-08 94 views
0

我不知道如何把這個。我有這個功能:作爲變量的快速功能

protected function _do_thumb($temaid) 
{ 
    $firstPost = $this->registry->topics->getPostById($temaid); 
    preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $firstPost['post'], $match); 
    return $match[1];  
} 

[...] 

while ($i = $this->DB->fetch()) 
     { 
      $forum = $this->registry->class_forums->forum_by_id[ $i['forum_id'] ]; 

      if ($this->registry->permissions->check('read', $forum) != TRUE) 
      { 
       continue; 
      } 

      if ($forum['password'] != "") 
      { 
       continue; 
      } 

      $to_echo .= $this->_parseTemplate($row, array ( 
       'topic_title' => str_replace('&#', '&amp;#', $i['title']), 
                  'topic_id'  => $i['tid'], 
                  'topic_link'  => "showtopic=".$i['tid'], 
                  'forum_title' => htmlspecialchars($forum['name']), 
                  'forum_id'  => $i['forum_id'], 
                  'last_poster_id' => $i['last_poster_id'], 
                  'last_post_name' => $i['last_poster_name'], 
                  'last_post_time' => $this->registry->getClass('class_localization')->getDate($i['last_post'] , 'LONG', 1), 
                  'timestamp'  => $i['start_date'], 
                  'starter_id'  => $i['starter_id'], 
                  'starter_name' => $i['starter_name'], 
                  'board_url'  => $this->settings['board_url'], 
                  'board_name'  => $this->settings['board_name'], 
                  'rfc_date'  => date('j\-M\-Y', $i['start_date']), 
       'thumb'   => $this->_do_thumb($i['topic_firstpost']) 
       )) . "\r\n"; 
     }    

$ firstPost應該產生另一個函數的值結果。這裏的問題是該函數似乎在整個循環的一半停止,所以它返回一個不完整的結果,而只返回一個列表的第一個元素。 $ forum但是工作正常,因爲它被表示爲一個變量,所以我相信解決方案可能是以相同的方式表達$ topic。就像這樣:

$this->registry->topics->getPostById[ $temaid ]; 

但是,我不知道我該怎麼做。它甚至有可能嗎?

謝謝。

+0

您確定這是故意的嗎? '$ fetch_sql()' –

+0

是的,我刪節了這個函數,因爲它比這更復雜。但簡單地說,它。 –

+0

你能添加完整的代碼嗎? '$ some_stuff。= array('forum_title'=> $ forum ['name'],'thumb'=> $ topic ['firstpost']);'無效 –

回答

0

getBostById()$i = $this->DB->fetch()在while循環中,您調用getPostById()時,它們都使用$this->DB來存儲來自數據庫的信息。

當被調用getPostById()時,它覆蓋$this->DB中的信息,並將隱藏的指針移動到末尾。這個函數返回來自數據庫的POST數據,並且爲此使用類似$i = $this->DB->fetch()的東西。因此指向數據的指針移動到最後。

接下來,當調用$i = $this->DB->fetch()時,它看到指針在末尾,並停止循環。

解決方法是,當從數據庫中獲取結果時,不要覆蓋它。在致電getPostById之前,請將它們緩存起來:

//while ($i = $this->DB->fetch()) 
$items = array(); 
$counter = 0 ; 
while ($i = $this->DB->fetch()) { 
    $items[$counter++] = $i; 
} 
foreach ($items as $i){ 
     $forum = $this->registry->class_forums->forum_by_id[ $i['forum_id'] ]; 

     if ($this->registry->permissions->check('read', $forum) != TRUE) 
     { 
      continue; 
     } 

     if ($forum['password'] != "") 
     { 
      continue; 
     } 

     $to_echo .= $this->_parseTemplate($row, array ( 
      'topic_title' => str_replace('&#', '&amp;#', $i['title']), 
                 'topic_id'  => $i['tid'], 
                 'topic_link'  => "showtopic=".$i['tid'], 
                 'forum_title' => htmlspecialchars($forum['name']), 
                 'forum_id'  => $i['forum_id'], 
                 'last_poster_id' => $i['last_poster_id'], 
                 'last_post_name' => $i['last_poster_name'], 
                 'last_post_time' => $this->registry->getClass('class_localization')->getDate($i['last_post'] , 'LONG', 1), 
                 'timestamp'  => $i['start_date'], 
                 'starter_id'  => $i['starter_id'], 
                 'starter_name' => $i['starter_name'], 
                 'board_url'  => $this->settings['board_url'], 
                 'board_name'  => $this->settings['board_name'], 
                 'rfc_date'  => date('j\-M\-Y', $i['start_date']), 
      'thumb'   => $this->_do_thumb($i['topic_firstpost']) 
      )) . "\r\n"; 
    }    
+0

它已經是一個已定義的函數。我說的問題不是這樣,而是它具有()而不是[]的事實。這就是爲什麼$論壇工作正常s我試圖做同樣的$主題。 –

+0

你說過,可能是它返回錯誤的數據。所以你在尋找[]。但是你想用函數獲得什麼:帖子或主題?如果您嘗試獲取帖子,那麼您正在使用主題ID。所以這可能是錯誤的。 –

+0

我知道它看起來很奇怪,但它是正確的。它應該返回給定主題ID的第一個帖子。 –