Repitition of selected item in dropdownlist in PHP - php

I have this problem that the selected value in my dropdownlist is showing twice. What should i do so that it will show only once.
<td>
<select class="span8" style="width:100%" name="from_time[]" id="from_time<?php echo $count;?>" value="start_from" onchange="calculate_by_ajax(this.id);">
<option value="<?php
$start_from=$jobs_result['start_from'];
//$date_arr_to= explode(" ", $start_from);
//$date_to= $date_arr_to[0];
//$time_to= $date_arr_to[1];
$sec="SELECT DATE_FORMAT('$start_from', '%H:%i') as tp";
$sec_exe=mysql_query($sec);
$sec_res=mysql_fetch_array($sec_exe);
?>" selected="selected">
<?php echo $sec_res['tp']; ?></option>
<?php include("list.php"); ?>
</select>
</td>

Got it, instead of modifying one of the available options and marking it as selected, you actually add another option with your code.
You could change list.php to look like this (i.e. an array of all the times)
<?php
$drop_down_values = array('11:35', '11:36', '11:27');
?>
Then your code should look like this:
<?php
include("list.php");
$start_from=$jobs_result['start_from'];
$sec="SELECT DATE_FORMAT('$start_from', '%H:%i') as tp";
$sec_exe=mysql_query($sec);
$sec_res=mysql_fetch_array($sec_exe);
?>
<td>
<select class="span8" style="width:100%" name="from_time[]" id="from_time<?php echo $count;?>" value="start_from" onchange="calculate_by_ajax(this.id);">
<?php foreach ($drop_down_values as $value) {
if ($value == $sec_rec) {
echo "<option selected=\"selected\">$value</option>";
} else {
echo "<option>$value</option>";
}
}
?>
</select>
</td>

Related

Dropdowns not populating correctly with PHP foreach loop

I am currently populating a table with rows from a database. One of the columns is a dropdown that needs to be populated with multiple values.
Each dropdown is automatically defaulting to Bowling Green even if that row should not be Bowling Green. Most rows are either Southeast or Michigan but regardless of what it should be, it is defaulting to Bowling Green for some reason.
How can I have the dropdowns keep all values as options in the dropdown (bowling green, michigan, southeast), but have the dropdown value default to the value that it is in the database?
<?php
$sql = "SELECT TOP 100 *
FROM Table_OS_List
ORDER BY [CURRENT_SKU] ASC";
$drops = "SELECT [Purchasing_Group]
FROM Table_OS_List
GROUP BY [Purchasing_Group]";
$drop = $dbh->query($drops);
$allDrops = $drop->fetchAll();
?>
<?php
/* Foreach loop that brings in information to populate table */
foreach ($dbh->query($sql) as $rows) {
?>
<tr class="row">
<td class="old_sku" id="old_sku"><?php echo intval ($rows['OLD_SKU'])?></td>
<td class="current_sku" id="current_sku"><?php echo intval ($rows['CURRENT_SKU'])?></td>
<td class="id" id="id" style="display: none;"><?php echo intval ($rows['ID'])?></td>
<td class="dropdown-select" id="purchgroup">
<select id="selected_group" class="selected_group" disabled>
<?php
foreach($allDrops as $dropdown) { ?>
<option class="choice"
value="<?php echo $dropdown['Purchasing_Group'];?>">
<?php echo $dropdown['Purchasing_Group'];?>
</option>
<?php } ?>
</select>
</td>
<td><input type="button" class="edit" name="edit" value="Edit"></td>
<td><input type="button" class="delete" name="delete" id="<?php echo intval ($rows['ID'])?>" value="Delete"></td>
</tr>
<?php } ?>
Example of what it looks like now. You can see that it has multiple options which is what I need, but, regardless of it's value in the database, is defaulting to Bowling Green:
You need to add the selected attribute to the correct option.
Something like this:
<option class="choice"
<?php echo $row['Purchasing_Group'] == $dropdown['Purchasing_Group'] ? "selected" : ""; ?>
value="<?php echo $dropdown['Purchasing_Group'];?>">
<?php echo $dropdown['Purchasing_Group'];?>
</option>
Where $row['Purchasing_Group'] is the column in the Table_OS_List table.
The important code here is <?php echo $row['Purchasing_Group'] == $dropdown['Purchasing_Group'] ? "selected" : ""; ?> - this is known as a Ternary, it could also be written in expanded form like this:
<?php
if($row['Purchasing_Group'] == $dropdown['Purchasing_Group']) {
echo "selected";
}
?>
You can add a selected attribute with a inline ternary operator just like so;
<option class="choice" <?php echo $dropdown['Purchasing_Group'] ? ' selected' : null ?> value="<?php echo $dropdown['Purchasing_Group'];?>">
<?php echo $dropdown['Purchasing_Group'];?>
</option>
and after you fetching all the datas from the DB, always good to check if result or results existing.

