populate multipe selectboxes from same table - php

I would like to populate three selectboxes with the same items (from one database table).
This is the code I have so far. I want to avoid three queries. Would you help me with a solution to populate multiple selectboxes?
<select name="resort_3" class="swcomp" data-index="3">
<?php
$today = date("Y-m-d");
$sQuery2 = "SELECT DISTINCT res_id, resort from sv_snow WHERE lud='$today' ORDER by resort";
$sResult2 = mysql_query($sQuery2) or die('Error, query 2 failed');
while($ro = mysql_fetch_assoc($sResult2))
{
echo '<option value="'. $ro['res_id'] . '">'. ucfirst($ro['resort']) . '</option> ';
}
?>
</select>

Save the options in a string and then assign those to all 3 select boxes. Simple as that
<?php
while($ro = mysql_fetch_assoc($sResult2))
{
$options.='<option value="'. $ro['res_id'] . '">'. ucfirst($ro['resort']) . '</option> ';
}
?>
<select name="resort_1" class="swcomp" data-index="1">
<?php echo $options; ?>
</select>
<select name="resort_2" class="swcomp" data-index="2">
<?php echo $options; ?>
</select>
<select name="resort_3" class="swcomp" data-index="3">
<?php echo $options; ?>
</select>

Related

Duple select from database in same table but diffent proposes

First option of select must be the name referring to the ID. The remaining select options are the remaining names
<select class="input" name="client_id">
<?php
$sel_client_detail="Select * from client WHERE client_id=".$id."";
$result_detail = mysqli_query($con,$sel_client_detail);
while($new_record_row = mysqli_fetch_assoc($result_detail)) { ?>
<option selected><?php echo $row['nome'];?></option>
<?php };?>
<?php
$sel_client="Select * from client";
$result = mysqli_query($con,$sel_client);
?>
<option>-----------</option>
<?php while($new_record_row = mysqli_fetch_assoc($result)) { ?>
<option><?php echo $new_record_row['nome'];?></option>
<?php };?>
</select>
Output:
<select>
<option selected> Izzi (current ID name)</option>
<option> ____________</option>
<option> Other existing clients</option>
<option> Other existing clients</option>
<option> Other existing clients</option>
<option> Other existing clients</option>
</select>
If you want the user to be first in your option list just run the query once and build the HTML parts in 2 seperate strings. Then once the loop is complete put them together and echo them
<?php
echo '<select class="input" name="client_id">';
$itsme = '';
$others = '<option>-----------</option>';
$sql = "Select * from client";
$result = $con->query($sql);
while($row = $result->fetch_assoc()){
if ( $id == $row['id'] ) {
$itsme = "<option selected='selected'>$new_record_row[nome]</option>";
} else {
$others += "<option>$new_record_row[nome]</option>";
}
}
// put the option tags together in the order you specified
echo $itsme . $others . '</select>';
Here's a different, but more conventional, approach to this common scenario:
Why not just make the chosen ID selected when you get to it in the list? Then it will still show to the user first. It's more efficient than having two separate queries.
Like this:
<select class="input" name="client_id">
<?php
$sel_client="Select * from client";
$result = mysqli_query($con,$sel_client);
?>
<option>-----------</option>
<?php while($new_record_row = mysqli_fetch_assoc($result)) { ?>
<option <?php echo ($new_record_row["client_id"] == $id ? "selected": ""); ?> ><?php echo $new_record_row['nome'];?></option>
<?php };?>
</select>

How to Use Dynamic Dropdown Selected value in Php

Php/Html:
Here I add The all Countries list to add/edit page.But Edit page Want Value Selected in dropdown.please help
<select class="form-control" id="country_list" name="country">
<?php
global $wpdb;
$network_lists = $wpdb->get_results("SELECT * FROM media_countries");
foreach($network_lists as $network_list){
?>
<option value="<?php echo $network_list->country_name ?>" selected="<?php
echo $network_list->country_name ?>"><?php echo $network_list>country_name ?></option>
<?php }?>
</select>
This is how I would do it. This will check the value against the value the user selects with the value from the database and properly populate your dropdown menu.
global $wpdb;
$network_lists = $wpdb->get_results("SELECT * FROM media_countries");
echo
'<select class="form-control" id="country_list" name="country">';
foreach($network_lists as $network_list) {
if($_POST['country'] == $network_list->country_name) {
echo '<option value="' . $network_list->country_name . '"' . ' selected="selected"' . '>' . $network_list->country_name . '</option>';
}else {
echo '<option value="' . $network_list->country_name . '">' . $network_list->country_name . '</option>';
}
}
echo
'</select>';
Selected value is in $_POST['country'];
Try:
<select class="form-control" id="country_list" name="country">
<?php
global $wpdb;
$network_lists = $wpdb->get_results("SELECT * FROM media_countries");
foreach($network_lists as $network_list){
?>
<option value="<?php echo $network_list->country_name; ?>" <?php echo ( $_POST['country_name'] == $network_list->country_name ? 'selected' : '' ); ?>><?php echo $network_list>country_name; ?></option>
<?php }?>
</select>
The above code automatically selects the option that has value equals to the submitted $_POST['country'] value.

