首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >谷歌图书在代码点火器中显示缩略图

谷歌图书在代码点火器中显示缩略图
EN

Stack Overflow用户
提问于 2017-03-23 11:00:12
回答 1查看 381关注 0票数 1

使用Codeigniter 3,我想显示一个HTML表,其中包括一些书籍细节,即项目ID、项目图像和项目标题。我想通过谷歌图书填充图片项目。

到目前为止,我的代码可以工作,因为我可以从MySQL数据库中检索所有数据,并将其显示在一个简单的HTML中。但是,我不知道如何用Google中的相应图像填充HTML表中的图像字段。

我正在从我的数据库中检索ISBN,我能用它来查找Google吗?

正如您所看到的,我试图创建一个foreach,但是它不起作用,所以我已经将它注释掉了。我在foreach中收到的错误消息是;

消息:未定义索引:第23行中的项

我目前的代码如下;

模型

代码语言:javascript
复制
class Items_model extends CI_Model {
    public function itemList() {
        $query = $this->db->get('item', 10);
        return $query->result_array();
    }  
}

控制器

代码语言:javascript
复制
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);
    }
}

视图

代码语言:javascript
复制
<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>

任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-23 17:29:18

您需要使用Googlebooks来获取您的书的id。

首先,您必须以这样的方式提出请求:

代码语言:javascript
复制
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“格式。看看文档

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42974125

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档