<?php
$get = mysqli_query($conn, "SELECT supplier.*,product.* FROM
supplier,product ORDER BY supplier.supplierid ASC");
$option = '';
while ($row = mysqli_fetch_array($get, MYSQLI_ASSOC)) {
$option .= '<option value = "' . $row['productname'] . '">' .
$row['suppliername'] . '</option>';
}
<label style="margin-left:25px;margin-bottom: 10px; ">Supplier:
</label><select style="margin-bottom:10px ;margin-left: 50px;width:
200px"> <?php echo $option; ?></select>
Hello Maam And Sir I have A problem About that data Output In a Select Box. The thing that happen when i use this code or use two multiple table, the data output also double but if 1 table it is working the way i want. But I don't want to have a dirty codes that why i to put in a single query.
Related
I have a trouble about select option value choosing with PHP
Multiple select box is showing for state and city. Jquery loads as ajax method for database. And I need to reach "option value" inside value. I will show the problem inside below codes.
<?php
function load_country() {
include 'includes/db.php';
$sql = "SELECT * FROM ilce ORDER BY name";
mysqli_set_charset($con, "UTF8");
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)) {
$output .= '<option name="' . $row['name'] .'" value="' .
$row["ilce_id"].'">' . $row["name"]. '</option>';
}
return $output;
}
?>
<select class="form-control" name="state" id="state">
<option>Choose State</option>
<?php
echo load_country();
?>
</select>
<?php
$state = mysqli_real_escape_string($con, $_POST['state']);
echo $state;
?>
<option value="5">New York</option>
$state is showing for me Example : "id" => 5
Thus, I want to reach "New York", inside value.
Please help me
Thanks
The text content of an <option> element is not posted to the server, only its value.
If you're inserting the selected state into a database (like in a user table), you might just insert the numeric state ID and join the two tables when you need to display the information.
Alternatively, you can fetch an array of all states indexed by their IDs and then reference that array:
while ($row = mysqli_fetch_array($result)) {
$allStates[$row['ilce_id']] = $row['name'];
}
$stateId = $_POST['state'];
$thisState = $allStates[$stateId];
echo $thisState;
Or just fetch the one row that you need by its ID, depending on what you need.
A less desirable solution is to set the <option> value to the row's name rather than its ID:
$output .= '<option value="' . $row["name"] . '">' . $row["name"] . '</option>';
But that seems to defeat the purpose of using a relational database.
Also see Get Text From Tag Using PHP.
I can't figure out why my drop down list, which is populated from Mysql, is giving me a number instead of the string value. Below is the code for the drop down list, which properly displays the item names. The problem is when I pull the selected item later I get a number instead of the string item name the user selected.
<select name="product1" class="form-control" id="sel1">
<?php
require('./PHPConnect.php');
$dropdown = array();
$downquery = "SELECT * FROM masterDESC";
$response = #mysqli_query($dbc, $downquery);
if($response){
while(($row = #mysqli_fetch_array($response,MYSQL_ASSOC))){
$dropdown[] = $row['DESCRIPTION'];
}
}
foreach($dropdown as $key => $value){
echo '<option value=' . $key . '>' . $value . '</option>';
}
echo "</select>";
mysqli_close($dbc);
?>
Here is the code later used to pull the selected item.
$product1 = $_POST['product1'];
Any thoughts?
It is spitting put the index number because you are retrieving an array which is just the description values (from this "$dropdown[] = $row['DESCRIPTION'];" you have returned from the DB and when there are no keys listed - the array index is the $key - therefore the number
you need to change the following
foreach($dropdown as $key => $value){
echo '<option value=' . $key . '>' . $value . '</option>';
}
to this and set the value of the option to the same as the displayed content (or leave it with the number and reference it later as $dropdown[index].
foreach($dropdown as $value){
echo '<option value="' . $value. '">' . $value . '</option>';
}
Never mind I got it figured out. It was the key value pair on the foreach. It was necessary for a similar drop down I created in the past but didn't work here. I just got rid of the foreach and it worked.
<select name="product1" class="form-control" id="sel1">
<?php
require('./PHPConnect.php');
//$dropdown = array();
$downquery = "SELECT *
FROM masterDESC";
$response = #mysqli_query($dbc, $downquery);
if($response){
while(($row = #mysqli_fetch_array($response,MYSQL_ASSOC))){
$dropdown = "<option value=\"{$row['DESCRIPTION']}\">
{$row['DESCRIPTION']}</option>\n";
echo $dropdown;
}
}
echo "</select>";
mysqli_close($dbc);
?>
I am trying to create a dropdown menu that will have as options certain fields recovered from a database using a for loop.
I am doing the following:
for ($article_id=1; $article_id <=30; $article_id++)
{
$sqltitles = "SELECT title FROM articles WHERE article_id='$article_id'";
$cursor = mysqli_query($db, $sqltitles) or die($sqltitles."<br/><br/>".mysql_error());
$row = mysqli_fetch_row($cursor);
$titles = $row[0];
echo $titles;
echo "</br>";
}
Which draws all the titles from the database and shows them one at a time.
Is there any way to make all those titles appear as options to a dropdown menu, so the user can select which one to read?
Something like this should work. I made a modification to how the query is working. If you specify article IDs for a range of articles in your query rather than just one specific ID, you should be able to execute only one query instead of one for each ID you want to retrieve. Regardless of whether or not you decide to use the approach I suggested, the syntax for creating the dropdown menu should be the same.
<select>
<?php
$sqltitles = "SELECT title FROM articles WHERE article_id BETWEEN 1 AND 30" ;
$cursor = mysqli_query($db, $sqltitles) or die($sqltitles."<br/><br/>".mysql_error());
while ($row = mysqli_fetch_row($cursor)) {
echo '<option value ="' . $row[0] . '">' . $row[0] . '</option>';
}
?>
</select>
Here is a good reference for how to create HTML <select> tags.
try something along the lines of...
echo '<select name="dropdownname">';
foreach ($titles as $key => $val) {
echo('<option value="' . $val .'">' . $val . '</option>');
}
echo '</select>';
I am facing difficulties while selecting values from drop down list based on the selected value from another drop down list that too retrieve from sql database
My PHP code is embedded with html, here is the code i am trying to do with select:
Country Name: <select name="status" style="width: 150px;">
<option value="select_country" selected>select</option>
<?php
$c_id="";
include 'dbconfig.php';
$sql = "select * from Country";
$result = sqlsrv_query($conn,$sql);
while($row = sqlsrv_fetch_array($result))
{
echo("<option value = '" . $row['Country'] . "'>" . $row['Country'] . " </option>");
$c_id=$row['CountryId'];
}
?>
</select> Create New Country
<br /><br />
State Name: <select name="status" style="width: 150px;" >
<option value="select_State" selected>select</option>
<?php
$s_id="";
$sql = "select * from State where CountryId = $c_id";
$result = sqlsrv_query($conn,$sql);
while($row = sqlsrv_fetch_array($result))
{
echo("<option value = '" . $row['State'] . "'>" . $row['State'] . " </option>");
$c_id=$row['State'];
}
?>
</select> Create New State
Please suggest me how to overcome this and thanks in advance..
You should use AJAX if you want the second select shown options based on the first option list picked value.
You have to use JAAX upon selection of first drop down list and replace the options of second drop down list.
I created a database with 3 tables being spusername, splocation, sprecord. spusername has id, splocation_id, lastname, firstname. I want to be able to have a drop down menu that has pulled id, lastname, firstname from the database, and within the pulldown it only shows a list of all the names being lastname,firstname. then once I select a person I have another drop down that has types of training in it. then when I hit submit it will generate a record in another table with the persons id and training record. so when I do a search it will pull up the user and the training records for that person.... I have already created a submit page in a .php that sends lastname, firstname, splocation_id for new users and I think I can create a search that does what I want it to, but I have never made a data entry doing a pulldown that has values generated from the database.
EDIT Code: With help from Vegard's coding I got this, and now it works great after a few trial and errors. Thank You!
Code:
<?php
if (isset($_REQUEST['Submit'])) {
$sql = "INSERT INTO $db_table(spusername_id,sptraining_id) values ('".mysql_real_escape_string(stripslashes($_REQUEST['spusername_id']))."','".mysql_real_escape_string(stripslashes($_REQUEST['sptraining_id']))."')";
if($result = mysql_query($sql ,$db)) {
echo '<h1>Thank you</h1>Your information has been entered into the database<br><br>';
} else {
echo "ERROR: ".mysql_error();
}
} else {
?>
<h1>Add Training Information To Database</h1><hr>
<br><br>
<form method="post" action="">
<select name="spusername_id">
<option value="default">Select Employee</option>
<?php
include("connectspusers.php"); /*file where you have stored your DB conn. settings*/
$result = mysql_query('SELECT id, lastname, firstname FROM spusername ORDER BY lastname ASC') or die (mysql_error());
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['id'] . ' ' . $row['lastname'] . ' ' . $row['firstname'] . '">' . $row['lastname'] . ', ' . $row['firstname'] . '</option>';
}
?>
</select>
<select name="sptraining_id">
<option value="default">Select Training</option>
<?php
include("connectsptraining.php"); /*file where you have stored your DB conn. settings*/
$result = mysql_query('SELECT id, trainingtype, level FROM sptraining ORDER BY level ASC') or die (mysql_error());
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['id'] . ' ' . $row['trainingtype'] . ' ' . $row['level'] . '">' . $row['trainingtype'] . ' - ' . $row['level'] . '</option>';
}
?>
</select>
<br><br>
<input type="submit" name="Submit" value="Submit">
</form>
<?php
}
?>
Something like this?
<select name="pulldown1">
<option value="default">Choose an option</option>
<?php
include("connect.php"); /*file where you have stored your DB conn. settings*/
$result = mysql_query('SELECT id, lastname, firstname FROM spusername ORDER BY firstname ASC') or die (mysql_error());
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . htmlentities($row['id'], ENT_QUOTES) . ' ' . htmlentities($row['lastname'], ENT_QUOTES) . ' ' . htmlentities($row['firstname'], ENT_QUOTES) . '">' . htmlentities($row['lastname'], ENT_QUOTES) . ', ' . htmlentities($row['firstname'], ENT_QUOTES) . '</option>';
}
?>
</select>
<select name="pulldown2">
<option value="default">Choose and option</option>
<?php
$result = mysql_query('SELECT traingtype FROM trainingtable ORDER BY trainingname ASC') or die (mysql_error());
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['trainingtype'] . '">' . $row['trainingtype'] . '" "' . $row['lastname'] . '</option>';
}
?>
</select>
This will result in two dropdown menus where the first dropdown lists the users last- and firstname separated by a comma+space and the second will list the different types of training. The ID filed is only sendt via the variable, but not displayed to the user.
When pulling the values from the variable in pulldown1, just use explode:
$userdetails = $_POST['pulldown1'];
$values = explode(" " $userdetails);
$ID = $values[0];
$lastname = $values[1];
$firstname = $values[2];
Haven't tested the code so it might need tweaking, and ofcourse you need to change the variable names corresponding to your actual db rownames.
Edit: In your code, you have to use $row and not $row2.
Secondly, instead of this:
<option value='{$id}'>{$lastname},{$firstname}</option>
use this:
<option value="' . $row['id'] . '">' . $row['lastname'] . ', ' . $row['firstname'] . '</option>
<select name="id" size="1">
<?php
$result=mysql_query("select * from spusername;");
while($user=mysql_fetch_array($result)) {
echo "<option value=\"".$user['id']."\">".$user['lastname'].", ".$user['firstname']."</option>";
?>
</select>
Go on with always using "id" as a reference to the user and try using post instead of get to send the request(keeps the URL in your user's browser clean).
You build a select in a loop with the data from your database.
example with mysql (did not test):
$query = "select id, lastname, firstname from spusername";
$result = mysql_query($query);
echo "<select>";
while($row = mysql_fetch_array($result)){
echo "<option value='".$row['id']."'>".$row['lastname']. " ". $row['firstname']."</option>";
}
echo "</select>";
EDIT: (response to your edit)
In your code you use $row2 instead of $row
Just an addendum to Vegard's solution:
Single quotes can be a bit tricky with surnames. It really depends on how you're storing the data in your database though.
If you have a surname O'Leary or O'Reilly you might get truncated results as you're building your select loop on the names. Give it a try.
You can fix this issue by using
htmlentities($row['lastname'], ENT_QUOTES) in your select loop