使用 Codeigniter 3,我想显示一个包含一些书籍详细信息的 HTML 表格,即项目 ID、项目图像 和项目标题。我想通过 Google 图书填充项目图像。
到目前为止,我的代码可以正常工作,因为我可以从 MySQL 数据库中检索所有数据并将其显示在一个简单的 HTML 表格中。但是,我不确定如何使用 Google 图书中的相应图像填充 HTML 表格中的图像字段。
我正在从我的数据库中检索 ISBN,我可以使用它来查找 Google 图书 URL 吗?
我尝试创建一个 foreach
,如您所见,但它不起作用,所以我将其注释掉了。我在 foreach
中收到的错误消息是;
Message: Undefined index: items on line 23
我当前的代码如下;
型号
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);
}
}
查看
<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>
感谢任何帮助。
最佳答案
您需要使用 Googlebooks API 来获取您的图书的 ID。
首先你必须通过这种方式提出请求:
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;
然后您就得到了缩略图。
请注意,您的 ID 必须采用“zyTCAlFPjgYC”格式。 看看 doc
关于php - Google 图书在 Codeigniter 中显示缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42974125/