I'm a absolute newby for php and i'm struggling with just a basic thing.
I want to set a default value for a select box i have. Default value should be 2
My code
<div class="selector-wrapper">
<select id="adults" class="form-control" onchange="toggleSetGuests()">
<?php
for($i = 0; $i <= 20; $i++):
?>
<option value="<?php echo $i ?>" <?php if($i == 0){ echo('selected="true"'); } ?>>
<?php echo $i ?> Adults
</option>
<?php
endfor;
?>
</select>
</div>
So in this select box i get options from 0 - 20. I want to set the default value as 2 instead of 0
My approach was like below
<option value="<?php echo $i ?>" <?php if($i == 2){ echo('selected="true"'); } ?>>
But did not work.
How do i set the default value to be selected as the value 2
<option value="<?php echo $i ?>" <?php if($i == 2){ echo ' selected'; } ?>>
should work.You don't need selected=true
Here is the code (HTML / PHP) :
<div class="col-sm-6">
<div class="form-group">
<select id="horseSize" name="horseSize" class="form-control" >
<option value="<?php echo htmlspecialchars($horseSize); ?>" selected><?php echo htmlspecialchars($horseSize); ?> m</option>
<?php
$i = 0.50;
while($i <= 2.00){
if ($i != $horseSize){
?>
<option value="<?php echo htmlspecialchars(number_format($i, 2)) ; ?>"><?php echo htmlspecialchars(number_format($i, 2)) ; ?> m</option>
<?php
$i = $i + 0.01;
}
}
?>
</select>
</div>
</div>
$horseSize comes from a SQL query and is working fine. Th problem is in displaying webpage.
When i am trying to display this webpage, my website goes down because of gateway giving no answer. This triggers only in one situation : if the variable value of $horseSize is inferior to 1. As if it was not possible to display something like 0,....
Maybe I did something wrong in this code, but I tried many times to fix, I couldn't reach it.
you are incrementing $i only under if condition, so value like 0.50 it will always never increase. Also floating point comparison will not work. please read: Floating point numbers
<div class="col-sm-6">
<div class="form-group">
<select id="horseSize" name="horseSize" class="form-control" >
<option value="<?php echo htmlspecialchars($horseSize); ?>" selected><?php echo htmlspecialchars($horseSize); ?> m</option>
<?php
$i = 0.50;
$precision = 0.00001;
while($i <= 2.00){
if(abs($i-$horseSize) > $precision) {
?>
<option value="<?php echo htmlspecialchars(number_format($i, 2)) ; ?>"><?php echo htmlspecialchars(number_format($i, 2)) ; ?> m</option>
<?php
$i = $i + 0.01;
} else {
$i = $i + 0.01;
}
}
?>
</select>
</div>
</div>
Why preg_all_match returning two results? Where is the problem? Thanks for your helps.
<?php
$site=file_get_contents("http://www.site.gen.tr");
preg_match_all('#<div class=\'search\'>(.*?)<\/div>#si',$site,$baslik); ?>
<select name="sub_category" id="sub_category_id">
<option value="" selected="selected">Bir Sey Seçin</option>
<?php for($i=0; $i < count($baslik); $i++) { ?>
<option value="<?php echo $baslik[0][$i];?>"><?php echo $baslik[0][$i]; ?></option>
<?php } ?>
</select>
Change your for loop to:
for($i=0; $i < count($baslik[0]); $i++) {
My Drop down menu to choose a date does not work unless I hold down left click on my mouse.
Only the year drop down works correctly but the month and days only work if you hold your mouse button down.
Here is the website it is on: www.cipslimoshuttle.com/tickets
Here is the code:
<label>
Date of travel:
<select name="ticketYear">
<?php
$i = date('Y');
while($i <= date('Y')+5){
?>
<option <?php if(date('Y') == $i) echo 'selected' ?> value="<?php echo $i ?>"><?php echo $i ?></option>
<?php
$i++;
}
?>
</select>
<select name="ticketDay">
<?php
$i = 1;
while($i <= 31){
?>
<option <?php if(date('j') == $i) echo 'selected' ?> value="<?php echo $i ?>"><?php echo $i ?></option>
<?php
$i++;
}
?>
</select>
<select name="ticketMonth">
<?php
$months = array(
'01-January',
'02-February',
'03-March',
'04-April',
'05-May',
'06-June',
'07-July',
'08-August',
'09-September',
'10-October',
'11-November',
'12-December'
);
foreach($months as $month) {
$month = explode('-',$month);
?>
<option <?php if(date('m') == $month[0]) echo 'selected' ?> value="<?php echo $month[0] ?>"><?php echo $month[1] ?></option>
<?php
}
?>
</select>
</label>
The problem is with your label tag You need to end before the select box.Check this it will work
<label>
Date of travel: </label>
<select name="ticketYear">
<?php
$i = date('Y');
while($i <= date('Y')+5){
?>
<option <?php if(date('Y') == $i) echo 'selected' ?> value="<?php echo $i ?>"><?php echo $i ?></option>
<?php
$i++;
}
?>
</select>
<select name="ticketDay">
<?php
$i = 1;
while($i <= 31){
?>
<option <?php if(date('j') == $i) echo 'selected' ?> value="<?php echo $i ?>"><?php echo $i ?></option>
<?php
$i++;
}
?>
</select>
<select name="ticketMonth">
<?php
$months = array(
'01-January',
'02-February',
'03-March',
'04-April',
'05-May',
'06-June',
'07-July',
'08-August',
'09-September',
'10-October',
'11-November',
'12-December'
);
foreach($months as $month) {
$month = explode('-',$month);
?>
<option <?php if(date('m') == $month[0]) echo 'selected' ?> value="<?php echo $month[0] ?>"><?php echo $month[1] ?></option>
<?php
}
?>
</select>
How to make the current month selected by default in option select using php
Here is what i have tried so far.
$curmonth = date("F");
And to display the entire month
<select>
<?php
for($i = 1 ; $i <= 12; $i++)
{
$allmonth = date("F",mktime(0,0,0,$i,1,date("Y")))
?>
<option value="<?php
echo $i;
if($curmonth==$allmonth)
{
echo 'selected';
}
?>"
>
<?php
echo date("F",mktime(0,0,0,$i,1,date("Y")));
}
?>
</option>
And according to the above code, I am assigning the current month as $curmonth, and inside loop assigning the $allmonth for entire, month.
And inside the Value
<option value="<?php
echo $i;
if($curmonth==$allmonth)
{
echo 'selected';
}
?>"
>
<?php
echo date("F",mktime(0,0,0,$i,1,date("Y")));
}
?>
</option>
for checking if the current month equals all month and displaying the selected to make it select. But i am not getting result.. What i am getting is all the items are being displayed in the option select.
What i am missing ?
What you have missed is,
You are trying to display the selected inside the value
What you need to do is
<option value="<?php
echo $i;
?>"
<?php
if($allmonth==$curmonth)
{
echo ' selected';
}
?>
>
<?php
echo $allmonth;
}
?>
</option>
So, the result will be
<select >
<option value="1">
January<option value="2">
February<option value="3">
March<option value="4">
April<option value="5">
May<option value="6">
June<option value="7">
July<option value="8">
August<option value="9" selected>
September<option value="10">
October<option value="11">
November<option value="12">
December</option>
You're not correctly closing the <option> tags you are creating. Indenting you're code makes these issues more apparent:
<select>
<?php
for($i = 1 ; $i <= 12; $i++)
{
$allmonth = date("F",mktime(0,0,0,$i,1,date("Y")))
?>
<option value="<?php
echo $i;
if($curmonth==$allmonth)
{
echo 'selected';
}
?>"
>
<?php
echo date("F",mktime(0,0,0,$i,1,date("Y")));
//Close tag inside loop
?>
</option>
<?php
}
Hope you are wrong here
<option value="<?php
echo $i; ?>"
<?php
if($curmonth==$allmonth)
{
echo 'selected';
}
?>"
>
<?php
echo date("F",mktime(0,0,0,$i,1,date("Y")));
}
?>
</option>
You have missed closing quotes for the value
Take a look on this example:
$current = date('F');
for($i = 1 ; $i <= 12; $i++) {
$month = date("F",mktime(0,0,0,$i,1,date("Y")));
if( $current == $month )
echo $month . " - Selected \r\n";
else
echo $month . "\r\n";
}
IN HTML FORMAT:
echo "<select>";
$current = date('F');
for($i = 1 ; $i <= 12; $i++) {
$month = date("F",mktime(0,0,0,$i,1,date("Y")));
if( $current == $month )
echo "<option value='$month' selected='selected'>" $month . "</option>";
else
echo "<option value='$month'>" $month . "</option>";
}
echo "</select>";
DEMO
You have error with closing quotes in the value of options in the line
<option value="<?php
echo $i; if($curmonth==$allmonth)
{
echo 'selected';
}
?>"
^ // This is the quotes opened for value and is not properly closed
>
Here you have to close the quotes. As per your code it will display as:
<option value="9 selected" >
Also you are not closing <option> properly. Change your code to :
<select>
<?php
for($i = 1 ; $i <= 12; $i++)
{
$allmonth = date("F",mktime(0,0,0,$i,1,date("Y")))
?>
<option value="<?php echo $i;?>" <?php if($curmonth==$allmonth){echo 'selected';}?> >
^ // close quotes here
<?php
echo $allmonth;?>
</option>
}
</select>