code for fetching value to select option

I have select option. this option has multiple value from database. I want to update something from database, this value i want to update is exist on the select option I have.
this is my option code
$id = $_GET['update'];
$query = mysql_query("SELECT * FROM transaction where id = '$id'") or die ("could not search");
$count = mysql_num_rows($query);
while ($rows = mysql_fetch_array($query)) {
$id = $rows['id'];
$tranid = $rows['tranid'];
$trandate = $rows['trandate'];
$patientid = $rows['patientid'];
$transactiontype = $rows['transactiontype'];
$trandescription = $rows['trandescription'];
$tranquantity = $rows['tranquantity'];
$tranunitprice = $rows['tranunitprice'];
$tranamount =$rows['tranamount'];
$gettrandescription = $rows['trandescription'];
}
}
if (isset($_POST['selectmedicine'])) {
$gettrandescription=$_POST['medicineid'];
}
if (isset($_POST['selectroomquantity'])) {
$tranquantity=$_POST['quantity'];
}
?>
<script type="text/javascript">
$('#collapseone').collapseone({
toggle: true
});
<option value="<?php echo $trandescription; ?>" <?php if($trandescription==$gettrandescription){ echo "selected";} ?> ><?php echo $gettrandescription; ?></option>
<option value="<?php echo $tranquantity; ?>" <?php if($tranquantity==$tranquantity){ echo "selected";} ?> ><?php echo $tranquantity; ?></option>
this has value results, but i cant fetch this value to my existing select option.
If you want to "make that variable an array" as aldrin27 said, append [] to the name attribute of the select tag. The selected value of the option with name selectroomquantity will be available in your script as $_POST["selectroomquantity"] (this is the varible).
<select multiple name="selectroomquantity[]">
<option value="...">...</option>
</select>
It should only be necessary if multiple options can be selected however.
Also, there seems to be a typo:
<?php if($tranquantity==$tranquantity)
That check will always return true. It should probably be:
<?php if($tranquantity==$gettranquantity)
hi all i just got the code on how to fecth the value to dropdown. actually i made a wrong word. pre-selected is the right one, sorry for that. here;s the working code.
<select name="selectmedicine" class="form-control col-sm-4" id="medicinename">
<option id="0" style="width:100px"></option>
<?php
$medicine = mysql_query("SELECT * FROM medicine");
while ($row = mysql_fetch_array($medicine)) {
echo '<option id="' . $row['medicinename'] . '"';
echo ' value="' . $row['medicineid'] . '"';
if($row['medicinename'] == $trandescription) {
echo ' selected="selected"';
}
echo '>';
echo $row['medicinename'];
echo '</option>';
}
?>
</select>
thanks everyone, whos trying to help me on this. actually this is my five revised question sorry for that. and finally i got the right one.

How to display/get the column 2 value from database not display in select option?

