Welcome Guest, Not a member yet? Register   Sign In
Updated: Amazon Services API Library
#2

[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 '&lt;/form&gt;';
?&gt;

&lt;?php if (isset($search_results)): // Check to see if $search_results has been passed ?&gt;
<p>Page: &lt;?= $page_number ?&gt; of &lt;?= $total_pages ?&gt;</p>
&lt;?= (isset($pagination)) ? $pagination: ''; ?&gt;
    <ul class="books">
        &lt;?php foreach($search_results->Items->Item as $book): //Loop through results ?&gt;
            <li>
                Title: <a >DetailPageURL ?&gt;" title="">&lt;?=$book->ItemAttributes->Title //Echo title of book ?&gt;</a><br/>
                Author: <i>&lt;?=$book->ItemAttributes->Author // Echo author of book ?&gt;</i><br/>
                <img >MediumImage->URL //Link to Thumbnail ?&gt;" alt="Cover of &lt;?=$book->ItemAttributes->Title //Add title of book to alt attribute ?&gt;" class="image-thumbnail" border="1" />
            </li>
        &lt;?php endforeach; ?&gt;
    </ul>
&lt;?= (isset($pagination)) ? $pagination: ''; ?&gt;
&lt;?php endif; ?&gt;


Messages In This Thread
Updated: Amazon Services API Library - by El Forum - 02-07-2010, 09:56 PM
Updated: Amazon Services API Library - by El Forum - 02-07-2010, 09:58 PM
Updated: Amazon Services API Library - by El Forum - 03-24-2010, 09:23 PM



Theme © iAndrew 2016 - Forum software by © MyBB