Suppose there is a table FRUITS:
mysql> select * from FRUITS;
+---------+-----------+
| fruitid | fruitname |
+---------+-----------+
| 1 | Orange |
| 2 | Apple |
| 3 | Pear |
| 4 | Banana |
+---------+-----------+
4 rows in set (0.02 sec)
I have a view file that contains a form.
Here i have a list where user selects which food he wants, so in the case user selects "fruits" I get data from table FRUITS and fill a combo box....
After the user fills the form, some tables will be updated. During the process I need to consult data from FRUITS table and fill a combo box when user selects fruits in the list of food....
If I were not using Codeigniter I could use
<?php
$conn = mysql_connect('localhost','yourUsernameHere','yourPasswordHere');
mysql_select_db('testdb',$conn);
$query = "select fruitid,fruitname from FRUITS order by fruitname";
$result = mysql_query($query,$conn);
$selectbox='<select name=\'fruit\'>';
while ($row = mysql_fetch_assoc($result)) {
$selectbox.='<option value=\"' . $row['fruitid'] . '\">' . $row['fruitname'] . '</option>';
}
$selectbox.='</select>';
mysql_free_result($result);
echo $selectbox;
?>
to fill a combo box inside the form and then capture that value and send it to insert data to other tables...
How could I do this on Codeigniter, as the queries must be done in controller file...?
let us say I have
class Fruits extends CI_Model {
function __construct()
{
parent::__construct();
}
function what_fruits()
{
$this->db->select('fruitname');
$this->db->from('FRUITS');
$q = $this->db->get('');
if($q->num_rows() > 0)
{
$data = array();
foreach($q->result() as $row)
{
$data=$row;
}
return $data;
}
}
}
class main_form_view extends CI_Controller
{
function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('main_form_view');
}
public function result()
{
$this->load->model('Fruits');
$data['fruitname'] = $this->Fruits->what_fruits();
}
}
<html>
<head></head>
<body>
<form method="post">
<select name="food" id="food" onchange="fill_cb_food(this.value);" >
<option value="0">fruits</option>
<option value="1">other1</option>
<option value="2">other2</option>
</select>
//response of ajax call display here.???
<div class="selectbox" id="id_fruits">
<select name="fruits" id="fruits">
<option value="">--Choose fruit--</option>
</select>
</div>
</form>
</body>
</html>
function fill_cb_food(id)
{
$.ajax({
type: "POST",
url: "main_form_view/result.php,
data: "fruit=" + fruitname,
success: function(html){
$("#id_fruits").html(html);
}
});
}
So I want to get a combo box with
Orange
Apple
Pear
Banana
when user selects Fruits option in the form...
You can use ajax post to fetch data runtime and fill into your combo box.. like:
$.ajax({
url: 'http://url_to_fetch_fruit_table_data/',
type: "POST",
data:data,
success: function(response) {
$("select").html(response);
}
});
or you can also loop and fill using foraeach .
In the model you select any column using '*'.
You have to pass the data to the view-file:
class main_form_view extends CI_Controller
{
public function index()
{
$this->load->model('Fruits');
$data['fruits'] = $this->Fruits->what_fruits();
$this->load->view('main_form_view', $data);
}
}
then you can do in the view-file:
<?php foreach($fruits as $fruit): ?>
<option value="<?php print $fruit['fruitid'] ?>"><?php print $fruit['fruitname']</option>
<?php endforeach; ?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.