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;
Related
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
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 have form with a number of drop boxes which have the numbers 1-5 in them. I can use this code to populate a drop down but was wondering if I can somehow only make the call to the db once but populate all the drop downs that use the same numbers?
<?php
$sql = "SELECT * FROM riskNumDrop";
$result = $conn->query($sql);
if (!$conn->query($sql)) {
echo "query failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
echo '<select class="assess" name="precontcons" style="width:4em">' ;
while($row = $result->fetch_assoc()){echo '<option value='. $row['riskNumDrop'] .'>'.$row['riskNumDrop'].'</option>';}
?> </select>
So Ideally, I generate the output once and reuse it multiple times. Im guessing an array (which $result already is) but how do I populate a drop down from it? TIA
Saving as a string would save you the processing of having to loop through the same data generating the same output multiple times. If this is what you want, you could do the following.
Replace:
echo '<select class="assess" name="precontcons" style="width:4em">' ;
while($row = $result->fetch_assoc()){echo '<option value='. $row['riskNumDrop'] .'>'.$row['riskNumDrop'].'</option>';}
?> </select>
with:
$drop = '<select class="assess" name="precontcons" style="width:4em">' ;
while($row = $result->fetch_assoc()){
drop .= '<option value='. $row['riskNumDrop'].'>'.$row['riskNumDrop'].'</option>';
}
$drop .= '</select>';
Then you can echo $drop several times if you want.
If for whatever reason you want different select attributes, you could just save the options list and print the select around that, like this:
$dropOptions = "";
while($row = $result->fetch_assoc()){
$dropOptions .= '<option value='. $row['riskNumDrop'].'>'.$row['riskNumDrop'].'</option>';
}
Then just echo '<select class="foo" name="bar">'.$dropOptions.'</select'>
You could store the data into an array, and then print the data by enumerating the array.
$risks = array();
// Load values into array.
while ($row = $result->fetch_assoc()) {
array_push($row['riskNumDrop']);
}
// Drop down #1
echo '<select>';
foreach ($risks as $risk) {
echo '<option value='. $risk .'>'. $risk .'</option>';
}
echo '</select>';
// Drop down #2
echo '<select>';
foreach ($risks as $risk) {
echo '<option value='. $risk .'>'. $risk .'</option>';
}
echo '</select>';
Simply get the results into an array using the fetch_all method. It returns all rows from the result set into an associative or numeric array. The you can do whatever you want with such array:
$result = $conn->query('SELECT * FROM riskNumDrop');
if (!$result) {
echo "query failed: (" . $mysqli->errno . ") " . $mysqli->error;
exit; // fetch_* functions cannot be called on error (sice $result is false)
}
// get all rows from the result
$rows = $result->fetch_all(MYSQLI_ASSOC);
// output first dropdown...
echo '<select name="dropdown_1">';
foreach ($rows as $row) {
// options for the first dropdown (same as you did before)
}
echo '</select>';
// output second dropdown...
echo '<select name="dropdown_2">';
foreach ($rows as $row) {
// options for the second dropdown
}
echo '</select>';
I'm trying to get selected option in select,I need to make a select from my database and use id to mark which category is set.
echo"<select>";
while ($row = mysql_fetch_array($rezultat1))
{ if ($row['biljka_id']==$GET['b']) {
echo "<option value=".$row['biljka_id']." selected='selected' >".$row['naziv']."</option>";
} else { echo "<option value=".$row['biljka_id'].">".$row['naziv']."</option>";
}
echo"</select>";
Following is the short code.
You need not use if else statement as you need to justify only one variable depending upon condition.
You can do it with ternary operator.
<?php
echo "<select>";
while ($row = mysql_fetch_array($rezultat1)) {
$selected = ($row['biljka_id']==$GET['b']) ? 'selected="selected"' : '';
echo "<option value=".$row['biljka_id']. $selected ">".$row['naziv']."</option>";
}
echo"</select>";
?>
Rererence
You didn't properly close your while loop or the else part of your if, depending on how you look at it. Plus, why not use concatenation instead. So, you might re-write your code as follows:
$selectStr = '';
$selectStr .= "<select>";
while ($row = mysql_fetch_array($rezultat1))
{
if ($row['biljka_id']==$GET['b'])
{
$selectStr .= "<option value='".$row['biljka_id']."' selected='selected' >".$row['naziv']."</option>";
}
else
{
$selectStr .= "<option value='".$row['biljka_id']."'>". $row['naziv']. "</option>";
}
}
$selectStr .= "</select>";
echo $selectStr;
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)