2010-09-30 42 views
0

我寫了下面的CodeIgniter模型函數來抓取並遍歷MySQL查詢結果。如何獲得查詢的整個結果集?

function get_book_info() { 

/* 
* SELECT b.isbn, b.title, b.publisher, b.date, b.thumb, b.filename, b.pages, t.tag 
* FROM books AS b 
* INNER JOIN books_tags AS bt ON b.isbn = bt.book_id 
* INNER JOIN tags AS t ON bt.tag_id = t.id 
* ORDER BY b.title, t.tag 
*/ 

$this->db->select('b.isbn, b.title, b.publisher, b.date, b.thumb, b.filename, b.pages, t.tag'); 
$this->db->from('books AS b'); 
$this->db->join('books_tags AS bt', 'b.isbn = bt.book_id', 'inner'); 
$this->db->join('tags AS t', 'bt.tag_id = t.id', 'inner'); 
$this->db->order_by('b.title, t.tag'); 
$query = $this->db->get(); 
$result = $query->result(); 

$counter = ''; 
$record = $meta = $tags = array(); 
foreach ($result as $book) { 
    // If this is the first appearance of this book 
    if ($counter != $book->isbn) { 
     // If the meta array already exists 
     if ($meta) { 
      // Add the combined tag string to the meta array 
      $meta['tags'] = implode(', ', $tags); 
      // Add the meta array 
      $record[] = $meta; 
      // Empty the tags array 
      $tags = array(); 
     } 
     // Reset the counter 
     $counter = $book->isbn; 
     // Grab the book from Amazon 
     $amazon = $this->amazon->get_amazon_item($book->isbn); 
     // Collect the book information 
     $meta = array(
      'isbn' => $book->isbn, 
      'title' => strip_slashes($book->title), 
      'publisher' => strip_slashes($book->publisher), 
      'date' => date('F j, Y', strtotime($book->date)), 
      'thumb' => $book->thumb, 
      'filename' => $book->filename, 
      'pages' => $book->pages, 
      'rating' => $amazon->Items->Item->CustomerReviews->AverageRating, 
      'raters' => $amazon->Items->Item->CustomerReviews->TotalReviews 
     ); 
     // Add the tag to the tags array 
     $tags[] = $book->tag; 
    } else { 
     // All we need is the tag 
     $tags[] = $book->tag; 
    } 
} 

return $record; 
} 

該代碼適用於除最後一本書以外的所有書 - 它總是會錯過最後一行。我可以看到它爲什麼這樣做,但我不知道如何解決它。這是我嘗試過的,但是我仍然沒有得到包含在$ record數組中的最後一本書。

function get_book_info() { 

    /* 
    * SELECT b.isbn, b.title, b.publisher, b.date, b.thumb, b.filename, b.pages, t.tag 
    * FROM books AS b 
    * INNER JOIN books_tags AS bt ON b.isbn = bt.book_id 
    * INNER JOIN tags AS t ON bt.tag_id = t.id 
    * ORDER BY b.title, t.tag 
    */ 

    $this->db->select('b.isbn, b.title, b.publisher, b.date, b.thumb, b.filename, b.pages, t.tag'); 
    $this->db->from('books AS b'); 
    $this->db->join('books_tags AS bt', 'b.isbn = bt.book_id', 'inner'); 
    $this->db->join('tags AS t', 'bt.tag_id = t.id', 'inner'); 
    $this->db->order_by('b.title, t.tag'); 
    $query = $this->db->get(); 
    $result = $query->result(); 

    $counter = ''; 
    $record = $meta = $tags = array(); 
    $count = count($result); 
    $i = 0; 

    foreach ($result as $book) { 
     // If this is not the last row 
     if ($i < $count) { 
      // If this is the first appearance of this book 
      if ($counter != $book->isbn) { 
       // If the meta array already exists 
       if ($meta) { 
        // Add the combined tag string to the meta array 
        $meta['tags'] = implode(', ', $tags); 
        // Add the meta array 
        $record[] = $meta; 
        // Empty the tags array 
        $tags = array(); 
       } 
       // Reset the counter 
       $counter = $book->isbn; 
       // Grab the book from Amazon 
       $amazon = $this->amazon->get_amazon_item($book->isbn); 
       // Collect the book information 
       $meta = array(
        'isbn' => $book->isbn, 
        'title' => strip_slashes($book->title), 
        'publisher' => strip_slashes($book->publisher), 
        'date' => date('F j, Y', strtotime($book->date)), 
        'thumb' => $book->thumb, 
        'file' => $book->filename, 
        'pages' => $book->pages, 
        'rating' => $amazon->Items->Item->CustomerReviews->AverageRating, 
        'raters' => $amazon->Items->Item->CustomerReviews->TotalReviews 
       ); 
       // Add the tag to the tags array 
       $tags[] = $book->tag; 
      } else { 
       // All we need is the tag 
       $tags[] = $book->tag; 
      } 
     // If this is the last row 
     } else { 
      // If this is the first appearance of this book 
      if ($counter != $book->isbn) { 
       // Grab the book from Amazon 
       $amazon = $this->amazon->get_amazon_item($book->isbn); 
       // Collect the book information 
       $meta = array(
        'isbn' => $book->isbn, 
        'title' => strip_slashes($book->title), 
        'publisher' => strip_slashes($book->publisher), 
        'date' => date('F j, Y', strtotime($book->date)), 
        'thumb' => $book->thumb, 
        'file' => $book->filename, 
        'pages' => $book->pages, 
        'rating' => $amazon->Items->Item->CustomerReviews->AverageRating, 
        'raters' => $amazon->Items->Item->CustomerReviews->TotalReviews 
       ); 
       // Add the tag to the tags array 
       $tags[] = $book->tag; 
       // Add the combined tag string to the meta array 
       $meta['tags'] = implode(', ', $tags); 
       // Add the meta array 
       $record[] = $meta; 
      } else { 
       // All we need is the tag 
       $tags[] = $book->tag; 
       // Add the combined tag string to the meta array 
       $meta['tags'] = implode(', ', $tags); 
       // Add the meta array 
       $record[] = $meta; 
      } 
     } 
     $i++; 
    } 

    return $record; 
} 

有什麼建議嗎?我怎樣才能解決這個問題?

+0

只是問...我相信你,這是失蹤的最後一本書,但你怎麼發現那個出來?你是否在$ record上做了var_dump()來查看已經填充的內容? – Anthony 2010-09-30 14:43:59

+0

print_r($ record); – Marcus 2010-09-30 14:46:05

回答

0

我想通了。我有它在第二個例子接近直角的,我只需要初始化我的$ I計數器來代替1 0

$i = 1; 
+1

只需要9分鐘! – kevtrout 2010-09-30 15:43:52

相關問題