Selected as Selected Database value Codeigniter

In user edit module have a dropdown in which i show options from database.
Now i select a value from dropdown and want to set it as selected.
SO next time when i edit user i can see which was the last value selected.
When i edit a user i select some value from dropdown, and then save the record back to database. Next time when i open that record i want the option i selected last time to be shown selected.
I tried this :
<tr>
<td>Base INI File</td>
<?php
if(isset($_GET['id']))
{
$id=$_GET['id'];
btn_edit_file($id);
}
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<td>
<select required name="base_ini_id" id="base_ini_id" class="form-control">
<option value="">Select</option>
<?php foreach($base as $value) { ?>
<option id="emp" class="specialLink" value="<?php echo $value->id;?>"><?php echo $value->base_ini_filename;
if($value->id == $value->base_ini_filename){echo "selected='selected'";} ?> </option>
<?php } ?>
</select>
</td>
<td>
<?php echo btn_edit('customer/upload_ini/edit_ini_custom/'); ?>
</td>
<script type="text/javascript">
$(document).ready(function() {
$('#base_ini_id').change(function() {
var id = $("#base_ini_id").val();
var url = "/bizrtc/customer/upload_ini/edit_ini_custom/";
$("#edit_link").attr("href",url+ id);
$("#edit_link").attr("target","_blank");
});
});
</script>
</tr>
do this :
<select required name="base_ini_id" id="base_ini_id" class="form-control">
<option value="">Select</option>
<?php foreach($base as $value)
{
?>
<option id="emp" class="specialLink" value="<?php echo $value->id;?>"
<?php if($value->id == $user->base_ini_id){echo "selected";} ?>>
<?php echo $value->base_ini_filename;?></option>
<?php } ?>
</select>
Try below code:
<select required name="base_ini_id" id="base_ini_id" class="form-control">
<option value="">Select</option>
<?php foreach($base as $value) { ?>
<option id="emp" class="specialLink" value="<?php echo $value->id;?>" <?php if($value->id == $value->base_ini_filename){echo "selected='selected'";} ?> >
<?php echo $value->base_ini_filename; ?>
</option>
<?php } ?>
</select>
Aside from the incorrect closing tag in HTML, I also would like to note that you might want to double check your condition:
if($value->id == $value->base_ini_filename)
Seems like you are comparing very different thing
where is the user saved value ?
first you should get the user saved value into one variable
you option tag should look like below
<option id="emp" class="specialLink" value="<?php echo $value->id;?>">
<?php
if($value->id == $usersavedvalue){echo "selected='selected'";} ?> ><?php echo $value->base_ini_filename; ?> </option>

How to Check which HTML Select from Multiple Selects changed without JS

