i would like to give a dropdown a default value with php / mysql queries
like in the db table users there is a record saying usergroup
and i want it so the dropdown default value changes to the value of that record i got this code so far:
Usergroup: <select value=\"" . $usergroup . "\" name='usergroup'>
<option value='4'>Administrator</option>
<option value='3'>Moderator</option>
<option value='2'>Donator</option>
<option value='1'>Regular Member</option>
</select>
and php part:
$username = mysql_real_escape_string(htmlentities(stripslashes($_POST['username'])));
$query="SELECT * FROM users where `username` like '{$username}'";
$result = mysql_query($query);
$num = mysql_numrows($result);
$i=0;
while ($i < $num) {
$username2=mysql_result($result,$i,"username");
$name=mysql_result($result,$i,"name");
$usergroup=mysql_result($result,$i,"usergroup");
$email=mysql_result($result,$i,"email");
$ip=mysql_result($result,$i,"ip");
$profpic=mysql_result($result,$i,"profilepic");
$id=mysql_result($result,$i,"id");
$i++;
}
thanks in regards!
i gotten it to work with a few if statements, i was planning to use it that way but didn't want the code to become too messy:
if ($usergroup == 4) {
echo "<select name='usergroup'><option value='4' selected='selected'>Administrator</option><option value='3'>Moderator</option><option value='2'>Donator</option><option value='1'>Regulat Member</option></select><br>";
}
if ($usergroup == 3) {
echo "<select name='usergroup'><option value='4'>Administrator</option><option value='3' selected='selected'>Moderator</option><option value='2'>Donator</option><option value='1'>Regulat Member</option></select><br>";
}
if ($usergroup == 2) {
echo "<select name='usergroup'><option value='4'>Administrator</option><option value='3'>Moderator</option><option value='2' selected='selected'>Donator</option><option value='1'>Regulat Member</option></select><br>";
}
if ($usergroup == 1) {
echo "<select name='usergroup'><option value='4'>Administrator</option><option value='3'>Moderator</option><option value='2'>Donator</option><option value='1' selected='selected'>Regulat Member</option></select><br>";
}
This is probably what you are looking for:
<?php
// $userGroup = the value from the database
$userGroup = 4;
?>
<select>
<option <?php echo ($userGroup) == 1 ? "selected" : "" ?> value="1">Administrator</option>
<option <?php echo ($userGroup) == 2 ? "selected" : "" ?> value="2">Moderator</option>
<option <?php echo ($userGroup) == 3 ? "selected" : "" ?> value="3">Donator</option>
<option <?php echo ($userGroup) == 4 ? "selected" : "" ?> value="4">Regular member</option>
</select>
If you don't recognize the if/else syntax (shorthand), check out this guide. Really handy for situations like this.
Related
I know only this method. this method is assume that you know the values in all <option>
<select name="agama" id="agama">
<option value="Islam"<?php if ($rows['agama'] === 'Islam') echo ' selected="selected"'>Islam</option>
<option value="Khatolik"<?php if ($rows['agama'] === 'Khatolik') echo ' selected="selected"'>Khatolik</option>
<option value="Protestan"<?php if ($rows['agama'] === 'Protestan') echo ' selected="selected"'>Protestan</option>
<option value="Hindu"<?php if ($rows['agama'] === 'Hindu') echo ' selected="selected"'>Hindu</option>
<option value="Buddha"<?php if ($rows['agama'] === 'Buddha') echo ' selected="selected"'>Buddha</option>
<option value="Lain-Lain"<?php if ($rows['agama'] === 'Lain-Lain') echo ' selected="selected"'>Lain-Lain</option>
</select>
.... the above code is example from other people not mine.
but My case is the <option> is select from database too.
I have 2 table, oav_event and oav_album
the oav_album has foreign key (event_id) from oav_event table
I want to check if row['event_id'] from oav_album table is equal to option value (from oav_event table) if true, then set selected="selected"
while($row = mysqli_fetch_assoc($result)) { ?>
<option value="<?php echo $row['event_id']; ?>" >Event: <?php echo $row['event_date']; ?> </option>
<?php } ?>
the option will change depend on change in database table, so I don't know the value in option. How should I do?
<select name="event_id">
<?php
$sql = "SELECT * FROM oav_event";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)) {
$selected = "";
if($row['event_id'] == $Yourmatchvalue)
{
$selected = "selected";
}
?>
<option value="<?php echo $row['event_id']; ?>" selected="<?php echo $selected; ?>" >Event: <?php echo $row['event_date']; ?> </option>
<?php } ?>
</select>
may this helps your. you need to replace $Yourmatchvalue variable with your variable.
You can use $_GET as the method on your form and pass the id of the record using it:
while($row = mysqli_fetch_assoc($result)) {
if (!empty($_GET['event_id']) && $row['event_id'] == $_GET['event_id']) {
$selected = 'selected = "selected"';
} else {
$selected = '';
}
echo '<option '.$selected.' value="'.$row["event_id"].'">'.$row["event_date"].'</option>';
}
Here is a solution,
$selected_value = 'Hindu'; // This will come from database
Change option tag with this
<option value="<?php echo $row['event_id']; ?>" <?php echo ($row['event_id'] == $selected_value) ? 'selected="selected"' : ''; ?> >Event: <?php echo $row['event_date']; ?> </option>
Create one function which will create options list like this:
function setDropdownValue($selectQueue_list,$selectedVal)
{
$queueVal = '';
$selectQueue_list_res=$db->query($selectQueue_list);
while($selectQueue_list_res_row=$db->fetchByAssoc($selectQueue_list_res))
{
$val = $selectQueue_list_res_row['id'];
$name = $selectQueue_list_res_row['name'];
if($val == $selectedVal)
{
$queueVal .= "<option value='$val' selected='selected' label='$name'>$name</option>";
}
else
{
$queueVal .= "<option value='$val' label='$name'>$name</option>";
}
}
return $queueVal;
}
Then create a query:
$get_value_query="SELECT id, name FROM table";
$dropdown_selected_value = !empty($dropdown_value) ? $dropdown_value: ''; // Pass value which you want to be selected in dropdown
Then call this function:
$dropdown_options = setDropdownValue($get_value_query, $dropdown_selected_value);
Later when you get dropdown options in $dropdown_options, use jquery to populate the dropdown, like this:
$('#dropdown_select_id').html("$dropdown_options");
Give it a try, and let me know.
These is my code to get the information from the database. So status is the one where in my database would be either Interested or Not Interested.
while($row= mysqli_fetch_array($result))
{
$ID =$row['Particulars_ID'];
$name = $row['Name'];
$number =$row['Number'];
$status =$row['Status'];
$remarks =$row['Remarks'];
}
I would like to echo out the value Not Interested if my database shows that this person is not interested. However from my below code, it always show Interested no matter which person i click on.
echo "<select name = 'status', id = status>
<option value='Interested'>Interested</option>
<option value='Not Interested'>Not Interested</option>
</select><br>";
first - you do not need the comma in the select, second - ensure that this is the only element with the id of status, third - simply check the $status value in each option and echo selected if it is.
echo "<select name = 'status' id = 'status'>
<option value='Interested'";
if($status == "Interested"){echo " selected";}
echo">Interested</option>
<option value='Not Interested' ";
if($status == "Not Interested"){echo " selected";}
echo">Not Interested</option>
</select><br>";
since the default is the first option Interested, then if ($status === "Not Interested") set the option attribute selected
<?php
if ($status === "Not Interested") $selected = "selected";
else $selected = "";
echo "<select name = 'status', id = status>
<option value='Interested'>Interested</option>
<option value='Not Interested' $selected>Not Interested</option>
</select><br>";
Change this
echo "<select name = 'status', id = status>
<option value='Interested'>Interested</option>
<option value='Not Interested'>Not Interested</option>
</select><br>";
to this
// Set the selected attributes based on the value of status
$interested = ($status === 'Interested') ? ' selected' : '';
$notInterested = ($interested === '') ? ' selected' : '';
echo <<< SELECT
<select name="status" id="status">
<option$interested>Interested</option>
<option$notInterested>Not Interested</option>
</select>
SELECT;
You don't need the value attribute if the value is the same as the text.
If there were more than two options, I recommend a loop like this:
<?php foreach ($options as $o) : ?>
<option<?php if ($o === $optionValue) ?> selected<?php endif ?>><?= optionValue ?></option>
<?php endforeach ?>
I have a select box that when the page loads it sets a value selected based upon a variable. I also have 2 IF statements that checks the variable and returns the results. Here is my code I am trying to do:
<select id="satbreak1" name="satbreak1">
<?php if ($trimmed_title[0] == "Guest")
{
echo ('<option selected value="#">Select One</option>');
echo ('<option value="y">Yes</option>');
echo ('<option value="n">No</option>');
}
if ($trimmed_title[0] != "Guest")
{
echo ('<option <?php if($trimmed_satBreakfast[0] == "") echo ("selected ") ; ?> value="#">Select One</option>');
echo ('<option <?php if($trimmed_satBreakfast[0] == "y") echo ("selected ") ; ?> value="y">Yes</option>');
echo ('<option <?php if($trimmed_satBreakfast[0] == "n") echo ("selected ") ; ?> value="n">No</option>');
}
?>
</select>
What I need to do is if $trimmed_title[0] == "Guest" then echo out the first part, but if $trimmed_title[0] != "Guest") then echo out the second part and also check what the status of $trimmed_satBreakfast[0] is and set the selected value.
What is the best / correct way to handle this?
The first part works fine but having an php script in a echo seems to fail.
I think this might do the trick - not tested but theoretically looks ok
<select id="satbreak1" name="satbreak1">
<?php
if ( $trimmed_title[0] == "Guest" ) {
echo '
<option selected value="#">Select One</option>
<option value="y">Yes</option>
<option value="n">No</option>';
} else {
$tsb=$trimmed_satBreakfast[0];
echo '
<option selected value="#" '.( $tsb=='' ? 'selected' : '' ).'>Select One</option>
<option value="y" '.( $tsb=='y' ? 'selected' : '' ) .'>Yes</option>
<option value="n"'.( $tsb=='n' ? 'selected' : '' ).'>No</option>';
}
?>
</select>
Something like this:
<?php
$options = array(
'values' => array('', 'y', 'n'),
'titles' => array('Select One', 'Yes', 'No')
)
$index = $trimmed_title[0] == 'Guest' ? 0 : array_search($trimmed_satBreakfast[0], $options['values']);
//if ($index === false) $index = 0;
$optHtml = array();
foreach($options['values'] as $i => $val) {
$selected = $index == $i ? 'selected' : '';
$optHtml[] = "<option $selected value=\"$val\">{$options['titles'][$i]}</option>";
}
$optHtml = implode("\n", $optHtml);
?>
<select id="id" name="name">
<?php echo $optHtml?>
</select>
or any other style to split html from php:
//if ($index === false) $index = 0;
...
?>
<select id="id" name="name">
<?php foreach($options['values'] as $i => $val) {?>
<option <?=$index==$i?'selected':''?> value="<?=$val?>">
<?=$options['titles'][$i]?>
</option>
<?php } ?>
</select>
In this case HTML not used in the common php code, but php used in HTML (only general template features: loop through data and echo it).
Common purpose of this is possibility to extract template part to separate file.
It is already PHP so you have to remove the php tags
echo ('<option '.($trimmed_satBreakfast[0] == "") ? : '"selected"' : ''.' value="#">Select One</option>');
echo ('<option '.($trimmed_satBreakfast[0] == "y") ? : '"selected"' : ''.' value="y">Yes</option>');
echo ('<option '.($trimmed_satBreakfast[0] == "n") ? : '"selected"' : ''.' value="n">No</option>');
I currently have this code to remember the last selected item on post.
<select tabindex="1" name="system_customer">
<option value="Choose">- Choose a Customer -</option>
<?PHP while ($row = mysql_fetch_array($result_get_users))
{
echo "<option value='".$row['id']."'";
echo $system_customer == $row['id'] ? 'selected="selected"' : '';
echo ">".$row['firstname']." ".$row['lastname']."</option>";
} ?>>
</select>
On load (before submitting) the list defaults to "Choose". What I would like to do, is if the URL has "userid=1" to select user 1, or if it is "userid=2" then select user 2, but keep the code to remember the last selected.
Thank you.
PS - cannot use mysqli or PDO due to the software I am writing a module for. It is hard coded and encrypted.
Add this above your code -
$system_customer = "";
if (!empty($_GET['userid'])) {
$system_customer = $_GET['userid'];
}
Then -
<select tabindex="1" name="system_customer">
<option value="Choose">- Choose a Customer -</option>
<?PHP
while ($row = mysql_fetch_array($result_get_users)) {
echo "<option value='".$row['id']."'";
echo $system_customer == $row['id'] ? 'selected' : '';
echo ">".$row['firstname']." ".$row['lastname']."</option>";
}
?>
</select>
You can use session or cookie
for exp :
<?PHP
Session_start();
if ( isset ( $_POST['system_customer'] ) ) {
$_SESSION['SELECTED_USER_ID'] = $_POST['system_customer'];
?>
and you haave to make some changes in you dropdown list
<select tabindex="1" name="system_customer">
<option value="Choose">- Choose a Customer -</option>
<?PHP while ($row = mysql_fetch_array($result_get_users)) {
echo "<option value='".$row['id']."'";
echo $_SESSION['SELECTED_USER_ID'] == $row['id'] ? 'selected="selected"' : '';
echo ">".$row['firstname']." ".$row['lastname']."</option>";
}
?>>
</select>
in you select form you have to check if session id is equal to row if then put selected attribiut for that option .
but if you want to save details for long time it's better to use cookie or databases you can't do it in another cases .
I've got a few select lists on my form which look a bit like this:
<option value="400000" <?php if($_GET['MaxPrice'] == "400000") { echo("selected"); } ?>>£400,000</option>
As you can see (above) I've got a bit of PHP in there telling the form to remember it's selection upon submit.
Is there a short bit of PHP that would remember every selection without the rather heavy method I'm using above?
UPDATE:
<select name="MaxPrice" id="MaxPrice">
<option value="9999999" selected>Price (Max)</option>
<option value="100000" <?php if($_GET['MaxPrice'] == "100000") { echo("selected"); } ?>>£100,000</option>
<option value="200000" <?php if($_GET['MaxPrice'] == "200000") { echo("selected"); } ?>>£200,000</option>
<option value="300000" <?php if($_GET['MaxPrice'] == "300000") { echo("selected"); } ?>>£300,000</option>
<option value="400000" <?php if($_GET['MaxPrice'] == "400000") { echo("selected"); } ?>>£400,000</option>
</select>
UPDATE 2: Is there a way to implement this technique into some JavaScript?
if (BuyRent=='buy')
document.SearchForm.MaxPrice.options[9999999]=new Option("Price (Max)","150000000");
document.SearchForm.MaxPrice.options[100000]=new Option("\u00A3100,000","100000");
document.SearchForm.MaxPrice.options[200000]=new Option("\u00A3200,000","200000");
document.SearchForm.MaxPrice.options[300000]=new Option("\u00A3300,000","300000");
document.SearchForm.MaxPrice.options[400000]=new Option("\u00A3400,000","400000");
Usually, you use some kind of loop. Here's an example :
$my_values = array(100000, 200000, 300000, 400000);
foreach($my_values as $value) {
echo "<option value=\"{$value}\"";
echo ($_GET['MaxPrice'] == $value) ? 'selected="selected"';
echo ">" . number_format($value, 0, '.', ',') . "</option>";
}
Which would print 4 tags such as <option value="100000" selected="selected">100,000</option>.
This code also uses the ternary operator, but it's obviously not mandatory, you can write the whole if/else statement if wanted.
Edit:
<select name="MaxPrice" id="MaxPrice">
<option value="9999999">Price (Max)</option>
<?php
$my_values = array(100000, 200000, 300000, 400000);
foreach($my_values as $value) {
echo "<option value=\"{$value}\"";
echo ($_GET['MaxPrice'] == $value) ? 'selected="selected"' : "";
echo ">£ " . number_format($value, 0, '.', ',') . "</option>";
}
?>
</select>
good question, it made me wonder if there exists a js plugin to remember form field out there.
And the answer seems to be "Yes!"