Hello,
I'm writing an application on top of CodeIgniter to better organize my ebook collection. I'm nearly done, but I realize my 'browse' page is running far too many queries - two per book - to get their information. Obviously not at all ideal, especially since I have about 1000 books to put into this system.
I currently have one model function which gets all of the books (will eventually be modified to take parameters - that's the next step) and another that gets the meta information for each returned book. The second function is the one which makes two queries for each book - one to get the information in the book table and another to get the tags associated with the book. Here are the two model functions:
Get the list of books:
function get_books() {
$this->db->select('isbn')->order_by('title');
$query = $this->db->get('books');
$result = $query->result();
return $result;
}
Get the book meta information:
function get_book_info($isbn) {
// Grab the book from Amazon
$amazon = $this->amazon->get_amazon_item($isbn);
// Get the book info
$this->db->select('title, publisher, date, thumb, filename, pages');
$query = $this->db->get_where('books', array('isbn' => $isbn));
$bookResult = $query->row();
// Get the book's tags
$this->db->select('tag');
$this->db->from('tags AS t');
$this->db->join('books_tags AS bt', 'bt.tag_id = t.id', 'left');
$this->db->where('bt.book_id', $isbn);
$this->db->order_by('t.tag');
$tagQuery = $this->db->get();
foreach ($tagQuery->result() as $row) {
$tagResult[] = $row->tag;
}
$tagResult = implode(', ', $tagResult);
// Send data
$data = array(
'isbn' => $isbn,
'thumb' => $bookResult->thumb,
'title' => strip_slashes($bookResult->title),
'file' => $bookResult->filename,
'publisher' => strip_slashes($bookResult->publisher),
'date' => date('F j, Y', strtotime($bookResult->date)),
'pages' => $bookResult->pages,
'tags' => $tagResult,
'rating' => $amazon->Items->Item->CustomerReviews->AverageRating,
'raters' => $amazon->Items->Item->CustomerReviews->TotalReviews
);
return $data;
}
I'm certain there's a way to write one or two queries that will gather all the records into objects I can then filter through, rather than having to write two queries for each one, but I have no idea where to even start trying to write that. Any suggestions are welcome.
Thanks much, Marcus