Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I realize this thread is old, but I am new to CodeIgniter and have been working with a similar challenge. My challenge is to create a search form that finds growers in a specific zip code. Here is my solution. It's simpler than I expected and might help someone else. </p> <p>This code assumes you are connected to your database and have a standard MVC CI application, etc. </p> <p>I handle most of this task in the model and view, but I do have this method in my controller: </p> <pre><code>public function result() { $zipcode = $this-&gt;input-&gt;post('zip_code'); $query = $this-&gt;db-&gt;get_where('growers', array('zip LIKE' =&gt; $zipcode)); return $query-&gt;result_array(); } </code></pre> <p>In my model, I used the following method: </p> <pre><code>public function result() { $zipcode = $this-&gt;input-&gt;post('zip_code'); $query = $this-&gt;db-&gt;get_where('growers', array('zip LIKE' =&gt; $zipcode)); return $query-&gt;result_array(); } </code></pre> <p>I have three views -- one page (located in views/pages/search.php), and two widgets --one for the search form and one for the search results (located in views/widgets/result). </p> <p>I have the search result form on the same page that the results display. However, each section is contained in its own view file, which I have placed in views/widgets. The code for this section in the page view is:</p> <pre><code> &lt;div class="search" style="margin-top:0px;"&gt; &lt;?php $this-&gt;load-&gt;view('widgets/search'); ?&gt; &lt;/div&gt; &lt;/div&gt; &lt;div id="results"&gt; &lt;div id="grower-info"&gt; &lt;?php $this-&gt;load-&gt;view('widgets/result'); ?&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>The search form widget is:</p> <pre><code>&lt;form action="search-results" method="post"&gt; &lt;input type="text" maxlength="10" name="zip_code" value="zip code" size="10"&gt; &lt;input type="submit" name="submit" value="SEARCH"&gt; &lt;/form&gt; </code></pre> <p>The search result widget is: </p> <pre><code>&lt;?php $results = $this-&gt;pages_model-&gt;result(); foreach ($results as $result) { echo '&lt;h4&gt;'.$result['company'].'&lt;/h4&gt;'; echo $result['address_1'] . ' ' . $result['address_2'].'&lt;br&gt;'; echo $result['city'].', ' . $result['state'] . ' ' . $result['zip'].'&lt;br&gt;'; echo 'Phone: ' . $result['office_phone'].'&lt;br&gt;'; echo 'Fax: ' . $result['office_fax'].'&lt;br&gt;'; echo 'Website: &lt;a href="'.$result['website'].'" target="_blank"&gt;' . $result['website'].'&lt;/a&gt;&lt;br&gt;'; echo '&lt;br&gt;'; echo '&lt;hr&gt;'; } if (count($results) &lt; 1) { echo 'No results found. Please try your search again, or try &lt;a href="another-search"&gt;another search&lt;/a&gt;.'; } ?&gt; </code></pre> <p>I hope that helps someone!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload