Using Codeigniter 3, I would like to display a HTML table consisting of some book details, namely Item ID, Item Image and Item Title. I would like to populate the Item Image via Google Books.
So far my code works, in the sense that I can retrieve all the data from the MySQL database and display it in a simple HTML table. However I'm not sure how to populate the image field in the HTML table with the corresponding image from Google Books.
I am retrieving the ISBN from my database, can I use this to lookup the Google Books URL?
I tried to create a foreach
as you will see but it doesn't work so I have commented it out. The error message I receive in the foreach
is;
Message: Undefined index: items on line 23
My current code is below;
Model
class Items_model extends CI_Model {
public function itemList() {
$query = $this->db->get('item', 10);
return $query->result_array();
}
}
Controller
class Items extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('items_model');
$this->load->database();
}
public function index() {
$data['items'] = $this->items_model->itemList();
//foreach ($data['items'] as $row)
//{
// $page = //file_get_contents("https://www.googleapis.com/books/v1/volumes?q=isbn:".$row['item_isbn']);
// $bookData = json_decode($page, true);
// $data['BookImage'] = '<img src="'.$bookData['items'][0]['volumeInfo']['imageLinks']['thumbnail'].'" alt="Cover">'; <-- line 23
//}
$this->load->view('item_view', $data);
}
}
View
<table>
<tr>
<td><strong>ID</strong></td>
<td><strong>ISBN</strong></td>
<td><strong>Image</strong></td>
<td><strong>Title</strong></td>
</tr>
<?php foreach ($items as $item): ?>
<tr>
<td><?php echo $item['item_id']; ?></td>
<td><?php echo $item['item_isbn'] ?></td>
<td><?php // what goes here? ?></td>
<td><?php echo $item['item_title']; ?></td>
</tr>
<?php endforeach; ?>
</table>
Any help is appreciated.
You need to use the Googlebooks API to get the id of your book.
First you have to make a request in this way:
foreach ($items as $item):
$data = @file_get_contents('https://www.googleapis.com/books/v1/volumes?q=isbn+".$yourISBN."');
$data = json_decode($data);
$data = $data->items[0]->id;
// Next you have to insert $data(the item_id) inside of one of the
followings links:
// "smallThumbnail": "https://books.google.com/books?id=zyTCAlFPjgYC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
// "thumbnail": "https://books.google.com/books?id=zyTCAlFPjgYC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api",
// "small": "https://books.google.com/books?id=zyTCAlFPjgYC&printsec=frontcover&img=1&zoom=2&edge=curl&source=gbs_api",
// "medium": "https://books.google.com/books?id=zyTCAlFPjgYC&printsec=frontcover&img=1&zoom=3&edge=curl&source=gbs_api",
// "large": "https://books.google.com/books?id=zyTCAlFPjgYC&printsec=frontcover&img=1&zoom=4&edge=curl&source=gbs_api",
// "extraLarge": "https://books.google.com/books?id=zyTCAlFPjgYC&printsec=frontcover&img=1&zoom=6&edge=curl&source=gbs_api"
//Example:
$thumbnail = "https://books.google.com/books?id=".$data."&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api";
endforeach;
And then you've got your thumbnail.
Note that your id must have this format "zyTCAlFPjgYC". Have a look at the doc