I have a problem to select the preferred value
<?php
...
if ($result->num_rows > 0) {
$selected_rep = ($row['ID']=67) ? 'selected="selected"' :'';
while($row = $result->fetch_assoc()) {
echo "<option $selected_rep value='". $row["ID"]."'>" . $row["NUME"]. ' '. $row["PRENUME"]. "</option>\n";
}
}
...
?>
I want to select the value with ID=67.
Not function $selected_rep ,select all values.
Simply put the selected test inside the loop where it has access to the loaded $row for each of the resultset rows you are processing
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$selected_rep = ($row['ID'] == 67) ? 'selected="selected"' :'';
echo "<option $selected_rep value='". $row["ID"]."'>" . $row["NUME"]. ' '. $row["PRENUME"]. "</option>\n";
}
}
Ahhh and you write test using == and not =
== is this equal to that
= set this to that
Related
So, what I'm trying to achieve that after the user hits submit, the dropdown retains the value that the user have chosen. This is my code for the dropdown, I understand that I have to selected = 'selected', but I couldn't figure out how it fits in my code.
if (mysqli_num_rows($result) > 0) { // output data of each row
while($row_branch = mysqli_fetch_array($result)) {
$menu_branch .= "<option value='".$row_branch["0"]."'>" .
$row_branch["0"]. "</option>";
}
}
echo $menu_branch;
You cannot just add selected = selected inside the loop because every option will be selected.
You might need the if statement to select the option of your choice.
For example:
if($row_branch["0"] == 'bar'){
$menu_branch .= "<option value='".$row_branch["0"]."' selected>" .
$row_branch["0"]. "</option>";
}else{
$menu_branch .= "<option value='".$row_branch["0"]."'>" .
$row_branch["0"]. "</option>";
}
When the user select an option, Where you submit this information?
If this information return from a database
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row_branch = mysqli_fetch_array($result)) {
$select = ($row_branch["0"]==$value_db)? "selected='selected'" : "";
$menu_branch .= "<option value='".$row_branch["0"]."' $select>" .
$row_branch["0"]. "</option>";
}
}
if i understand right:
// $yourvalue is previous dropdown value
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row_branch = mysqli_fetch_array($result)) {
if ($row_branch["0"] == $yourvalue)
$selected = "selected";
else
$selected = "";
$menu_branch .= "<option value='".$row_branch["0"]."' $selected>" .
$row_branch["0"]. "</option>";
}
}
echo $menu_branch;
This script does not display the DB value in a drop down on the edit form.
<?php
echo "<select name='assign' value=''><option>Select name</option>";
while ($r = mysql_fetch_array($result)) {
$value = $r['name'];
echo "<option value=" . $r['emp_id'] . ">" . $r['name'] . " if ($name=='$value') echo 'selected = 'selected''></option>";
}
echo "</select>";
It does not show any error. How it can write in a correct way.
You can try this :
$echoSting = '<select name="assign"><option value="">Select name</option>'.PHP_EOL;
while($r = mysql_fetch_array($result)) {
$value=$r['name'];
$echoSting .= '<option value="'.$r['emp_id'].'" '.($name==$value ? 'selected' : '').'>'.$r['name'].'</option>'.PHP_EOL;
}
$echoSting .= '</select>'.PHP_EOL;
echo $echoSting;
a side note, try looking into PDO for your database stuff : http://php.net/manual/en/book.pdo.php
Try this:
echo "<select name='assign' value=''><option>Select name</option>";
while($r = mysql_fetch_array($result)) {
$value=$r['name'];
echo "<option value='.$r['emp_id'].'>'.$r['name'].' "; if ($name=='$value') echo "selected = 'selected'";echo">$value</option>";
}
echo "</select>";
I'm trying to get a drop down menu to keep its selected value when the user hits submit, but it fails due to errors on the form.
I have a while loop returning values from a database to build the options for the drop down, but how do I echo "selected" on the right option?
I have tried if($district == $row["name"]) { echo "selected";} as you see below, but it doesn't work.
<?php
$result = mysql_query("SELECT dist.name FROM districts AS dist JOIN int_bd AS ibd ON dist.id = ibd.districts_id WHERE banners_id = 6 GROUP BY dist.id ORDER BY dist.id ASC", $connection);
if (!result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
echo '<option value="{$row["name"]}"'; if($district == $row["name"]) { echo "selected";} ; echo '>' . $row["name"] . "</option>";
}
?>
Sorry for the delay. None of the suggested answers worked for me. Any other ideas?
Can you try this,
<?php
$result = mysql_query("SELECT dist.name FROM districts AS dist JOIN int_bd AS ibd ON dist.id = ibd.districts_id WHERE banners_id = 6 GROUP BY dist.id ORDER BY dist.id ASC", $connection);
if (!result) {
die("Database query failed: " . mysql_error());
}
$district = $_REQUEST['name']; // You need pass the value you have been submitted
while ($row = mysql_fetch_array($result)) {
$selected ="";
if(trim($district) == trim($row["name"])) { $selected = "selected";}
echo '<option value="{$row["name"]}" '.$selected.' >' . $row["name"] . "</option>";
}
?>
Try this..
if($district == $row["name"])
{
echo "<option value='$district' selected>$district</option>";
}
I just found the answer. This is what I did:
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row["name"] . '"';
if($row["name"] == $district) { echo 'selected';} ;
echo '>' . $row["name"] . '</option>';
}
It seems to have been this line
echo '<option value="{$row["name"]}"';
that was causing the problem.
A drop down list is printed in php in a while loop by taking value from database table. Here is the code:
<select name='supervisor' class='form-control' name='supervisor'>
<?php
$sql = "SELECT username FROM system_user where type='supervisor'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['username'] ."'>" . $row['username'] ."</option>";}?>
</select>
How can I set a default value for this? There is one 'username' value that i want to make as the default value. How can i do that?
<select name='supervisor' class='form-control' name='supervisor'>
<?php
$sql = "SELECT username FROM system_user where type='supervisor'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
$sel = ''; if($row['username'] == 'usename'){ $sel = 'selected'; }
echo "<option $sel value='" . $row['username'] ."'>" . $row['username'] ."</option>";
} ?>
</select>
function dropDown(array $array, $default = null, $select_attrs = '')
{
$s = '<select $select_attr>';
foreach((array)$array as $k => &$v) {
$default = ($v === $default) ? 'selected' : null;
$s.='<option '.$default.' >'.$v.'</option>';
}
return $s;
}
this one worked for me...
<select name='supervisor' class='form-control' name='supervisor'>
<?php
$sql = "SELECT username FROM system_user where type='supervisor'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
$sel = ''; if($row['username'] == 'usename'){ $sel = 'selected'; }
echo "<option $sel value='" . $row['username'] ."'>" . $row['username'] ."</option>";
} ?>
I have a section of my code with a few elseif condtions that don't seem to be working properly. I've built a conditional drop down form that's working for the most part so far except for when it reach a couple elseifs.
I'm using JS to send the dropdown option name to my PHP script with GET which is where it seems to be going weird. For most of the form it pulls the options for the next dropdown from a MySQL query but for the first drop I've just used a few elseifs to make it less complex. you can check the live script here. Here's the section giving me trouble (it's the last two elseifs):
if (isset($_GET['setpoint'])) {
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row{'stp'} . "'>" . $row{'stp'} . "</option>";
}
} elseif (isset($_GET['power'])) {
echo "<option>Please Choose Setpoint Options</option>";
while ($row = mysql_fetch_array($result)) {
$row{'stp'} = ucfirst($row{'stp'}); //capitalizes the first letter; necessary?
echo "<option value='" . $row{'stp'} . "'>" . $row{'stp'} . "</option>";
}
} elseif (isset($_GET['source'])) {
echo "<option>Please Choose Input Range</option>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row{'sir'} . "'>" . $row{'sir'} . "</option>";
}
} elseif (isset($_GET['type']) && $_GET['type'] = "Digital") {
echo "<option>Please Choose Input Source</option>";
echo "<option value='RS232C'>RS232C</option><option value='RS422'>RS422</option><option value='RS485'>RS485</option><option value='current loop'>current loop</option>";
} elseif (isset($_GET['type']) && $_GET['type'] = "Analog") {
echo "<option>Please Choose Input Source</option>";
echo "<option value='DC current'>DC Current</option><option value='DC voltage'>DC Voltage</option><option value='AC current'>AC Current</option><option value='AC voltage'>AC Voltage</option><option value='process'>Process</option><option value='thermocouple'>Thermocouple</option><option value='RDT'>rdt</option>";
}
The first dropdown will set $_GET['type'] to either 'Analog' or 'Digital' but no matter which option $_GET['type'] is set to it will just run the first elseif that checks if $_GET['type'] is set. If $_GET['type'] = 'Analog' then it should be returning the last elseif and not the second to last.
This is most of the script for reference:
//prevents injections
//any order
isset($_GET['type'])?$type = urldecode($_GET['type']):"";
//$type = mysql_real_escape_string(urldecode($_GET['type']));
isset($_GET['source'])?$source = mysql_real_escape_string(urldecode($_GET['source'])):"";
isset($_GET['range'])?$power = mysql_real_escape_string(urldecode($_GET['range'])):"";
isset($_GET['setpoint'])?$setpoint = mysql_real_escape_string(urldecode($_GET['setpoint'])):"";
//forms the query depending on what data is recieved through GET
//first option on the bottom; last option on the top to avoid conflicts
if (isset($_GET['setpoint'])) {
$query = "SELECT DISTINCT stp FROM meters WHERE sio='$range' AND pso='$power' AND stp='$setpoint' ORDER BY model";
} elseif (isset($_GET['power'])) {
$query = "SELECT DISTINCT stp FROM meters WHERE sio='$range' AND pso='$power' ORDER BY model";
} elseif (isset($_GET['range'])) {
$query = "SELECT DISTINCT pso FROM meters WHERE sio='$range' ORDER BY model";
} elseif (isset($_GET['source'])) {
$query = "SELECT DISTINCT sir FROM meters WHERE sio LIKE '%$source%' ORDER BY sir";
}
//creates a result array from query results
isset($query)?$result = mysql_query($query):"";
//outputs dropdown options dependent on what GET variables are set
//first option on the bottom; last option on the top to avoid conflicts
if (isset($_GET['setpoint'])) {
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row{'stp'} . "'>" . $row{'stp'} . "</option>";
}
} elseif (isset($_GET['power'])) {
echo "<option>Please Choose Setpoint Options</option>";
while ($row = mysql_fetch_array($result)) {
$row{'stp'} = ucfirst($row{'stp'}); //capitalizes the first letter; necessary?
echo "<option value='" . $row{'stp'} . "'>" . $row{'stp'} . "</option>";
}
} elseif (isset($_GET['source'])) {
echo "<option>Please Choose Input Range</option>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row{'sir'} . "'>" . $row{'sir'} . "</option>";
}
} elseif (isset($_GET['type']) && $_GET['type'] = "Digital") {
echo "<option>Please Choose Input Source</option>";
echo "<option value='RS232C'>RS232C</option><option value='RS422'>RS422</option><option value='RS485'>RS485</option><option value='current loop'>current loop</option>";
} elseif (isset($_GET['type']) && $_GET['type'] = "Analog") {
echo "<option>Please Choose Input Source</option>";
echo "<option value='DC current'>DC Current</option><option value='DC voltage'>DC Voltage</option><option value='AC current'>AC Current</option><option value='AC voltage'>AC Voltage</option><option value='process'>Process</option><option value='thermocouple'>Thermocouple</option><option value='RDT'>rdt</option>";
Again, you can check the live script here.
$_GET['type'] = "Digital" is assignment, not a check.
What you need is
$_GET['type'] == "Digital"
Same with $_GET['type'] = "Analog"
It should be
$_GET['type'] == "Analog"
Also, like Darkbee mentioned, use PDO or MySQLi
You're assigning $_GET['type'] when I think you want the comparison operator equals:
$_GET['type'] = "Digital"
becomes
$_GET['type'] == "Digital"
try
elseif (isset($_GET['type']) && $_GET['type'] == "Digital")
and
elseif (isset($_GET['type']) && $_GET['type'] == "Analog")
Two Equal signs is an equal one is an assignment (will return true always)