I want to do is have a dropdown box that will display the MealOption and when that is selected somehow find that products meal id?
This is my database layout
<?php
public function get_data()
{
$mysqli= new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME) or die("Couldn't connect".mysqli_connect_error());
$sql="select id,name from table";
$result=$mysqli->query($sql) or die($mysqli->error);
while ($row=$result->fetch_array(MYSQLI_ASSOC))
{
echo "<option value=\"".$row["id"]."\" selected>".$row["name"]."</option>";
}
}
?>
<select name="abc" id="xyz">
<?php get_data(); ?>
</select>
In the function connect the db and then run the query. in the while loop echo your rows like above so you can get your name and id from table. In the end in your html call this function.
Related
I used the below code to get the datas or options in my dropdown list from mysql db. also it works fine, but my problem is when i select a particular option from this dropdown list, on submission no value or empty value is saving for that for field. simply i can see the option name, but value is seems like this
value=" ";
actually, value is what i see as option name.
<strong> Select Data </strong>
<select name="data1">
<option value=""> NONE </option>
<?php
//Mysql db connection
$con = mysqli_connect("localhost", "my_user", "my_password", "my_db");
//Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Perform queries
$rs = mysqli_query($con, "SELECT DISTINCT relation FROM relation_names");
if ($rs && mysqli_num_rows($rs)) {
while ($rd = mysqli_fetch_object($rs)) {
echo("<option value='$rd->id'>$rd->relation</option>");
}
}
mysqli_close($con);
?>
</select>
add id in query too
$rs = mysqli_query($con,"SELECT DISTINCT relation,id FROM relation_names");
Then only
$rd->id
will populate correct value
If you want relation as value then do like below:
echo("<option value='$rd->relation'>$rd->relation</option>");
I have a MySQL database and am trying make a conditional dropdown menu ("Subcategory") show values based on the values in the first dropdown, ("Category").
This is a reference data table, so the parent ID of the subcategory should match the ref_data_id of the category.
The conditional list relies on the value of the first dropdown box, and I have tried $_POST and $_GET to try to get the value from the first object to use in my MySQLi query but neither seems to work.
Can anyone help?
<?php
// connect to the database
include("connectdb.php");
?>
<html>
<!--First Dropdown Menu - CATEGORIES-->
<div class="label">Select Category:</div>
<select name ="Category_HTML">
<option value = "">---Select---</option>
<?php
$stmt = "SELECT * FROM `ref_data` WHERE Parent_ID IS NULL ;";
$result = mysqli_query($mysqli, $stmt);
while ( $row=mysqli_fetch_array($result)) {
$description = $row['Description'];
$refdataID = $row['Ref_Data_ID'];
echo "<option value='$refdataID'> $description </option>";
}
?>
</select>
<!--Second Dropdown Menu - Subcategory-->
<div class="label">Select Subategory:</div>
<select name="subcategory_HTML">
<option value = "">---Select---</option>
<?php
$idvalue = $_POST['Category_HTML'];
$stmt = "SELECT * FROM `ref_data` WHERE Parent_ID = $idvalue;";
$result = mysqli_query($mysqli, $stmt);
while ( $row=mysqli_fetch_array($result)) {
$description = $row['Description'];
$refdataID = $row['Ref_Data_ID'];
echo "<option value='$refdataID'> $description </option>";
}
?>
</select>
</html>
try to make an AJAX call when a value is select in the category list i.e
<select name ="Category_HTML" onchange="AJAX_CALL()">
and populate the result in the
<select name="subcategory_HTML">
I have table called empicons that has two columns: EmpNo, IconId
I have the EmpNo stored as a session called $EmpNo
I have another table called icons that has 3 columns: Id, Name, URL
Currently I have a recordset that selects the icons table Id and Name and presents it in a Multiple Select box that then updates the empicons table.
Recordset query: SELECT id, name FROM gbl_icons ORDER BY id ASC
Multiple Select Form Element:
<select name="icons[]" size="10" multiple="multiple">
<?php
do {
?>
<option value="<?php echo $row_icons['id']?>"><?php echo $row_icons['name']?></option>
<?php
} while ($row_icons = mysql_fetch_assoc($icons));
$rows = mysql_num_rows($icons);
if($rows > 0) {
mysql_data_seek($icons, 0);
$row_icons = mysql_fetch_assoc($icons);
}
?>
</select>
I'm trying to do a join with empicons and icons table so that the Multiple Select highlights the data elements found in empicons based on the EmpNo variable. I cannot get the join to work correctly and then update the form element to highlight these.
Try this. It uses mysqli_ functions. If you're having a problem with JOINing the two tables, then we need to work on your query.
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT id, name FROM gbl_icons ORDER BY id ASC";
echo '<select name="icons[]" size="10" multiple="multiple">';
if ($result = mysqli_query($link, $query)) {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
echo '<option value="' . $row[id] . '">' . $row[name] . '</option>';
}
/* free result set */
mysqli_free_result($result);
}
echo '</select>';
/* close connection */
mysqli_close($link);
?>
Your query should be like this
SELECT i.id, i.name, e.EmpNo FROM gbl_icons i
LEFT JOIN empicons e ON e.IconId = i.id AND e.EmpNo = '$EmpNo'
ORDER BY i.id ASC
So you will get all icons from your 'gbl_icons' table and for each one of them a corresponding record in empicons for the current EmpNo. If there is no corresponding record you will get NULL in the 'EmpNo' column.
And your code should be like that
<select name="icons[]" size="10" multiple="multiple">
<?php
while($row_icons = mysqli_fetch_assoc($icons)) {
?>
<option value="<?=$row_icons['id']?>" <?=!empty($row_icons['EmpNo']) ? ' selected="selected"' : ''?><?=$row_icons['name']?></option>
<?php
}
?>
</select>
I have two tables in MySQL db namely vendor and menu.
vendor table
vendor_id(Primary) vendor_name
menu table
menu_id(Primary) menu_details cost vendor_id(Foreign)
I need to create a combo box which fetches vendor_name instances from the MySQL db dynamically. On selection of vendor_name from the combo box, menu table(HTML and table should be in editable mode) should be displayed from the MySQL database. I am new to MySQL and PHP. So please help me to deal with this criteria.
Below is the code that I'm working on:
<html><body>
<select name="vendor" id="vendor">
<option value="Choose">Choose</option>
<?php $conn = mysql_connect('localhost','root','');
if (!$con) { die('Could not connect: ' . Mysql_error()); }
mysql_select_db('project',$conn);
$query = "select vendor_id,vendor_name from vendor";
$result = mysql_query($query,$conn);
while($fetch= mysql_fetch_array($query)){
echo "<option>".$row[1]."</option>";
}?>
</select>
</body></html>
If I understood You correctly then this should do the thing that you're after. It displays the combobox from vendor table and if you change the selection, it reloads the page and shows the menu that corresponds to the vendor_id in the menu table.
<html>
<body>
<?php
// connecting to the server
$conn = mysql_connect('localhost','root','') or die('Could not connect: ' . Mysql_error());
// selecting the database
mysql_select_db('project',$conn) or die("db error: ".mysql_error());
// is menu_id set?
if (isset($_GET['menu']) && intval($_GET['menu'])>0) {
// if yes, then query the menu items
$query = "select menu_id, menu_details, cost, vendor_id from menu where vendor_id=".intval($_GET['menu']);
$result = mysql_query($query, $conn) or die("SQL error: ".mysql_error());
while ($row = mysql_fetch_assoc($result)) {
// print the menu items
print_r($row);
}
}
// select the vendor combobox
$query = "select vendor_id,vendor_name from vendor";
$result = mysql_query($query,$conn);
// If the vendor selection has changed then reload the page
?>
<select name="vendor" id="vendor" onchange="document.location='index.php?menu='+this.options[this.selectedIndex].value;">
<option value="Choose">Choose</option>
<?php
// loop through the results
while($fetch= mysql_fetch_assoc($query)){
echo "<option value=\"".$row['vendor_id']."\">".$row['vendor_name']."</option>";
}
?>
</select>
</body>
</html>
the assumption is that the file is named index.php. (look at the select onchange="" filename. You'd need to change it if You have a different filename.
I've got a second select box which is created using Javascript when the user selects a value from the first select box. The JS runs a PHP file which queries a MYSQL database for the relevent items depending on the first selection.
The problem I'm having is that the value for the item the user selects in the second box is not passed in the header when the form is submitted.
Does anyone have any ideas how to get the value and pass it in the header?
Thanks.
This is the PHP for the second select box:
<?php
$cxn = mysqli_connect("localhost", "root", "root", "db") or die("Couldn't connect");
$choice = $_GET['choice'];
$query = "SELECT ID, Model FROM models WHERE MakeID ='$choice'";
$result = mysqli_query($cxn, $query) or die("Couldn't execute query");
echo "
<select id='model' name='model'>
<option value='0'>Model (any)</option>";
while ($row = $result->fetch_object()) {
echo "
<option value='$row->ID'>$row->Model</option> \n
";
}
echo "
</select> ";
?>
After creating select field you must append it to your form.
<script>document.getElementById("MyForm").appendChild("select_field_id");</script>