This is likely a lot simpler than I'm thinking but I can't seem to figure it out.
I have the following code that's part of a image results list:
<option value="30" <?php echo ($_SESSION['results']== 30) ? 'selected' : ''; ?>>30</option>
<option value="40" <?php echo ($_SESSION['results']== 40) ? 'selected' : ''; ?>>40</option>
<option value="50" <?php echo ($_SESSION['results']== 50) ? 'selected' : ''; ?>>50</option>
<option value="60" <?php echo ($_SESSION['results']== 60) ? 'selected' : ''; ?>>60</option>
<option value="70" <?php echo ($_SESSION['results']== 70) ? 'selected' : ''; ?>>70</option>
<option value="80" <?php echo ($_SESSION['results']== 80) ? 'selected' : ''; ?>>80</option>
<option value="90" <?php echo ($_SESSION['results']== 90) ? 'selected' : ''; ?>>90</option>
The default of course is 40 results but I want the default to be 80. How do I make it so 80 is automatically selected by default
A cleaner way to do this would be:
<?php
session_start(); //Start session
$_SESSION['results'] = $_SESSION['results'] ? $_SESSION['results'] : 80; //Check and set the session variable else default it to 80
$numbers = [30, 40, 50, 60, 70, 80, 90]; //Initialize your array
?>
<select name="test">
<?php
foreach ($numbers as $num) { //Loop through the array and populate your select box.
?>
<option value="<?php echo $num; ?>" <?php echo ($_SESSION['results'] == $num) ? 'selected' : ''; ?>><?php echo $num; ?></option>
<?php
}
?>
</select>
Related
I have a form which a user fills up and hits "Save & Next" which will take them to another page where the user can upload images and hit "Final Submit". They can also go back to the previous page to edit the data.
At that time, all the data he had previously filled up should be displayed on the text box. I have used session variable to store the data and display it.
I am stuck in the drop down box.
<select name="District">
<option value="East">East</option>
<option value="West">West</option>
<option value="North">North</option>
<option value="South">South</option>
</select>
When the user submits, I am storing the selected value in Session $_SESSION['District'] = $_POST['District']; and when the user clicks back to go the previous page, I need to auto select that option value in the drop down.
How can I achieve this?
Using array would make it easier.
<?php
$options = array(
'East', 'West', 'North', 'South',
);
?>
<select name="District">
<?php foreach($options as $option) { ?>
<option value="<?php echo $option; ?>" <?php echo (isset($_SESSION['District']) && $_SESSION['District'] == $option) ? "selected" : "" ?>><?php echo $option; ?></option>
<?php } ?>
</select>
Check value in session and if matches set the attibute selected
<select name="District">
<option <?php if (!empty($_POST['District']) && $_POST['District'] == 'East'){ echo 'selected'; }?> value="East">East</option>
<option <?php if (!empty($_POST['District']) && $_POST['District'] == 'West'){ echo 'selected'; }?> value="West">West</option>
<option <?php if (!empty($_POST['District']) && $_POST['District'] == 'North'){ echo 'selected'; }?> value="North">North</option>
<option <?php if (!empty($_POST['District']) && $_POST['District'] == 'South'){ echo 'selected'; }?> value="South">South</option>
</select>
Just check the selected value against the one stored in session.
Your option will look like this:
<option value="East" <?php echo ($_SESSION['District']=="East" ? "selected" : ""; ?>>East</option>
And the right one will be selected
<select name="District">
<option value="East" <?php if($_SESSION['District'] == "East"):?>selected="selected"<?php endif; ?>>East</option>
... Repeat with all options ...
Use below code:
<select name="District">
<option value="East" <?php echo ($_SESSION['District'] == "East") ? "selected" : "" ?>>East</option>
<option value="West" <?php echo ($_SESSION['District'] == "West") ? "selected" : "" ?>>West</option>
<option value="North" <?php echo ($_SESSION['District'] == "North") ? "selected" : "" ?>>North</option>
<option value="South" <?php echo ($_SESSION['District'] == "South") ? "selected" : "" ?>>South</option>
</select>
<option value="East" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'East' ? 'selected="selected"' :'' ;?> >East</option>
<option value="West" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'West' ? 'selected="selected"' :'' ;?>>West</option>
<option value="North" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'North' ? 'selected="selected"' :'' ;?>>North</option>
<option value="South" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'South' ? 'selected="selected"' :'' ;?>>South</option>
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 try to make my dropdown selected only if error happen, and this is my script
<select name="usertype" id="usertype" class="form-control">
<option value="">Please choose the user right</option>
<option value="admin"<?php if(isset($error)
&& $_POST['usertype'] == 'admin' ? ' selected="selected"' : '');?>>
Admin
</option>
<option value="author"<?php if(isset($error)
&& $_POST['usertype'] == 'author' ? ' selected="selected"' : '');?>>
Author
</option>
<option value="public"<?php if(isset($error)
&& $_POST['usertype'] == 'public' ? ' selected="selected"' : '');?>>
Public
</option>
</select>
can anyone tell me the right way? because it doesn't work.
You're mixing up your ternary, its (condition) ? true : false. Here's a revised one:
<?php $usertype = array('admin', 'author', 'public'); ?>
<select name="usertype" id="usertype" class="form-control">
<option disabled selected>Please choose the user right</option>
<?php foreach($usertype as $val): ?>
<option
value="<?php echo $val; ?>"
<?php echo (isset($error, $_POST['usertype']) && $_POST['usertype'] == $val) ? 'selected="selected"' : ''; ?>
>
<?php echo ucfirst($val); ?>
</option>
<?php endforeach; ?>
</select>
try out this code:
<select name="usertype" id="usertype" class="form-control">
<option value="">Please choose the user right</option>
<option value="admin" <?php echo ((isset($error) && $_POST['usertype'] == 'admin') ? ' selected="selected"' : '');?>>Admin</option>
<option value="author" <?php echo ((isset($error)&& $_POST['usertype'] == 'author') ? ' selected="selected"' : '');?>>Author</option>
<option value="public" <?php echo ((isset($error) && $_POST['usertype'] == 'public') ? ' selected="selected"' : '');?>>Public</option>
</select>
I've used Selected="Selected" on SELECT that is fed by values I have written into the code, my query is how can I use it with a dynamic query from the MySQL db?
This is my written code
<label>Fuel</label>
<select tabindex="1" id="proptenure" name="proptenure">
<option value=""></option>
<option value="1" <?php echo ($searchfuel == '1' ? 'selected' : '')?>>Mains gas</option>
<option value="2" <?php echo ($searchfuel == '2' ? 'selected' : '')?>>Wood or coal fire</option>
<option value="3" <?php echo ($searchfuel == '3' ? 'selected' : '')?>>Oil</option>
<option value="4" <?php echo ($searchfuel == '4' ? 'selected' : '')?>>Electric storage heaters</option>
<option value="5" <?php echo ($searchfuel == '5' ? 'selected' : '')?>>LPG or bottled gas</option>
<option value="6" <?php echo ($searchfuel == '6' ? 'selected' : '')?>>No central heating system</option>
</select>
How can use the above type of line on my Query based SELECT
<?php echo ($searchtenure == '2' ? 'selected' : '')?>
This is an example of how I'm using SELECT from a query
<label>Fuel Type</label>
<?php $fueltype = db::getInstance()->query('SELECT * FROM lkup_fueltype');
if(!$fueltype->count()) {
echo 'Problem';
} else { ?>
<select tabindex="1" id="propertyfueltype" name="propertyfueltype">
<?php foreach ($fueltype->results() as $fueltype) { ?>
<option value="<?php echo $fueltype->PropertyFuelType; ?>"><?php echo $fueltype->PropertyFuelType; ?></option> <?php } } ?>
</select>
So how can I use the Selected="selected"?
This should do the job:
<label>Fuel Type</label>
<?php $fueltype = db::getInstance()->query('SELECT * FROM lkup_fueltype');
if(!$fueltype->count()) {
echo 'Problem';
} else { ?>
<select tabindex="1" id="propertyfueltype" name="propertyfueltype">
<?php foreach ($fueltype->results() as $fueltype) { ?>
<?php if($fueltype->PropertyFuelType == 2){ $selected = ' selected="selected"'; }else{ $selected = NULL; }?>
<option value="<?php echo $fueltype->PropertyFuelType; ?>"<?php echo $selected; ?>><?php echo $fueltype->PropertyFuelType; ?></option> <?php } } ?>
</select>
For multiple steps form I preserve entered value in <input> elements with this code:
<input name="city" type="text" value="<?php echo isset($_POST['city']) ? $_POST['city'] : '' ?>"/>
but I don't know how to implement it for <select> elements in order to preserve selected option.
Add "selected" to the desired option:
<select>
<option value="a" <?php echo ($_POST['select'] == 'a') ? 'selected' : ''; ?>>a</option>;
<option value="b" <?php echo ($_POST['select'] == 'b') ? 'selected' : '' ?>>b</option>
<option value="c" <?php echo ($_POST['select'] == 'c') ? 'selected' : ''; ?>>c</option>
</select>
Or, if you create your select with a loop, do it once for every options:
<?php $list = array('a', 'b', 'c'); ?>
<select>
<?php foreach($list as $value): ?>
<option value="<?php echo $value; ?>" <?php echo ($_POST['select'] == $value) ? 'selected' : ''; ?>><?php echo $value; ?></option>
<?php endforeach; ?>
</select>