【发布时间】:2017-08-15 21:27:25
【问题描述】:
使用 Codeigniter 3,我想显示一个 HTML 表格,其中包含一些书籍详细信息,即 Item ID、Item Image 和 Item Title。我想通过 Google 图书填充商品图片。
到目前为止,我的代码可以正常工作,因为我可以从 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>
感谢任何帮助。
【问题讨论】:
标签: php mysql codeigniter oop google-books