Create a PHP Dropdown menu from a for loop? - php

I am trying to create a drop down menu with the options of 1,2,3 and 4.
The below code is what I am using just now and the dropdown is empty.
Any idea what I am doing wrong?
<select name="years">
<?php
for($i=1; $i<=4; $i++)
{
"<option value=".$i.">".$i."</option>";
}
?>
<option name="years"> </option>
</select>
<input type="submit" name="submitYears" value="Year" />

You are not outputting the option tags.
Try it like this:
<select name="years">
<?php
for($i=1; $i<=4; $i++)
{
echo "<option value=".$i.">".$i."</option>";
}
?>
<option name="years"> </option>
</select>
<input type="submit" name="submitYears" value="Year" />

You basically use html without closing the php syntax.Your code should look like this:
<select name="years">
<?php
for($i=1; $i<=4; $i++)
{
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
}
?>
<option name="years"> </option>
</select>
<input type="submit" name="submitYears" value="Year" />
Or are you trying to echo the option? In that case you forgot the echo statement:
echo "<option value= ".$i.">".$i."</option>";

This worked for me. It populates years as integers from the current year down to 1901:
<select Name='ddlSelectYear'>
<option value="">--- Select ---</option>
<?php
for ($x=date("Y"); $x>1900; $x--)
{
echo'<option value="'.$x.'">'.$x.'</option>';
}
?>
</select>

You forgot something..
Add print / echo before "<option value=".$i.">".$i."</option>";

place an echo in your loop to output your options.
echo "<option value=".$i.">".$i."</option>";

Just echo the <option> tag
echo "<option value=".$i.">".$i."</option>";

<select name="year">
<?php for ($i = 0; $i <= 9; $i++) : ?>
<option value='<?= $i; ?>' <?= $fetchData->year == $i ? 'selected' : '' ?>><?= $i; ?></option>
<?php endfor; ?>
</select>

Related

looping inside a select tag and echo selected value

I am trying to loop inside a select tag and output the quantity inorder to allow the user to select a quantity from the selection box , but the numbers are printed next to each other ,,how to solve that ? ,,and how to echo the selected item !?
<select name="quantity">
<div style="font-
size:13px;position:relative;left:10px;width:80px;height:20px;border-style:
hidden;background-color:rgba(0, 0, 0, 0.50);color:white;" > </div>
<option value="" disabled selected>Quantity </option>
<option value="quantity"><?php for($i=1;$i<=$row["item_quantity"];$i++)
{echo
$i."<br>";}?></option>
</select>
Instead of this:
<option value="quantity"><?php for($i=1;$i<=$row["item_quantity"];$i++)
{echo
$i."<br>";}?></option>
try this:
for($i=1;$i<=$row["item_quantity"];$i++)
{
// To make a option selected
$selected = '';
if( $i == 2 ) // 2 or any value
{ $selected = 'selected="selected"'; }
echo '<option value="'. $i .'" '.$selected.' >'. $i .'</option>'
}
Explanation: The issue is, your loop is inside the option tag, which is wrong. Put the option tag inside the loop.
Check your script you have add loop at wrong place
for($i=1;$i<=$row["item_quantity"];$i++)
{
echo '<option value="'. $i .'">'. $i .'</option>'
}
TRY THIS
<select name="quantity">
<option value="" selected>Quantity </option>
<?php for($i=1;$i<=$row["item_quantity"];$i++) { ?>
<option value="<?php echo $i ?>"><?php echo $i ?></option>
<?php } ?>
</select>

echo selected in chosen select

I am using chosen select drop down to show auto complete drop down. I want to set selected value for edit. I tried following code which works for normal select option but not working for chosen select
<select class="chosen-select" >
<option value=""></option>
<?php if(!empty($list))
{
foreach($list as $d)
{
?>
<option value="<?php echo $d->id; ?><?php if($d->id == 2) { echo "selected"; } ?>"><?php echo $d->name; ?></option>
<?php } } ?>
</select>
You are putting your selected inside your value attribute, you need to write it after :
<select class="chosen-select" >
<option value=""></option>
<?php if(!empty($list)) {
foreach($list as $d) {
?>
<option value="<?php echo $d->id; ?>"<?php if($d->id == 2) { echo " selected"; } ?>><?php echo $d->name; ?></option>
<?php } } ?>
</select>
Building on #roberto06's answer, the following should be a bit cleaner to look at.
BTW, you really should consider using a template engine.
<select class="chosen-select">
<option value=""></option>
<?php if (!empty($list)): ?>
<?php foreach ($list as $d): ?>
<option value="<?php echo $d->id; ?>" <?php echo ($d->id == 2) ? "selected" : "">
<?php echo $d->name; ?>
</option>
<?php endforeach; ?>
<?php endif; ?>
</select>

How to have the max value selected inside a select

