Consider the following command:
echo '<option value="', $row['name'], '">', $row['name'], '</option>';
Now how do I add the following code into the code I'm doing does not work either.
if ($row['name'] =='class2' )echo " selected ";
Try
if ($row['name'] =='class2' ){echo "selected=\"selected\"";}
edit: I hope I got your point right with that less informations...
just to put it right for you:
<select>
<option
<?
if ($row['name'] =='class2' )
{echo "selected=\"selected\"";}
?> value="<?=$row[name]?>">
<?=$row[name]?>
</option>
</select>
Do you mean something like this ?
<option <?php echo $row['name']=='class2'?"selected='selected'":""; ?>
value="<?php echo $row['name'] ?>">
<?php echo $row['name'] ?>
</option>
Related
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>
I am trying to display two seperate items on the same line in a HTML form using a PHP loop to auto populate it. This is the PHP/HTML form:
<select class="form-control" name="list" title="pick a type">
<?php if(count($postOpts)) { ?>
<?php foreach($postOpts as $row) { ?>
<option value="<?= $row["name"] ?>"><?= $row["price"] ?></option>
<?php } ?>
<?php } else { ?>
<option value="<?= $post->getDefaultPrice() ?>"><?= "Default Shipping Option - $".$post->getDefaultPrice() ?></option>
<?php } ?>
</select>
I can have either the price or the name displaying, but not both on the same line.
Can anyone help?
May be try this,
<option value="<?= $row["name"] ?>"><?= $row["name"] ."( $".$row["price"].")" ?></option>
check the argument on here
if(count($postOpts)) {
should be
empty($postOpts)
<select class="form-control" name="list" title="pick a type">
<?php
if(empty($postOpts)) {
foreach($postOpts as $row)
{
?>
<option value="<?php echo $row["name"] ?>">
<?php echo $row["price"] ?>
</option>
<?php
}
} else {
?>
<option value="<?php echo $post->getDefaultPrice() ?>">
<?php echo "Default Shipping Option - $".$post->getDefaultPrice() ?>
</option>
<?php
}
?>
</select>
Replace <option value="<?= $row["name"] ?>"><?= $row["price"] ?></option> with <option value="<?= $row['name'] ?>"><?= $row['price'] ?></option> and let me know what you are getting now ?
You need to use single quote inside double quote or vice versa.
To make dropdown ticked while fetching dynamically from db.Fetch all values from db and list as dropdown.If the values from db matches particular value make that dropdown selected
<select id="designation" name="designation">
<? while($role=mysql_fetch_array($sql_role)){
if ($role['role'] == $desig ) echo $selected="selected"; else $selected=" ";?>
<option <?=$selected?>>Select</option>
<option value="<?=$role['id']?>"
<?=$selected?> >
<?=$role['role']?>
</option>
<? } ?>
</select>
You are echoing $selected in your check. I also recommend not to use php short tags because it may be deprecated in future PHP versions.
I recommend this code:
$html = '<select id="designation" name="designation">';
$html .= '<option>Select</option>';
while($role = mysql_fetch_array($sql_role))
{
$selected = ($role['role'] == $desig) ? ' selected' : '';
$html .= '<option value="'. $role['id'] .'"'. $selected .'>'. $role['role'] .'</option>';
}
$html .= '</select>';
echo $html;
Don not forget to assign $desig ;-).
Why don't you make the following changes and try. You can totally eliminate the intermediate variable storage. Just print selected if the condition matches. And also you should just put that default select option outside the while loop.
<select id="designation" name="designation">
<option>Select</option>
<? while($role=mysql_fetch_array($sql_role)){ ?>
<option value="<?=$role['id']?>" <? if ($role['role'] == $desig ) echo "selected='selected'"?> ><?=$role['role']?></option>
<? } ?>
</select>
try to avoid short scripts in php like this
<select id="designation" name="designation">
<? while($role=mysql_fetch_array($sql_role)){
<option <?php if ($role['role'] == $desig ) { ?> selected <?php } ?> ><?php echo $role['role']; ?></option>
<? } ?></select>
I have list of items in my dropdown. Now I wish to select item according to database value. Suppose I have list of cities their are nearly 300 cities. I have city value as new jersey in my record. Now while editing form checking each options is not possible for 300 options. Like
<option value="A" <? if ($a="A") echo "selected"; ?> >A</option>
<option value="B" <? if ($a="B") echo "selected"; ?> >B</option>
<option value="C" <? if ($a="C") echo "selected"; ?> >C</option>
<option value="D" <? if ($a="D") echo "selected"; ?> >D</option>
<option value="E" <? if ($a="E") echo "selected"; ?> >E</option>
<option value="F" <? if ($a="F") echo "selected"; ?> >F</option>
<option value="G" <? if ($a="G") echo "selected"; ?> >G</option>
<option value="H" <? if ($a="H") echo "selected"; ?> >H</option>
Isn't any way to minimise for 300 records. Can't I write short code for this?
** NOTE My options values are not stored in array.**
suppose your values are stored in an array called $options, you could write something like this:
foreach($options as $option)
{
echo "<option value=\"$option\"";
if($option == $a)
{
echo ' selected="selected"';
}
echo ">$option</option>";
}
you should use loops for output
like
$result = mysql_query('select id, city from cities');
while($row = mysql_fetch_assoc($result)) {
echo '<option value=' . $row['id'] . (($row['city'] == 'new jersey') ? 'selected' : '') . " >" . $row['city'] . '</option>';
}
Try range function in php:
<?php foreach (range('A', 'Z') as $letter){?>
<option value="<?php echo $letter?>" <? if ($a==$letter) echo "selected='selected'"; ?> ><?php echo $letter?></option>
<?php }?>
Change the range accordingly. You could use this function with integers too.
I am creating a like this:
<select name="id" id="id">
<OPTION VALUE="null">Select<OPTION>
<? foreach ($genres as $row){ ?>
<OPTION VALUE="<? echo $row->id; ?>"><? echo utf8_encode($row->name); ?></OPTION>
<?
}
?>
</select>
I am using the $_GET to check for a value on the URL and then just take it and with that value I have to pre-select the option in the select menu, any ideas how to do this? I'm unsure of how to do it via javascript (or even if it would be more complicated).
just use
selected="selected"
on your pre-selected option
<select name="id" id="id">
<OPTION VALUE="null">Select<OPTION>
<? foreach ($genres as $row){ ?>
$selected = $_GET['your_var'] == $row->id ? 'selected="selected"' : false;
<OPTION <? echo $selected; ?> VALUE="<? echo $row->id; ?>"><? echo utf8_encode($row->name); ?></OPTION>
<?
}
?>
</select>