cMinor cMinor - 6 months ago 82
MySQL Question

get data from table and fill combo box depending on select value Codeigniter

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...?

update



let us say I have

model



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;
}
}
}


Controller



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();

}
}


views main_form_view



<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...

Answer

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.