使用Codeigniter 3,我想显示一个HTML表,其中包括一些书籍细节,即项目ID、项目图像和项目标题。我想通过谷歌图书填充图片项目。
到目前为止,我的代码可以工作,因为我可以从MySQL数据库中检索所有数据,并将其显示在一个简单的HTML中。但是,我不知道如何用Google中的相应图像填充HTML表中的图像字段。
我正在从我的数据库中检索ISBN,我能用它来查找Google吗?
正如您所看到的,我试图创建一个foreach,但是它不起作用,所以我已经将它注释掉了。我在foreach中收到的错误消息是;
消息:未定义索引:第23行中的项
我目前的代码如下;
模型
class Items_model extends CI_Model {
public function itemList() {
$query = $this->db->get('item', 10);
return $query->result_array();
}
}控制器
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>任何帮助都是非常感谢的。
发布于 2017-03-23 17:29:18
您需要使用Googlebooks来获取您的书的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“格式。看看文档
https://stackoverflow.com/questions/42974125
复制相似问题