Not sure why in practice this would not work, but i am trying to make a page reload after submit, but with multiple <select>'s.
Each has it's own name, but how do I know which was changed on $_POST, without giving each select its own form.
Here is the current code:
<form id="productSelectedForm" action="products.php?filter=true" method="post">
<input type="hidden" name="model" value="<?php echo $item['model']; ?>" />
<ul>
<li>
<label><strong>Select available Grade: </strong>
<select id="gradeSelect" class="propChanged" name="grade" onchange="this.form.submit();">
<?php foreach( $props[ 'grade'] as $g) { ?>
<option value="<?php echo $g; ?>">
<?php echo $g; ?>
</option>
<?php } ?>
</select>
</label>
</li>
<li>
<label><strong>Select available Colour:</strong>
<select id="colourSelect" class="propChanged" name="colour" onchange="this.form.submit();">
<?php foreach( $props[ 'colour'] as $g) { ?>
<option value="<?php echo $g; ?>">
<?php echo $g; ?>
</option>
<?php } ?>
</select>
</label>
</li>
<li>
<label><strong>Select available Storage:</strong>
<select id="storageSelect" class="propChanged" name="storage" onchange="this.form.submit();">
<?php foreach( $props[ 'storage'] as $g) { ?>
<option value="<?php echo $g; ?>">
<?php echo $g; ?>
</option>
<?php } ?>
</select>
</label>
</li>
<li>
<label><strong>Select available Network:</strong>
<select id="networkSelect" class="propChanged" name="network" onchange="this.form.submit();">
<?php foreach( $props[ 'network'] as $g) { ?>
<option value="<?php echo $g; ?>">
<?php echo $g; ?>
</option>
<?php } ?>
</select>
</label>
</li>
</ul>
</form>
Is there any way to do this without using Javascript and individual forms, that i am missing ?
Thanks
Addy
From your given PHP it can be seen that you do not pre-select a certain option for the SELECT elements, so the browser will select the first by default.
Then when the user submits the form you cannot make the distinction between a voluntary, conscious choice for that first item, or whether the user just changed another SELECT and did not do anything with this one.
I would suggest to start every SELECT with a first option that is empty, and has an empty VALUE attribute, like this:
<option value=""><option>
<?php foreach( $props[ 'grade'] as $g) { ?>
<option value="<?php echo $g; ?>">
<?php echo $g; ?>
</option>
<?php } ?>
Then when you process the $_POST data, you will know whether the user made an explicit choice if the value is not empty, for example:
if (!empty($_POST['grade'])) {
// a grade was specified by the user.
}
Keeping State
Although your code does not seem to keep track of previously made selections, you might probably want that, and so you should somehow store those previous selections. Once you are able to do that, you will be able to find out what changed by comparing the previous values with the current submitted values.
You could use session variables for keeping track of the user's choices:
session_start();
// detect change and remember current selections
$changed = array();
foreach (array("grade", "colour", "storage", "network") as $key) {
if (isset($_POST[$key])) {
if (empty($_SESSION[$key]) || $_POST[$key] != $_SESSION[$key]) {
$changed[] = $key;
}
$_SESSION[$key] = $_POST[$key];
} else {
$_SESSION[$key] = "";
}
}
After this code has run you will have in the array $changed the list of items (e.g. ["storage"] ) that have a changed value. Normally, it should not be possible that more than one value changes at a time (since you submit immediately on a change), but who knows...
Then in your form you should make sure the user keeps the previously selected items selected, for example, for grade:
<select id="gradeSelect" class="propChanged" name="grade" onchange="this.form.submit();">
<?php foreach( $props[ 'grade'] as $g) { ?>
<option value=""></option>
<option value="<?php echo $g; ?>"
<?php if ($g == $_SESSION["grade"]) echo " selected "; ?>
>
<?php echo $g; ?>
</option>
<?php } ?>
</select>

Set HTML dropdown options from PHP variable

