2013-09-25 91 views
-3

我知道mysqli_data_seek讓我們移動所需的偏移量,但是如何在特定條件滿足時使用它來向後退一步?我正在使用while-loop作爲條件,因此您可以得出結論,當條件滿足時,指針將位於下一條記錄上。 我怎樣才能把它拉回一步?如何在mysqli_query結果對象中返回一條記錄

+3

你嘗試過什麼嗎? – BlitZ

+0

@CORRUPT嘗試併成功使用計數器變量某種類似的方法在答案中給出的方法,但只是想知道是否有任何迭代器函數爲這個像'prev()' – UDB

+0

不,沒有這樣的內置方法(即'prev ()','next()','current()'或'rewind()',就像['iterators'](http://php.net/iterator)一樣)。然而,你可以擴展['mysqli_result'](http://php.net/mysqli-result)類或創建一個[裝飾器](http://stackoverflow.com/questions/948443/how-to-implement-a -decorator-in-php),可以做到這一點。或者乾脆,創建自己的功能。 – BlitZ

回答

0

試試這個:

$i = 0; 
while($row = mysqli_fetch_row($result)){ 
if($i != 0 && condition){ 
     mysqli_data_seek($result, $i-1); 
} 
$i++; 
} 
1

也不是那麼難,因爲它看起來。

  1. 計數提取的偏移量。
  2. 當您達到條件時,請轉至offset - 1,如果需要,請使用continue重複循環。

例子:

$offset = 0; 

while($row = $result->fetch_assoc()){ 

    if($condition && $offset){ 
     $result->data_seek(--$offset); 
     continue; 
    } 

    $offset++; 

    // actions 

} 

而且,我會引用我的評論:

有沒有這種內置的方法(即prev()next()current()rewind(),就像iterators 一樣)。但是,您可以擴展 mysqli_result類或創建一個 decorator, 可以做到這一點。