2017-03-23 3 views
1

현재 Codeigniter 프레임 워크에서 작업중인 Google APIs Client Library for PHP을 사용하고 있습니다. 수동으로 ISBN 번호를 API 코드에 입력하면 책 제목과 이미지를 표시 할 수 있습니다.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

에 : 데이터, 당신은 단순히 뷰에 전달하고 뷰 자체에 반복 할 수 items ']);'와이'var_dump ($ results);를 컨트롤러에서 가져와야합니다. 나는 그 때 제공하고 응답 할 수있을 것이다. –

답변

2

이미 컬렉션이 있기 때문에 '[당신이이`위해서 var_dump의 출력 ($ 데이터를 제공하시기 바랍니다 수

... 
$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; ?>