CodeIgniter search form
I am currently learning CodeIgniter and I am looking to develop a simple example consisting of 2 f开发者_Go百科orms, let’s call them form a and form b. Form a has one edit field called “LastName” and form b will displays a list of all names in a table matching the value in “LastName” something like
select first_name, last_name from my_table where last_name = :LastName
I need this example to understand the mechanisms of passing variables from one form and controller to another. I am guessing this is using a method like $_POST but no examples on the web look very clear.
So you would have a form...
<form action="/controller/name/" method="post">
<p>Last Name: <input type="text" name="LastName" /></p>
<p><input type="submit" value="Submit"/></p>
</form>
Then in your controller (assuming your already connected to the database):
function index() {
// This is the last name from the form
$LastName = $this->input->post('LastName');
// Create the query
$sql = "SELECT * FROM users WHERE last_name = ?";
// Execute it, replacing the ? with the last name from the form
$query = $this->db->query($sql, array($LastName));
// Show results
foreach ($query->result() as $row) {
echo $row->first_name . "<br />";
echo $row->last_name;
}
}
Your view folder: application/view/form_a.php, application/view/forma_b.php
Your controller folder: application/controller/controller_name.php
Your model folder: application/model/related_model_name.php
Your controller_name.php file:
class Controller_name extends Controller
{
function index()
{
$this->load->view('form_a'); //this loads the form
}
function results()
{
$name= $this->post->input('last_name');
$this->load->model('related_model_name'); //this is the model to fetch the data
$data['names']= $this->related_model_name->searchByLastName($name);
if(!empty($data))
$this->load->view('form_b', $data);
}
}//eoc
Your related_model_name.php file
class Related_model_name extends Model
{
function __construct()
{
parent::Model();
}
function searchByLastName($name)
{
$query = $this->db->get_where('table_name', array('last_name'=>$name));
if($query->nu_rows() > 0)
return $query->results();
}//eof
}//eoc
Your form_b.php view file
do a print_r($data) and that should give you an idea of how to display the data. it maybe something like
foreach ($names as $name)
{
echo $name->name;
}
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.
This code assumes you are connected to your database and have a standard MVC CI application, etc.
I handle most of this task in the model and view, but I do have this method in my controller:
public function result()
{
$zipcode = $this->input->post('zip_code');
$query = $this->db->get_where('growers', array('zip LIKE' => $zipcode));
return $query->result_array();
}
In my model, I used the following method:
public function result()
{
$zipcode = $this->input->post('zip_code');
$query = $this->db->get_where('growers', array('zip LIKE' => $zipcode));
return $query->result_array();
}
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).
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:
<div class="search" style="margin-top:0px;">
<?php
$this->load->view('widgets/search');
?>
</div>
</div>
<div id="results">
<div id="grower-info">
<?php
$this->load->view('widgets/result');
?>
</div>
</div>
The search form widget is:
<form action="search-results" method="post">
<input type="text" maxlength="10" name="zip_code" value="zip code" size="10">
<input type="submit" name="submit" value="SEARCH">
</form>
The search result widget is:
<?php
$results = $this->pages_model->result();
foreach ($results as $result)
{
echo '<h4>'.$result['company'].'</h4>';
echo $result['address_1'] . ' ' . $result['address_2'].'<br>';
echo $result['city'].', ' . $result['state'] . ' ' . $result['zip'].'<br>';
echo 'Phone: ' . $result['office_phone'].'<br>';
echo 'Fax: ' . $result['office_fax'].'<br>';
echo 'Website: <a href="'.$result['website'].'" target="_blank">' . $result['website'].'</a><br>';
echo '<br>';
echo '<hr>';
}
if (count($results) < 1) {
echo 'No results found. Please try your search again, or try <a href="another-search">another search</a>.';
}
?>
I hope that helps someone!
精彩评论