Populating Select list with PHP and SQL - php

I am calling from my SQL server a list for the options and outputting into an array.. see query:
SQL
$location = "SELECT location_id, location_name, location_postcode, location_buildingname
FROM dbo.system_locations";
$stmt = sqlsrv_query( $conn, $location );
PHP
<select class="form-control" name="location">
<?php while ($row = sqlsrv_fetch_array($stmt)){ ?>
<option>
<?php echo $row['location_name']?>
</option>
<?php } ?>
</select>
This works great, however I want the option which is selected within the database to be the selected option..
There is one factor, the select list can be added to by visiting settings section and adding new location otherwise I would use the ternary operator method.

try this
<select class="form-control" name="location">
<?php while ($row = sqlsrv_fetch_array($stmt)){ ?>
<option <?php ($row['selected'] == 1){echo 'selected';}?>>
<?php echo $row['location_name']?>
</option>
<?php }?>
</select>

Related

How do I fix the selected item in a drop down box

I have edit form where I get info from database
<select name="table">
<?php
//fetch all tables from database
$user = $con->query("SELECT * FROM table") or die(mysql_error());
while($row = $table->fetch_object()) { ?>
<option value="<?php echo $row->tablename;?>">
<?php echo $row->tablename; ?>
</option> <?php }?> </select>
<label for="time">Time :</label>
<select name="time">
<option value="twotothree">2PM-3PM</option>
<option value="threetofour">3PM-4PM</option>
<option value="fourtofive">4PM-5PM</option>
<option value="fivetosix">5PM-6PM</option>
</select>
The names of the tables from drop-down menu are different. Everytime I select a table and a time slot and save the data, the selection goes back to the first row of the menu. Eg I select table 3 and 4PM-5PM after saving it goes back to table 1 and 2PM-3PM. I need to be fixed on the last selection as I might use 4PM-5PM for table 4 also. Any idea? Thanks
You can add selected attribute when rendering your select list, depending on $_POST variable, when it's available. For example for you table select element:
<select name="table">
<?php
$user = $con->query("SELECT * FROM table") or die(mysql_error());
while($row = $table->fetch_object()) { ?>
<option value="<?php echo $row->tablename;?>" <?php if (isset($_POST['table']) && $_POST['table'] == $row->tablename) echo 'selected'; ?> >
<?php echo $row->tablename; ?>
</option>
<?php }?>
</select>
In similar way you can do for time select element.

How to display same records in multiple drop down form database using PHP?

I have two drops down and I am able to display the records in one dropdown from a database. I am getting the second drop down list bank.I have to display same records in my second drop down but it is not displaying. If I delete first drop down then records are displaying in second drop down.I haven't change any think just copy paste the code.Would you help me in this?
<?php
$sql_emails="SELECT * FROM request";
$result_emails = $conn->query($sql_emails);
//first drop down
?>
<select class="selectpicker" name="first" data-live-search="true">
<option style="color:#bbb;" value="" disabled selected>Select emails</option>
<?php while($row = mysqli_fetch_array($result_emails)) { ?>
<option value="<?php echo $row['Email'];?>"><?php echo $row['Email'];?></option>
<?php } ?>
</select>
<?php mysqli_data_seek( $result_emails, 0 ); ?>
<select class="selectpicker" name="second" data-live-search="true" id="learner-emails">
<option style="color:#bbb;" value="" disabled selected>Select emails</option>
<?php while($row = mysqli_fetch_array($result_emails)){ ?>
<option value="<?php echo $row['Email'];?>"><?php echo $row['Email'];?></option>
<?php } ?>
</select>
mysqli_data_seek( $result_emails, 0 ); use this before second dropdown
Put the rows into an array at the beginning of your script.
$rows = [];
while($row = mysqli_fetch_array($result_emails))
$rows[] = $row;
Then, on the html sections, loop over the array. You can loop over it as many times as you want. You can't do that with the query results.
<?php foreach($rows as $row):;?>
<option value="<?php echo $row['Email'];?>"><?php echo $row['Email'];?></option>
<?php endfor;?>
mysqli_fetch_array() moves the pointer forward each time you call it. You need mysqli_data_seek() to set the pointer back to the start and then call mysqli_fetch_array() again.

Passing PHP variable to HTML code

