Linking two php dynamic dropdown lists - php

I'm in need of a bit of help >_<. I have two tables:
The first table which populated with car manufacturers called list_vehicle_marks with the columns
id | value
The second table, populated with car models called list_vehicle_models and has the following columns;
id | value | mark_id
The column mark_id has the same value as id in the first table, in other words, Ford has the id no. 37 and all Ford models have the mark_id as 37 too.
How would I create two dynamic dropdown menus, where the second menu shows only the models based on the manufacturer selected in the first menu?
This is what I have done so far:
<?php
connection to host, db, blah blah
if ($db->connect_error) {
echo "Failed to connect to MySQL: (" . $db->connect_error . ") "
. $db->connect_error;
} else {
$sql = "SELECT value FROM list_vehicle_marks";
$result_db = $db->query($sql);
if (!$result_db) {
echo $db->error . ' Error perform query!';
} else {
echo '<select name="value">';
echo '<option value="">Select...</option>';
while ($row = $result_db->fetch_object()) {
echo '<option value="' . $row->value . '">';
echo $row->value;
echo '</option>';
}
echo '</select>';
}
}
$db->close();
?>

In the code you posted, the drop down that you create, it should be something like this
echo '<option value="' . $row->id. '">';
echo $row->value;
echo '</option>';
Notice the $row->id instead of $row->value
To Populate the corresponding dropdown you have two options:
Once a user selects a value from the marks drop down you trigger a PHP function which gets the values of the corresponding vehicle models and re-render the page (You capture the id selected by the user using javascript)
You fetch all the vehicle models along with vehicle marks (add another query to your above code that fetches all the models), keep the result of the models query in a JavaScript variable and generate the second dropdown using javascript once a user makes a selection. Here too you capture the users selection and call a javascript function that spits out a corresponding id drop down.
The second method would not be a good option if your models table has a lot of data.

Related

PHP/MySQL - how to complete dropdown selection from list

I'm cobbling together a dropdown list that I would like to display the '[ve_venue]", "[ve_town]' from a MySQL query and, upon selection set a variable that I can use to pass the ve_ID on to an update query that adds a venue ID number to a separate table as a lookup key.
So far I've got some code that I've pieced together from various places and I can get it to display the town in a dropdown - I just need to add the venue field to the dropdown so I get "venue, town" in the list and I also need to be able to pass the ve_ID to a variable, say, $ve_ID so I can call it in some separate code (that will be on the same page in a separate include).
Here's what I've got so far....
<?
include('login_admin_sucess.php');
// Connects to your Database
include '../connect_include.php';
$query = "SELECT ve_ID, ve_venue, ve_town FROM `co_venues` ORDER BY ve_ID ";
$result = mysql_query($query);
while($r=mysql_fetch_array($result))
{
$ve_venue = $r['ve_venue'];
$result_array[$ve_venue][] = $r['ve_town'];
}
echo "<select name='x'>";
foreach($result_array as $v_venue => $value)
{
foreach($value as $title) {
echo "<option value='" . $v_venue . "'>" . $title . "</option>";
}
}
echo "</select>";
?>
I realise that mysql_ is deprecated...I'll work on that once I've got everything functioning.
You can concat the id and venue. For example:
$ve_venue = $r['ve_venue'] . "|" . $r['ve_ID'];
When you use the variable make a split with charapter "|" with preg_split function.

Creating drop down menu using php, sql connected to database table

I'm creating currently database with tables and trying to do it using second normalization form for my school.
I have list of institutional partners which has to be displayed in two tables.
I got this far that I have created database tables:
The first one called "as_partners_id" which contain columns: "Partner_ID" (this is primary key INT) and "Partner" (just a VARCHAR).
The other table called "as_partners" has some more columns, like: "Partner_ID, Country, EU terms, NON-EU terms, More info".
What I want to do is create drop down menu above first table from which you can choose ID from e.g. 1 to 30. After picking it will display the second table with more information about this particular ID. But I actually don't know how to do it properly. Here is the code that I have so far:
<?php
// Create connection
$con=mysqli_connect("localhost","easj_admin","","easj");
// Check connection
if (mysqli_connect_errno())
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
echo "We are connected.";
}
$result = mysqli_query($con,"SELECT Partner_ID, Partner FROM as_partners_id");
?>
BODY PART:
<table>
<tr>
<?php
// Get field information for all fields
while ($fieldinfo=mysqli_fetch_field($result))
{
echo "<th>" . $fieldinfo->name ."</th>";
}
echo "</tr>";
// <form>
// <input typ="select">
// </form>
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Partner_ID'] . "</td><td>" . $row['Partner'] . "</td>" ;
echo "</tr>";
}
?>
</table>
How can I add to my code this drop down menu that will display the second table called as_partners?
Please help.
<form name="myform" method="get">
<select name='whatever' onclick="if(this.value != ''){ myform.submit(); }">
<?
while($row = mysqli_fetch_array($result))
{
echo "<option value='" . $row['Partner_ID'] . "'>" . $row['Partner'] . "</option>";
}
?>
</select>
</form>
That will get you the drop down. Then from there you can submit the form using post or get and use the submitted id to generate the second table.
Then you'll need to add php code similar to this:
if(isset($_GET['whatever']))
{
[build 2nd table here]
}

PHP Dropdown of all records from database + select current set record

