How To create a Dependable Dropdown From Two Tables - php

I have two tables "oc_t_item_car_make_attr" , "oc_t_item_car_model_attr"
So how can i make a Dependable Dropdown From these Two Tables?

You can only do this by Ajax.
Following is city country reference site, you can get idea from that.
Referance Site
if you stuck any where feel free to ask me.

Actually didn't understand question very well, but you probably want something like this:
SELECT * FROM oc_t_item_car_make_attr
INNER JOIN oc_t_item_car_model_attr
ON oc_t_item_car_model_attr.fk_i_make_id = oc_t_item_car_make_attr.pk_i_id
WHERE oc_t_item_car_make_attr.pk_i_id = ?
You need to insert id in the place of question mark.

Here is the entire code what your looking for:
Below code will display all car names from database dynamically:
<select name="cars" id="carmodels">
<option value="">select</option>
<?php
$qry = mysqli_query("select * from oc_t_item_car_make_attr");
while($row=mysqli_fetch_array($qry)){
echo "<option value=".$row['pk_i_id'].">".$row['s_name']."</option>";
}
?>
</select>
Below result will display car models dependent on the names selected above:
html:
<select name="models" id="models">
</select>
you need to use ajax to get the car models from database:
$(document).on('change','#carmodels',function(){
var value = $(this).val();
$.ajax({
url: "demo.php",
type: "GET",
data: { id : value},
success: function(data){
$("#models").html(data);
}
});
demo.php:
<?php
include "connection.php"; //database connection code
$qry = mysql_query("SELECT * FROM oc_t_item_car_make_attr
INNER JOIN oc_t_item_car_model_attr
ON oc_t_item_car_model_attr.pk_i_id = oc_t_item_car_make_attr.pk_i_id
WHERE oc_t_item_car_make_attr.pk_i_id =".$_GET['id'].");
while($row=mysqli_fetch_array($qry)){
echo "<option value=".$row['pk_i_id'].">".$row['s_name']."</option>";
}
?>

Hey you can do this by ajax
Reference Link 1
Reference Link 2
also study inner join

Related

How to populate a select tag from values based on another select tag with codeigniter

Please forgive me if I make any mistakes in english, its not really my best language. I will edit this post if I make errors please let me know.
I'm trying to make a php page with Codeigniter with a select tag whose values are from a database, and am trying to populate a second select tag based from the value placed on the former.
My college_subj database have three columns.
CollCode, SC, and Subj.
Basically, the College Code(CollCode) and Subject Code(SC) have combinations. A College Code X can have A,B,C SC, and College Code Y can have B,C,D,E SC. I am trying to make those SC appear on my second select tag when the first select tag, the CollCode, has a value.
The page's function is accept values from the two select tags and insert it into the database.
Here's the select tags on my edit.php:
<form method="post" action="<?php echo base_url();>index.php/Controller/insertfunction" id="crq">
<h3>Select College Code:</h3>
<select id="codecrq" name="code">
<option value="" selected="selected">---Select College Code---</option>
<?php foreach ($code as $row4): ?>
<option label="<?php echo $row4['Code']; ?>" value="<?php echo $row4['Code']; ?>" <?php echo set_select('code', $row4['Code'], False); ?>> <?php echo $row4['Code'] ; ?> </option>
<?php endforeach; ?>
</select>
<h3>Select SC:</h3>
<select id="sccrq" name="sc">
<option value="" selected="selected">---Select SC---</option>
</select>
</form>
Here's how I get the values that I placed in the Code select tag which is in the Model:
public function Code() {
$this->db->distinct();
$this->db->select('college_subj.CollCode');
$this->db->from('college_subj');
$query = $this->db->get();
return $query->result_array();
}
Here's a jquery that I try to use to populate the SC select tag:
$("#codecrq").change(function(){
var selectedMark = $("code").val();
if(selectedMark !== ""){
$.ajax({
type: "GET",
url: "Controller/sccrq/" + selectedMark,
success: function(data){
$("#sccrq").html("");
$("#sccrq").append("<option value=''></option>");
$.each(data, function(){
$("#sccrq").append("<option value='" + this.sc + "'>" + this.sc + "</option>");
});
}
});
}
});
and here's the sccrq code from my controller, which is supposed to get the SCs based from the College Code combination and pass it on the SC select tag:
function sccrq($code){
$this->db->distinct();
$this->db->select('college_subj.sc');
$this->db->from('college_subj');
$this->db->where($code);
$query = $this->db->get()->result_array();
return $query;
}
I try making it run, but nothing comes out from the SC select tag.
Any help will be deeply appreciated!
Thank you for you're time!
You can try
function sccrq($code){
$query = $this->db->query("SELECT SC FROM college_subj WHERE code = '".$code."'");
return $query;
}

How can I best put a drop down list or select tag inside a html table - And then update a row if changed to mysql? complicated

I have a database containing a name and a status column. It contains data displayed in a table (obvious!). So, I want users to be able to select the status column's data and change it to any value listed in the drop down list. After the selection, they need to click a button that will update the selected row to the mySQL database.
How can I achieve this with PHP scripting and HTML?
Here is my code for displaying the data in a table on the website: (Pay no attention to phpReportGenerator.php- its only drawing the columns as per sql table)
<?php
include_once('includes/phpReportGenerator.php');
$prg = new phpReportGenerator();
$prg->width = "100%";
$prg->cellpad = "10";
$prg->cellspace = "0.5";
$prg->border = "1";
$prg->header_color = "#307D7E";
$prg->header_textcolor="#FFFFFF";
$prg->body_alignment = "left";
$prg->body_color = "#C6DEFF";
$prg->body_textcolor = "#000000";
$prg->surrounded = '1';
//$prg->font_name = "Boishakhi";
mysql_connect("localhost","username","password");
mysql_select_db("my_database");
$res = mysql_query("select * from table");
$prg->mysql_resource = $res;
//$prg->title = "Test Table";
$prg->generateReport();
?>
OR
Can somebody show me a easier/more effective way to do this?
Here is the sample code that you can use to update your mysql database by sending request to the server to access process_mysql.php :
<select onchange="update_mysql();">
<option value="1">option 1 </option>
<option value="2">option 2 </option>
</select>
Jquery function with ajax post request:
<script>
update_mysql(){
var id = $('select option:selected').val();
$.ajax({
type:"post",
url:"process_mysql.php",
data:"id="+id,
success:function(data){
alert('Successfully updated mysql database');
}
});
}
</script>

jquery php code to bind state and city from mysql database table

I have a form where in one table row a dropdown state field is there and in another tr another dropdown is there for selecting city, I need that before submitting the form when user selects a state from dropdown list automatically the city dropdown should fetch and display only those city from the selected state.
I don't want to use AJAX but want to do it with jquery.
Backend database has two tables one is state with two fields id and state, second is city table having 3 fields id, state and city where state is my foreign key.
How can I do it with jquery any help regarding this will be appreciated since I am a newbie, please help
If you don't want to use ajax you can always preload the full list of states with cities into a javascript object and call it from there.
Example:
<select id="stateDrop">
<option value="state1">State 1</option>
<option value="state2">State 2</option>
</select>
<select id="cityDrop"></select>
<script>
var cities = {'state1':['city_one', 'city_two', 'city_three'], 'state2':['city_four', 'city_five', 'city_six']};
$('#stateDrop').on('change', function(){
var cityList = cities[$(this).val()];
console.log(cityList);
var output = '';
$(cityList).each(function(k, v){
output += '<option value="'+v+'">'+v+'</option>';
});
$('#cityDrop').html(output);
});
</script>
Fiddle
If you want to avoid using an ajax call, you will need to build a mapping from state-id's to lists of cities. Then bind a function to the change event of the state select-element, and populate the city select-element with the values.
Assuming State ID 0 has the cities A and B, with city id's 0 and 1:
var map = [[{id:0,name:"A"},{id:1,name:"B"}]]
$("#states").on("change",function() {
var cities = map[$(this).val()];
$("#cities option").remove(); //remove previous options
$.each(cities, function(i,e) {
$("#cities").append('<option value="'+e.id+'">'+e.name+'</option>');
});
});
Example: http://jsfiddle.net/HQLQU/
The php/mysql part for building the JS-map could be something like this (untested!):
$query = "SELECT city_id, city_name, state_id FROM cities";
$result = mysql_query($query); //or preferably use a more modern extension
$cities = array();
foreach (mysql_fetch_assoc($result) as $row) {
$cities[$row['state_id']][] = array("id"=>$row['city_id'],"name"=>$row['city_name']);
}
echo "var map = " . json_encode($cities);

Populating a second select box depending of the first select box option

I'm having this select, let's call it "X", that is populated with car brands as options from the database via a SELECT.
<select name="X">
<?php
$row = mysqli_query("SELECT * FROM brands");
while($row2 = mysqli_fetch_array($row))
echo '<option value="' . $row2['brandID'] . '">' . $row2['brandName'] . '</option>
?>
</select>
Now i have to populate the second select, called "Y", with the models specifics to a brand selected.
For example, if the option that's selected in the first select box (X) is Audi i should have as options in the second select (Y) the following: A4,A6,TT,TTs
To populate the second select box manually is easy, basicaly the same thing as for the first just with a different SQL request.
$row = mysqli_query("SELECT modelName from modele WHERE brandName = '$brand'");
Where $brand would have a value according to the first select's selection.
Thanks
Put an id in your <select name="X"> code like <select name="X" id ="X">
Put another select like <select name="Y" id="Y">. Which will be blank.
put this jquery in your page.
$("X").on("change",function(){
var x_value=$("X").val();
$.ajax({
url:'ajax.php',
data:{brand:x_value},
type: 'post',
success : function(resp){
$("#Y").html(resp);
},
error : function(resp){}
});
});
in your ajax.php add the query.
<?php
$row = mysqli_query("SELECT modelName from modele WHERE brandName =".$_POST['brand']);
while($row2 = mysqli_fetch_array($row))
echo '<option value="' . $row2['modelId'] . '">' . $row2['modelName'] . '</option>
?>
Hopefully it will work. Please tell me if you need anything.
Use ajax and on change event together,
method:
1- build a page where you post your queries to.
2- that page should react to the query and sends a respond with the desired list of options.
example :
if ($_POST["vehicle"] == car) // send response with array of car names as json obj for example
else // send response with array of motorbikes names
your ajax should receive it and populate the filed on-complete.
If you need more details, I'll be happy to provide it

onchange select do different mysql query

As title says I need help with onchange. I have select tag,and I need to do different mysql query when I choose something from select list. Example:
<select name="list">
<option>cars</option>
<option>busses</option>
<option>trucks</option>
</select>
and then when i select cars it do this
$query="select * from table where type='cars'";
and if I choose trucks it do
$query="select * from table where type='trucks'";
and so on...
then I need to display the result in div under the list
example
<select name="list">
<option>cars</option>
<option>busses</option>
<option>trucks</option>
</select>
<div> this is where I need to display results from query</div>
Please help!!!
If you want to change the result by ajax :
HTML :
<select id="list" name="list">
<option>cars</option>
<option>busses</option>
<option>trucks</option>
</select>
in JS File:
$(document).ready(function() {
$('#list').change(function() {
var selected=$(this).val();
$.get("change_query.php?selected="+selected, function(data){
$('.result').html(data);
});
});
});
and you should create change_query.php file and your query and code in it and return the result in it
$type = $_POST["selected"];
$query="select * from table where type='".$type."'";
print result here .... ;
Tell me if you need any help ,I just guided you in Jquery not all code
You should add your SELECT code to a form with method attribute set to post,
and also a submit button of course.
Then , php will get the value of those INPUTS and do whatever you want.
$term = $_POST['list'];
/*
In order to prevent unwanted queries or injection add an array with those terms and
check if the posted value is in this array:
*/
$terms = array("cars" , "trucks");
if(!array_key_exists($term , $terms))
die(); //bad bad bad boy.
$query = "select * from table where type='$term'";
you could use
onchange="this.form.submit();"
as well.

Categories