2017-03-23 91 views
1

我正在使用Google APIs Client Library for PHP,我目前正在使用Codeigniter框架。当我在API代码中手动输入ISBN编号时,我可以显示书名和图像。适用于PHP的Google API库循环

表中的每个结果都应显示来自Google图书的相应标题和图片。

任何帮助,将不胜感激与此。此外,如果我的代码可以改进,我很乐意听到,我仍然在学习:)

我目前的工作代码如下;

控制器

class Items extends CI_Controller { 

    public function index() { 
     $data['items'] = $this->items_model->itemList(); 
// var_dump($data['items']) returns the following (http://pastebin.com/4XVS4Whb) 

     $client = new Google_Client(); 
     $client->setApplicationName("Client_Library_Examples"); 
     $client->setDeveloperKey("MyKeyHere"); 
     $service = new Google_Service_Books($client); 
     $isbn = '0-7515-3831-0'; // <- I need to replace this with my DB array 
     $results = $service->volumes->listVolumes($isbn); 

     foreach ($results as $item) { 
// var_dump($results) returns the following (http://pastebin.com/by50uLNq) 
// var_dump($item) returns the following (http://pastebin.com/b4vdt38P) 

      $bookTitle = $item['volumeInfo']['title']; 
      $bookImage = $item['volumeInfo']['imageLinks']['smallThumbnail']; 
      $data['bookTitle'] = $bookTitle; 
      $data['bookImage'] = $bookImage; 
     } 
     $this->load->view('item_view', $data); 
    } 
} 

型号

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

查看

<table> 
     <tr> 
      <td><strong>Title</strong></td> 
      <td><strong>Image</strong></td> 
     </tr> 
     <tr> 
      <td><?php echo $bookTitle ?></td>   
      <td><?php echo echo '<img src="'.$bookImage.'">';?></td>  
     </tr> 
</table> 
+0

能否请您提供这个'的var_dump输出($数据[”项目']);''和''var_dump($ results);'从你的控制器。我将能够提供并回答。 –

回答

2

既然你已经有了收藏数据,你可以简单地把它传递到视图和迭代的观点本身,因为:

... 
$service = new Google_Service_Books($client); 
$isbn = '0-7515-3831-0'; 
$results = $service->volumes->listVolumes($isbn); 
//pass in the $results to you view 
$this->load->view('item_view', $results); 

,并在视图

foreach ($results as $item) : ?> 
    <td><?php echo $item['volumeInfo']['title']; ?></td> 
    .... 
<?php endforeach; ?>