I want to query a list of items that is a dropdown according to a previous drop down
How do I query a drop down according to anoth开发者_StackOverflow中文版er queries result
so I have a function getMakes()
that returns a list of vehicle makes that i have placed in a dropdown,
I created a new query in the function getModels()
that gets all the possible models for every vehicle make, i want to make a where statement that only grabs the makes for the vehicle make that was selected
I was thinking to have the function getModels($make)
where $make is the value we want in the where clause, but how do i make the value in the where clause dynamic?
You will need to use ajax or javascript if you want to have the second dropdown populate based on a selection from the first dropdown. You can't have PHP do the work in this case; PHP will run once when the page loads and that's it.
You can have your main controller:
function index(){
$l1=$this->db->query('SELECT make FROM cars');
$l1arr = Array();
foreach($l1 as $key=>$value){
$l1arr[$key]=$value;
}
$data['list1']=$l1arr;
$this->load->view('welcome_message',$data);
}
then another controller that returns the models: [assuming you pass the make in the url]
function car_models(){
$make = $this->uri->segment(2);//the make passed in URL e.g. site.com/getcars/honda
$models=$this->db->query('SELECT models FROM cars WHERE make="',$make,'"');
$marr = array();
foreach($marr as $key=>$value){
$marr[$key]=>$value;
}
return form_dropdown('models_dropdown',$marr);
}
then have an ajax function call the models page based on the initial selection; you can do it with jQuery like so:
function getModels(make){
jQuery.ajax({
type: 'GET',
url: '/getcars/'+make,
success: function(data){
//populate dropdown with models
jQuery('#models_dropdown').html(data);
}
});
return false;
}
not sure I got all the details correct, but this should at least point you in the right direction.
Couldn't you make the $sql
string use some string concatenation to produce the SQL query? There should be some checking to prevent a SQL injection attack as doing this can be a little risky from a security perspective.
PHP string concatenation would be a tutorial if you want some more help with that.
精彩评论