I have table query which is displayed thru select option. The column 1 value is displayed on it. When I select the row 1 value from column 1, I also want the value on column 2 automatically displayed, how would I code that?
Example : medicinename(column1) = medicineprice(column2)
The medicinename is in select option. The medicineprice is not. Just want to display automatically the price every time I select the medicinename.
<div class="col-md-3"><label><h5>Medicine Name : </h5>
<select name="selectmedicine" class="form-control col-sm-4" id="medicinename" style="width:200px">
<option id="0" style="width:100px"> Select here...</option>
<?php
require_once 'config.php';
$medicine = mysql_query("SELECT * FROM medicine");
while ($displaymedicine = mysql_fetch_array($medicine)) {
$medicineid = $displaymedicine['id'];
$medicinename = $displaymedicine['medicinename'];
$medicineprice = $displaymedicine['medicineprice'];
?>
<option id="<?php echo $displaymedicine['medicineid']; ?>"><?php if($displaymedicine ['medicineid'] == $displaymedicine ['medicinename']) echo 'selected="selected"'; ?><?php echo $displaymedicine ['medicinename'] ?></option>
<?php
}
?>
</select>
Do you mean you want to be returned the price of the medicine to your PHP script when you select the name of the medicine? In which case you need to add a value="" attribute to the <option tag. It is the contents of value="" that is returned to the script i.e. if this option was selected from your <select name="selectmedicine">
<option value="999"...>Penicillin</option>
Then PHP would receive $_POST['selectmedicine'] which would contain 999
<div class="col-md-3"><label><h5>Medicine Name : </h5>
<select name="selectmedicine" class="form-control col-sm-4" id="medicinename" style="width:200px">
<option id="0" style="width:100px"> Select here...</option>
<?php
require_once 'config.php';
$medicine = mysql_query("SELECT * FROM medicine");
while ($row = mysql_fetch_array($medicine)) {
echo '<option id="' . $row['medicineid'] . '"';
echo ' value="' . $row['medicineprice'] . '"';
if($row['medicineid'] == $row['medicinename']) {
echo ' selected="selected"';
}
echo '>';
echo $row['medicinename'];
echo '</option>'
}
?>
</select>
Of course a better solution would be to put the unique ID of the medicine table into the value="" attribute. Then when the form is posted to your PHP script you read your database for that one row and you have all the data related to that medicine available to the script.
So that would be
<div class="col-md-3"><label><h5>Medicine Name : </h5>
<select name="selectmedicine" class="form-control col-sm-4" id="medicinename" style="width:200px">
<option id="0" style="width:100px"> Select here...</option>
<?php
require_once 'config.php';
$medicine = mysql_query("SELECT * FROM medicine");
while ($row = mysql_fetch_array($medicine)) {
echo '<option id="' . $row['medicineid'] . '"';
> changed code line
echo ' value="' . $row['id'] . '"';
> changed code line
if($row['medicineid'] == $row['medicinename']) {
echo ' selected="selected"';
}
echo '>';
echo $row['medicinename'];
echo '</option>'
}
?>

Populate html <select> with array data from mysql in PHP

I can see the query returning results, but I can't seem to be able to put them into a html dropdown box. Also, the dropdown box has just as many entries as the query returns, but THEY ARE ALL WHITE SPACES. HOWEVER, the page source shows correct option values such as
<option value="3 John"></option>
<option value="Jude"></option>
<option value="Revelation"></option>
Can somebody help me out? Why dont they actually show in the dropdown box?
<html>
<?php
//Connect to the database
$mysqli = new mysqli("localhost", "root", "", "bible");
//Return an error if we have a connection issue
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
//Query the database for the results we want
$query = $mysqli->query("select distinct bname as Name from kjv limit 1");
//Create an array of objects for each returned row
while($array[] = $query->fetch_object());
array_pop($array);
//Print out the array results
print_r($array);
?>
<h3>Dropdown Demo Starts Here</h3>
<select name="the_name">
<?php foreach($array as $option) : ?>
<option value="<?php echo $option->Name; ?>"></option>
</select>
<?php endforeach; ?>
Try This
<select name="the_name">
<?php foreach($array as $option) : ?>
<option value="<?php echo $option['Name']; ?>"><?php echo $option['Name']; ?></option>
<?php endforeach; ?>
</select>
After the query is executed use the while loop to add the options to select
$query = $mysqli->query("select distinct bname as Name from kjv limit 1"); ?>
<select>
<?php while($option = $query->fetch_object()){ ?>
<option><?php echo $option->Name; ?></option>
<?php } ?>
</select>
Not sure what the array_pop is doing in the code
AS TIM WAX SAID THIS IS THE SOLUTION
$query = $mysqli->query("select distinct bname as Name from kjv limit 1"); ?>
<select>
<?php while($option = $query->fetch_object()){ ?>
<option><?php echo $option->Name; ?></option>
<?php } ?>
</select>
<select name="the_name">
<?php foreach($array as $option) : ?>
<option value="<?php echo $option->Name; ?>"></option>
<?php endforeach; ?>
</select>
You ended your loop in a way that it also create <select> tag again and again. Change it and try again. I don't know much about .php but it could be a problem in showing your dropdown box.
here is mine .. im a beginner but it works for me,
$query = $mysqli->query("SELECT * FROM `student_type_db`"); //table of student type
echo "<select>";
while($row = $query->fetch_array()){
echo "<option>";
echo $row['student_type'] . " - " . $row['student_description'];
echo "</option>";
}
echo "</select>";
// student type = 1 | student description = regular
// output : 1 - regular

Categories