I have a PHP dropdown of a list of groupnames (together with id, so it can be updated). In this FORM page you can change the groupname specified for an item by choosing possibilities from the dropdown coming out from the database. My code below works, but there must be a better way, because I get the first field as the currently set, and then all the possibilities, so I get this record twice.
Example:
- Keyboard (Currently set)
- Speakers (Possible to choose, straight from DBS)
- Midi Controllers (Possible to choose, straight from DBS)
- Keyboard (Possible to choose, straight from DBS)
- Drum set (Possible to choose, straight from DBS)
As you see I get the currently set record again.
My code:
echo "<select name='itemgroupid'>";
// CHOOSE CURRENT SET RECORD AS SELECTED ITEM
echo "<option value='" . $itemgroupid . "'>";
$selected="
SELECT item.itemid, itemgroup.itemgroupname, itemgroup.itemgroupid
FROM item, itemgroup
WHERE item.itemid=$itemid";
$selectedresult=mysql_query($query) or die("query fout " . mysql_error() );
while($record=mysql_fetch_array($selectedresult) ) {
echo "" . $itemgroupname . "</option>";
}
// QUERY TO SHOW ALL POSSIBLE CHOOSABLE RECORDS FROM DATABASE
$itemgroupquery="SELECT itemgroupname,itemgroupid FROM itemgroup";
$itemgroupqueryresult = mysql_query ($itemgroupquery);
while($nt=mysql_fetch_array($itemgroupqueryresult)){
echo "<option value=$nt[itemgroupid]>$nt[itemgroupname]</option>";
}
echo "</select>";
There are 2 ways to achieve what you're looking for:
1) To show the selected item at the top of the dropdown
echo "<select name='itemgroupid'>";
// CHOOSE CURRENT SET RECORD AS SELECTED ITEM
echo "<option value='" . $itemgroupid . "'>";
$selected="
SELECT item.itemid, itemgroup.itemgroupname, itemgroup.itemgroupid
FROM item, itemgroup
WHERE item.itemid=$itemid";
$selectedresult=mysql_query($query) or die("query fout " . mysql_error() );
while($record=mysql_fetch_array($selectedresult) ) {
echo "" . $itemgroupname . "</option>";
}
// QUERY TO SHOW ALL POSSIBLE CHOOSABLE RECORDS FROM DATABASE
$itemgroupquery="SELECT itemgroupname,itemgroupid FROM itemgroup WHERE item.itemid != $itemid";
$itemgroupqueryresult = mysql_query ($itemgroupquery);
while($nt=mysql_fetch_array($itemgroupqueryresult)){
echo "<option value=$nt[itemgroupid]>$nt[itemgroupname]</option>";
}
echo "</select>";
2) Show the selected item in it natural place
echo "<select name='itemgroupid'>";
// QUERY TO SHOW ALL POSSIBLE CHOOSABLE RECORDS FROM DATABASE
$itemgroupquery="SELECT itemgroupname,itemgroupid FROM itemgroup";
$itemgroupqueryresult = mysql_query ($itemgroupquery);
while($nt=mysql_fetch_array($itemgroupqueryresult)){
echo "<option value=$nt[itemgroupid]";
if( $itemid == $nt['itemgroupid'] ) echo ' selected="selected"';
echo ">$nt[itemgroupname]</option>";
}
echo "</select>";
HTH
OK
In your code.
rather than output your selected value at the top, do it the proper way :)
Select your current item (make a note of the itemgroupid for example)
then in your output
while($nt=mysql_fetch_array($itemgroupqueryresult)){
echo "<option ";
if ($savedid==$nt[itemgroupid]) echo "selected ";
echo "$nt[itemgroupid]>$nt[itemgroupname]</option>";
}
echo "</select>";
This will produce with $savedid=1
<option value=0>group 0</option>
<option selected value=1>group 1</option>
<option value=2>group 2</option>
Add the default selected record to a empty array first like
toDisplay = array('selected_record');
Then, get the data from the database using your sql and append it to this array.
Later run a array_unique on it and finally using a loop create the html output string, in the same way you are doing it now.

How to generate a state/province dropdown that selects the value in the database

So I have several places for country and state/province dropdowns. What I'd like to do is have a function for each of these and when I run my while loop on customer data from mysql I want to select the right by default from the data in the database.
Reason is right now the dropdowns default to the HTML selected one so if users don't change it to theirs again when they edit their account they re-save their info with the wrong state/country.
I'd also like to use this in several places so I want a big array of countries and states/provinces that loop.
Just looking for a hand or be pointed to where this has been done in a function already.
Thank you kindly.
$states - query with all states, $user_state - current user's state. So your user's choice will be selected by default.
<?php
echo '<select name="state">';
while ($state = mysql_fetch_assoc($states))
{
echo '<option value="' . $state['state'] . '"';
if ($user_state == $state['state'])
{
echo ' selected';
}
echo '>' . $state . '</option>';
}
echo '</select>';

PHP MySQL Drop Down Box Populate Selected Value

I have read tutorials about how to populate an entire Drop down list with MySQL, but the problem I am running into is that I only want to grab the one from the database and have that be the selected one. So I would have like a drop down with three items (Item1, Item2, Item3) in the database its stored in a column called itemschoice which has a value of 'Item2'. How do I go about getting item2 to be selected when I load the drop down box?
In your <option> element add the selected attribute for the value that is in itemschoice.
Crude example using a made up function to get the choice:
$choice = get_items_choice();
$results = mysqli_query($sql);
echo '<select name="whatever">';
while($row = mysqli_fetch_array($results)) {
if ($row['choice'] === $choice) {
echo '<option value="' . $choice . '" selected="selected" />';
} else {
echo '<option value="' . $choice . '" />';
}
}
echo '</select>';
This is just an example, don't copy & paste this without adding some kind of error verification!

Categories