I have the following:
PHP CODE:
<select name="valoracion<?php echo $i; ?>" id="valoracion<?php echo $i; ?>">
<?php
for($h=1; $h<6; $h++){
echo '<option value="'.$h.'">'.$h.'</option>';
}
?>
</select>
This prints:
<select name="valoracion0" id="valoracion0">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
When this <select> appears I want by default for the selected value to be the max number. How can I do this?
Like this..
<select name="valoracion<?php echo $i; ?>" id="valoracion<?php echo $i; ?>">
<?php
$cnt=6;$sel=""; // <--- Add a variable $sel with nothing assigned
for($h=1; $h<$cnt; $h++){
if($h==($cnt-1)) //<-- Here goes the check
{
$sel="Selected";
}
echo '<option '.$sel.' value="'.$h.'">'.$h.'</option>';
}
?>
</select>
You can try this,
<select name="valoracion<?php echo $i; ?>" id="valoracion<?php echo $i; ?>">
<?php
for($h=1; $h<6; $h++){
if($h == 5){
echo '<option value="'.$h.'" selected >'.$h.'</option>';
}else{
echo '<option value="'.$h.'">'.$h.'</option>';
}
}
?>
</select>
This should work:
<select name="valoracion<?php echo $i; ?>" id="valoracion<?php echo $i; ?>">
<?php
for($h=1; $h<6; $h++){
if($h == 5){ $selected = " selected=\"selected\""; }else{ $selected = NULL; }
echo '<option value="'.$h.'"'.$selected.'>'.$h.'</option>';
}
?>
</select>

All zero values passed in the table

When I execute this code, I only get zeroes inserted into my database. Why? What can I do to solve this problem?
<select class="date" name="year"> <option value="">Year</option>
<?php $i=1; while($i<=31) { ?>
<option value="<?php $i ?>"> <? echo $i; ?> </option>
<?php $i++; } ?> </select>
And this is my mysql code:
$sql="INSERT INTO information ( Year )
VALUES ('$_POST[year]')";
here how it will be your code
<select class="date" name="year">
<option value="">Year</option>
<?php for($i=1;$i<=31 ;$i++ ) { ?>
<option value="<?php $i ?>"> <? echo $i; ?> </option>
<?php } ?>
</select>
EDIT :
<input type ="submit" value =" submit me"/>
<?php if (isset($_POST['year'])) // check if isset the year
{
$year = $_POST['year'] ;
} else {}
$sql="INSERT INTO information ( Year ) VALUES ('".$year."')";
?>
edit 2 :
your code is right but you should escape value to prevent sql injection
do like that
insert into ....
VALUES
('mysql_real_escape_string($_POST[user])','mysql_real_escape_string($_POST[email])'.........
<select class="date" name="day"> <option value="">Day</option>
<?php $i=1; while($i<=31) { ?>
<option value="<?php echo $i; ?>"> <?php echo $i; ?> </option>
<?php $i++; } ?> </select>
Always use <?php and ?> so your browser recognizes PHP
also, with your first <?php $i ?>
I changed this to echo $i; and on the same line I changed <? echo $i; ?> </option> To <?php echo $i; ?> </option>
and it worked for me.
With Your Insert:
$sql="INSERT INTO information (COL_FOR_DAY)
VALUES ('{$_POST['day']}')";

How to make only current year appear once?

I am have this query below that is a drop down for year (from 1900-current). I am using a $variable to show a previously chosen year first in the drop down for an edit form. It works except that for all other years 2011 is also repeated with them. How do i tweak it so that $y shows first and then all years underneath that?
Code:
<select name="year">
<?PHP for($i=date("1900"); $i<=date("2011"); $i++)
if($year == $i)
echo "<option value='$y' selected>$y</option>
<option value='$i' selected>$i</option>";
else
echo "<option value='$y' selected>$y</option>
<option value='$i'>$i</option>";
?>
</select></td>
</tr>
Keep it simple:
<select name="year">
<?php for($i=1900; $i<=date('Y'); $i++) {
if($i == date('Y'))
echo "<option value='$i' selected='selected'>$i</option>";
else
echo "<option value='$i'>$i</option>";
}
?>
</select></td>
</tr>
Or even better:
<select name="year">
<?php for($i=1900; $i<=date('Y'); $i++)
echo "<option value='$i' ".(($i == date('Y')?'selected="selected"':'')).">$i</option>";
?>
</select></td>
</tr>
And backwards
<select name="year">
<?php for($i=date('Y'); $i>=1900; $i--)
echo "<option value='$i' ".(($i == date('Y')?'selected="selected"':'')).">$i</option>";
?>
</select></td>
</tr>
<select name="year">
<?PHP
echo "<option value='$year' selected>$year</option>
for($i=1900; $i<=2011; $i++)
if($year != $i)
echo "<option value='$i'>$i</option>";
<option value='$i'>$i</option>";
?>
</select></td>
</tr>
Not sure what $y is, but I guess $year is the current year:
<select name="year">
<?php
for($i=date("1900"); $i<=date("2011"); $i++)
{
if($year == $i)
echo '<option value="'.$i.'" selected="selected">'.$i.'</option>';
else
echo '<option value="'.$i.'">'.$i.'</option>';
}
?>
</select>

Categories