I am trying to pass the variable from my database to an HTML code but it isn't working. If I enter in a numeric value for my Select option it will work but it doesn't work with me trying to reference my class_id. The code I'm basing mine off of is found here. I am writing this code on wordpress also.
global $wpdb;
$result = $wpdb->get_results("SELECT course FROM class;");
echo "
<form>
<select name='department' onchange ='showUser(this.value)'>";
foreach($result as $results){
echo "<option selected value='$results->class_id'>$results->course</option>";
}
echo" </select>
</form>";
Try not to mix your HTML and PHP together, I've fixed a few things for you below. You need to select class_id in your query to use it as the option value, you can't use selected on all options as well. You may want to check if there are actually any results before looping over them too.
<?php
global $wpdb;
$results = $wpdb->get_results("SELECT class_id, course FROM class;");
?>
<form>
<select name="department" onchange="showUser(this.value)">
<?php foreach( $results as $result ): ?>
<option value="<?= $results->class_id ?>"><?= $results->course ?></option>
<?php endforeach ?>
</select>
</form>
global $wpdb;
$result = $wpdb->get_results("SELECT course FROM class;");
echo '<form><select name="department" onchange="showUser(this.value)">';
foreach($result as $results){
echo '<option selected value="'.$results->class_id.'" >'.$results->course.'</option>';
}
echo '</select></form>';
it is easy and simple for understanding use this code it's working prefect
<?php global $wpdb; $result = $wpdb->get_results("SELECT course FROM class;"); ?>
<form>
<select name='department' onchange ='showUser(this.value)'>
<?php foreach($result as $results){ ?>
<option selected value='<?php echo $results->class_id; ?>'><?php echo $results->course; ?> </option>
<?php } ?>
</select>
</form>

using php code within html to dynamically load a dropdown list

I am trying to use php code within html to grab content from a database to populate the values for a drop down menu (see below). When I run the php script on its own (test.php) I get all the expected values. When nested within html I get nothing but a single blank value under Select a Species. I would expect this to run through the while loop more than once as there are about 7 values returned and I would also expect the contents to include data derived from the table.
What I am attempting is possible correct??
Is it just an error with code (no errors popping up in the logs)
<label for="select">Species Observed : </label>
<select name="select" class="textfields" id="species">
<option id = "0">-- Select a Species -- </option>
<?php
$con = mysqli_connect("localhost","xxx","xxxx","databases");
$result = mysqli_query($con, "SELECT * FROM bc_species WHERE 1");
while ($row = $result->fetch_assoc()){
?>
<option><?php echo $row['species'];?></option>
<?php } ?>
</select>
<option><?php echo $row["species"]; ?></option>
<?php
$optionData = '<option id = "0">-- Select a Species -- </option>';
$con = mysqli_connect("localhost","xxx","xxxx","databases");
$result = mysqli_query($con, "SELECT * FROM bc_species WHERE 1");
while ($row = $result->fetch_assoc()){
$optionData .= "<option>".$row['species']."</option>" ;
}
?>
<label for="select">Species Observed : </label>
<select name="select" class="textfields" id="species">
<?php echo $optionData;?>
</select>
//Put your fetched data on an array then loop them like this
<?php
foreach ($list as $data => $item) {
?>
<option value="<?php echo $data?>">
<?php echo $data?>
</option>
<?php
}
?>
This should work:
<label for="select">Species Observed : </label>
<select name="select" class="textfields" id="species">
<option id = "0">-- Select a Species -- </option>
<?php
$con = mysqli_connect("localhost","xxx","xxxx","databases");
$result = mysqli_query($con, "SELECT * FROM bc_species");
while ($row = $result->fetch_assoc()){
echo '<option>'. $row["species"].'</option>
} ?>
</select>
You were not iterating through the values within the HTML <option> tags, so I moved those into the while loop. (notice the PHP section includes echoing those tags).
I also removed the unnecessary "SQL injection-looking" where 1 clause from your query.

Html Select Box Get value from mysql

Hi I have a stored value in mysql from a select box. When i call a edit page I would like the select box to populate with the value i have stored in mysql
$taskCompany = $mysqli->real_escape_string($_POST['taskCompany']); This returns 1
<select id="taskCompany" required name="taskCompany" class="form-control">
<option value="" disabled selected>Select a Company</option>
<option value="1">building</option>
<option value="2">maintenace</option>
</select></div>
I would like the select box to show the correct option when loading the edit page. I do not store any select option in the database values or text just the value that is select when job was created.
Thanks Jon
I'd I have understood correctly, you have he value/values in your MySQL database and you want them to be shown in your drop down list:
You could do it like:
<option value = "1" <?php if($taskCompany == 1) echo "select='selected'";?>>building</option>
Or if you have the names coming from the database too.
<select id="taskCompany" required name="taskCompany" class="form-control">
<option>SELECT</option>
<?php
$res = musql_query("SELECT * FROM yourtablename");
while($row = mysql_fetch_array($res))
{
$taskCompany = $row['taskCompany'];
$co_name = $row['taskCompanyName'];
?>
<option value = "<?php echo $taskCompany; ?>" ><?php echo co_name; ?></option>
<?php } ?>
Here I have used $co_name to be displayed as a name assuming that you have building, maintenance etc stored in your database.
This is for the time being, as I wanted to demonstrate but Please
consider mysqli/PDO as mysql_* is deprecated.
<?php $taskCompany = $mysqli->real_escape_string($_POST['taskCompany']); ?>
<select id="taskCompany" required name="taskCompany" class="form-control">
<option value="" disabled selected>Select a Company</option>
<option value="1" <?php if($taskCompany == 1) echo "selected='selected'"; ?>>building</option>
<option value="2" <?php if($taskCompany == 2) echo "selected='selected'"; ?>>maintenace</option>
</select></div>
You can do this
<?php foreach($taskCompany as $row){ ?>
<option value="unique id for select options">name for select options</option>
<?php } ?>

Categories