0

Using PHP and MySQL I would like to retrieve a list of book covers based on their ISBN which I have in my database.

According to this answer, I can use the following URL;

https://www.googleapis.com/books/v1/volumes?q=isbn:0751538310

This works fine, and returns a JSON response. However I want to do this for 10 books, not just one - how do I achieve this?

My code so far;

Model

public function itemList() {
    $query = $this->db->get('item', 10);
    return $query->result_array();
}

A var_dump($this->items_model->itemList()) returns https://pastebin.com/nY5bFMmw

Controller

$page = file_get_contents("https://www.googleapis.com/books/v1/volumes?q=isbn:0751538310");
// I need to replace the above ISBN with the 10 from my database
$data = json_decode($page, true); 
echo "Image = " . '<img src="'.$data['items'][0]['volumeInfo']['imageLinks']['thumbnail'].'" alt="Cover">';
// This successfully prints 1 image. I need 10.

View

// What do I need to do in order to print 10 different images
<table>
    <tr>
        <td><strong>Image</strong></td>
    </tr>
        <tr>
            <td>// image here x 10</td>         
        </tr>
</table>

I think I need a foreach loop somewhere but I'm not really sure how or where?

Community
  • 1
  • 1
jonboy
  • 2,729
  • 6
  • 37
  • 77

1 Answers1

0

Edit: after adding your var_dump.

Simplest way would be something like this

$itemList = $this->items_model->itemList()

$data = [];
foreach ($itemList  as $book) {
    $data[] = json_decode(
        file_get_contents("https://www.googleapis.com/books/v1/volumes?q=isbn:{$book['item_isbn']}")
    );
}

And then you can loop ofer $data to generate your html.

Notice, there can be some special endpoint on google end to to this, but I had never used that api.

Vini
  • 627
  • 5
  • 18
  • It didnt look like that from your question. What is the result of `itemList()` then? – Vini Mar 28 '17 at 07:12
  • I edited original answer taking your data. But take into consideration that looping on arrays is like basic programming stuff in any language and you should learn those before doing anything. – Vini Mar 28 '17 at 08:23