[eluser]johnnytoobad[/eluser]
books.php Controller
Code:
<?php
class Books extends Controller {
function Books()
{
parent::Controller();
$this->load->library('amazon_api');
$this->load->helper('form');
$this->load->library('pagination');
}
function index()
{
$this->search_amazon();
}
function search_amazon($keywords = NULL, $offset = 0)
{
//Check if keywords have been submitted via the input
if($this->input->post('keywords')) {
$keywords = $this->input->post('keywords');
}
//If keywords have been entered
if ($keywords) {
//Convert CI pagination values to Amazon API ItemPage values
$page_number = ($offset/10)+1;
//Calls search_amazon method
$search_results = $this->amazon_api->search_amazon($keywords, $page_number);
//Configure and initialize pagination
$config = array (
'base_url' => base_url() . 'books/search_amazon/' . $keywords,
'total_rows' => $search_results->Items->TotalResults,
'uri_segment' => 4,
'per_page' => '10'
);
$this->pagination->initialize($config);
$data = array(
'search_results' => $search_results,
'title' => 'Results From Amazon Search',
'keywords' => $keywords,
'total_pages' => round($search_results->Items->TotalResults/10),
'page_number' => $page_number,
'pagination' => $this->pagination->create_links()
);
//No keywords have been enetred
} else {
$data = array(
'title' => 'Results From Amazon Search',
'error_message' => 'Please enter in a search term'
);
}
//Load the views and pass the data
$this->load->view('find-books', $data);
}
}
find-books.php (view)
Code:
<?php
//If page is displaying a search result, include that information
//at the top of the page. If not, then just display instructions
//for searching Amazon Database.
if (isset($keywords)) {
echo "<h2>Search Results For \"". $keywords ."\"</h2>";
echo "<p>Go back to the \"". anchor('books', 'Find Books')."\" page. Search again below.</p>";
}else if (isset($error_message)) {
echo "<h2>".$title."</h2>";
echo "<p>".$error_message.".</p>";
} else {
echo "<h2>".$title."</h2>";
echo "<p>In the search field below type the name of a <em>teacher</em>, <em>author</em>, or <em>book title</em>.</p>";
}
//Check if search keywords are present
if (!isset($keywords)) { $keywords = ''; }
$keyword_input = array(
'name' => 'keywords',
'id' => 'keywords',
'size' => 20,
'value' => set_value('keywords', $keywords )
);
$form_attributes = array(
'class' => 'search-form',
'id' => 'search-form',
'name' => 'search_terms'
);
// Set attributes for the form
echo form_open('books/search_amazon', $form_attributes);
// Set attributes for the input field
echo form_input($keyword_input);
// Set attributes for the submit field
echo form_submit('submit_button', 'Search Amazon!');
echo '</form>';
?>
<?php if (isset($search_results)): // Check to see if $search_results has been passed ?>
<p>Page: <?= $page_number ?> of <?= $total_pages ?></p>
<?= (isset($pagination)) ? $pagination: ''; ?>
<ul class="books">
<?php foreach($search_results->Items->Item as $book): //Loop through results ?>
<li>
Title: <a >DetailPageURL ?>" title=""><?=$book->ItemAttributes->Title //Echo title of book ?></a><br/>
Author: <i><?=$book->ItemAttributes->Author // Echo author of book ?></i><br/>
<img >MediumImage->URL //Link to Thumbnail ?>" alt="Cover of <?=$book->ItemAttributes->Title //Add title of book to alt attribute ?>" class="image-thumbnail" border="1" />
</li>
<?php endforeach; ?>
</ul>
<?= (isset($pagination)) ? $pagination: ''; ?>
<?php endif; ?>