I want to set the on a HTML dropdown menu from a php variable. I give you my code so you can see I want to do:
<?php
$html_table = '
<table border="0" cellspacing="0" cellpadding="0"><tr>';
while($arr = pg_fetch_array($result1))
{
$html_table .= "<tr><td> $arr[0] </td></tr>";
}
$html_table .='</tr>';
?>
<p>
<select name="db" size="1">
<option> $html_table </option> #### <- that is my question, how to get that working
</select>
</p>
I hope you understand what I want to do. If you know about nicer ways, let me know.
Cheers
<?php
$options = '';
while($arr = pg_fetch_array($result1)) {
$options .= '<option>'.$arr[0].'</option>';
}
?>
<p>
<select name="db" size="1">
<?php echo $options; ?>
</select>
</p>
Just replace $html_table with <?php echo $html_table; ?> and you're good to go - from the PHP point of view that is. HTML is of course invalid, as pointed out by others.
First: you didn't close your <table> tag.
Second: you can't paste<table> tag into <select>. You should put a series of <option> elements, each with one element of the array.
Third: To print PHP variable you should do like this:
<option><?php echo $html_table; ?></option>
But again: it will break your HTML, because table can't be inside select option.
The best solution is:
<select name="db" size="1">
<?php while ($arr = pg_fetch_array($result1)): ?>
<option><?php echo $arr[0]; ?></option>
<?php endwhile; ?>
</select>
so set your option elements like this
<option value="<?php echo $html_table; ?>"><?php echo $html_table; ?></option>
just know, an option can't be a table.

Updating a second dropdown after the first one has been chosen in javascript/php/html

I need an example that can let me populate the 2nd,3rd,...,7th dropdown list (all same entries) after the 1st dropdown list has been selected. I have all the values stored in an array in PHP before the HTML head.
I've looked at some examples, and couldnt find a specific solution
For example:
Let's say I have a warehouse full of franchisees and outlets under those aforementioned franchisees, I need to create order lists for 7 days ahead for that specific franchisee with n outlets. My solution was that the user/warehouse operator would pick a franchisee, then the 2nd (up to 7th) drop down list would have the outlets who are under that picked franchisee.
Thanks.
<?php
$productClass = new product();
$productClass->setProductList();
$franchiseeClass = new users();
$franchiseeClass->getAndSetAllFranchisees();
$franchiseeArray = $franchiseeClass->getUserList();
$operatorOutletClass = new users();
$operatorOutletClass->getAndSetUserByLevel("5");
?>
<html><head></head><body>
<div align="center">
<form name="BBOFranchisee" method="post" action="?" onSubmit="return checkSubmit()">
<table align="center" border="1">
<tr>
<td>
Franchisee:
</td>
<td colspan="8">
<select name="displayFranchisee" size="1" onChange="populateOutlet('findOutletByFranchisee.php?fid='+this.value)">
<option label="franchisees" value="0">--Choose Franchisee--</option>
<?php
for($i=0;$i<count($franchiseeArray);$i++)
{
foreach($franchiseeArray[$i] as $key => $val)
{
echo "<option label=\"franchisees\" value=\"$key\">$val</option>\n";
}
} ?>
</select>
</td>
</tr>
<tr>
<?php for($j=0;$j<7;$j++) { ?>
<td>
Outlet:
<select name="displayOutlet<?php echo $j; ?>" size="1">
<option label="outlets" value="0"> </option>
</select>
</td>
<?php } ?>
</tr>
</body>
</html>
I guess the continuation is the javascript part.. i tried to look at the solution that http://roshanbh.com.np/2007/12/change-dropdown-list-options-values-from-database-with-ajax-and-php.html provides, but it wont work with multiple drop downs.
In order to use that javascript example, you need to add an id to your 2nd-7th selects - id="displayOutlet<?php echo $j; ?>"
<?php for($j=0;$j<7;$j++) { ?>
<td>
Outlet:
<select name="displayOutlet<?php echo $j; ?>" id="displayOutlet<?php echo $j; ?>"size="1">
<option label="outlets" value="0"> </option>
</select>
</td>
<?php } ?>
Then in your javascript code, you can add a loop for the 2nd-7th selects.
if(req.status == 200) { // which represents ok status
for(var i=0;i<7;i++){
document.getElementById('displayOutlet'+i).innerHTML=req.responseText;
}
}

Categories