How to set selected item when looping through php array - php

I have an html dropdown box. Then I use an array to fill the items in it.
The keys in this for each loop is just a number from 0 - 9. My problem now is how can I control what shows up as the default choice:
<?php foreach($cat_r as $k=>$c){ ?>
<option name="<?php echo $k + 1; ?>" value="<?php echo $k + 1; ?>" selected="<?php if($k==1){ echo "selected"; } ?>"><?php echo $k + 1; ?></option>
<?php } ?>
In this code, you can see that I'm attempting to make the 2nd item as the default choice.
But it seems like I'm always brought to the last array item whatever number I type as the condition.

Try this:
<?php foreach($cat_r as $k=>$c){ ?>
<option value="<?php echo $k + 1; ?>" <?php if($k==1){ echo 'selected="selected"'; } ?>><?php echo $k + 1; ?></option>
<?php } ?>
Or, this format works too
<option value="foo" selected />

<?php foreach($cat_r as $k=>$c){ ?>
<option name="<?php echo $k + 1; ?>" value="<?php echo $k + 1; ?>"
<?php if($k==1){ echo "selected=\"selected\""; } ?>>
<?php echo $k + 1; ?>
</option>
<?php } ?>

I came here for finding solution to set options selected by user before as selected , above question was in different context and I ended up in scratching my head to this thing...
foreach($options as $k){
echo "<option value=".$k." ";
foreach($selectedoptions as $m){
if($k==$m)
echo "selected='selected'";
}
echo ">".$k."</option>";
}

This code was functional for me, thanks..
foreach($options as $k){
echo "<option value=".$k." ";
foreach($selectedoptions as $m){
if($k==$m)
echo "selected='selected'";
}
echo ">".$k."</option>";
}

Related

Select option value

I have a selection dropdown. Now what i want to accomplish is having a default first value (insted of an empty line in the pulldown menu).
The value needs to be "All manufacturers" and should be displayed always (like a placeholder.
If nothing is chosen, it will automatically search all the products of all manufacturers
<option <?php if(!isset($brand)) { echo 'selected="yes"' ; } ?> ></option>
<?php usort(
$manufacturers,
function($a, $b) {
return strcmp($a['name'], $b['name']);
}
)
?>
<?php foreach ($manufacturers as $manufacturer) { ?>
<?php if ($manufacturer['manufacturer_id'] == $manufacturer_id) { ?>
<option value="<?php echo $manufacturer['manufacturer_id']; ?>" selected="selected"><?php echo $manufacturer['name']; ?></option>
<?php } else { ?>
<option value="<?php echo $manufacturer['manufacturer_id']; ?>"><?php echo $manufacturer['name']; ?></option>
<?php } ?>
<?php } ?>
</select>
Any help appreciated!
If I understand you correctly I think you want to do this
<option value"0" <?php if(!isset($brand)) { echo 'selected="yes"' ; } ?> >All Manufacturers</option>

PHP getting value from a dynamic select list

I've got a dynamic select list, which is generated from MySQL. I've got no problem listing them, but I cant seem to get it's value when I submit the form. Here's my script for the Select:
<div class="form-group">
<select class="form-control" id="make" name="make">
<option value="">Make</option>
<?php
if ($result->num_rows > 0) {
$x = 1;
While($row = $result->fetch_assoc()) {
?>
<option value="<?php $row[fmake];?>" <?php if($_POST['make'] == $row[fmake]) echo 'selected="selected" '; ?>><?php echo $row[fmake];?></option>
<?php
$x = $x + 1;
}
}?>
</select>
</div>
And here's the script to get it's value:
if ($_POST["submit"]) {
$make = $_POST['make'];
when I do an echo for $make, I don't get anything at all. What went wrong? All help appreciated. Thanks
You forgot to echo $row['fmake'] and quotation marks mistake.
change this
<option value="<?php $row[fmake];?>" <?php if($_POST['make'] == $row[fmake]) echo 'selected="selected" '; ?>><?php echo $row[fmake];?></option>
by
<option value="<?php echo $row['fmake'];?>" <?php if($_POST['make'] == $row['fmake']) echo 'selected="selected" '; ?>><?php echo $row['fmake'];?></option>
shouldn't it be $row['fmake'] instead of $row[fmake]
<option value="<?php $row['fmake'];?>" <?php if($_POST['make'] == $row['fmake']) echo 'selected="selected" '; ?>><?php echo $row['fmake'];?></option>
Believe you're missing an echo.
value="<?php $row[fmake];?>"
Should be:
value="<?php echo $row[fmake];?>"

Php looping an array for hiding option in a `select`

I having trouble with my array for my select. The option value is base from $a. What do i need to add in my code so that if $a has an equal value from $b, option will be hidden.
My array
$a = array("1","2","3","4","5","6");
$b = array("2","4","6");
Php
<select>
<?php foreach ($a as $i) {?>
<option> value="<?php echo $i; ?>" >
<?php echo i?>
</option>
<?php } ?>
</select>
You can check array value exist by in_array and add hidden attribute to your elements if exist in array b
<select>
<?php
foreach ($a as $i) {
if(in_array($i,$b)){
echo '<option value="$i" hidden>';
}
else{
echo '<option value="$i">';
}
echo $i.' </option>';
}
?>
</select>
You can try this
<?php
$a = array("1","2","3","4","5","6");
$b = array("2","4","6"); ?>
<select>
<?php foreach ($a as $i) {
if (!in_array($i,$b)) {?>
<option value="<?php echo $i; ?>" >
<?php echo $i?>
</option>
<?php }} ?>
</select>
http://www.w3schools.com/php/func_array_in_array.asp
you just need to check if the values is not in $b array this way
$a = array("1","2","3","4","5","6");
$b = array("2","4","6");
Php
<select>
<?php foreach ($a as $i) {
if(!in_array($i,$b)){
?>
<option> value="<?php echo $i; ?>" >
<?php echo i?>
</option>
<?php }
} ?>
</select>
Try this -
<select>
<?php foreach ($a as $i) {
if(!in_array($i, $b)) {
?>
<option> value="<?php echo $i; ?>" >
<?php echo i?>
</option>
<?php }} ?>
</select>

Adding an "all" option on categories

This may seem like a basic question but I hope somebody could help me out.
I have a created a widget for a custom post type so that I could show the post types in a dynamic sidebar. In the widget, I have also created a dropdown that gets the categories to show.
My problem is that I also want to have the option to show all the categories and not just 1 category for the dropdown. Here's the code I have so far:
<select id="<?php echo $this->get_field_id('articleCategory'); ?>" name="<?php echo $this->get_field_name('articleCategory'); ?>">
<?php $arr = get_categories(); ?>
<?php foreach($arr as $option) { ?>
<option <?php echo $instance['articleCategory'] == $option->term_id ? 'selected="selected"' : '';?> value="<?php echo $option->term_id; ?>"><?php echo $option->name; ?></option>
<?php } ?>
</select>
Should I just use a multi-select for this and how do i go about it?
this will echo out all your categories but you'll have to update the selected part to match your instance.
<option value="<?php foreach($arr as $option) {echo $option->term_id.','; };?>"><?php echo $arr; ?>All Categories</option>
with your full code
<select id="<?php echo $this->get_field_id('articleCategory'); ?>" name="<?php echo $this->get_field_name('articleCategory'); ?>">
<?php $arr = get_categories(); ?>
<option value="<?php foreach($arr as $option) {echo $option->term_id.','; };?>"><?php echo $arr; ?>All Categories</option>
<?php foreach($arr as $option) { ?>
<option <?php echo $instance['articleCategory'] == $option->term_id ? 'selected="selected"' : '';?> value="<?php echo $option->term_id; ?>"><?php echo $option->name; ?></option>
<?php } ?>
</select>

Select box values in PHP MYSQL

I have a select box like this. This is nothing but looping from 1 to 10.
<select name="author_box[]" id="author_box[]">
<?php for($j=1;$j<=10;$j++){ ?>
<option id="<?php echo $j; ?>" name="<?php echo $j; ?>" value="<?php echo $$j; ?>"><?php echo $j; ?></option>
<?php } ?>
</select>
For example: I have a variable $tmp_rating="5". So what I want to display is showing the value 5 in the select box along with the other values (i.e. from 1 to 10).
How can I achieve that. In short, the value which I fetch from the database should be displayed first in the select box.
Thank you gentleman, for all the time and instant reply. This portal helps me a lot in fixing my issues in less time.
You have to use the selected attribute for the option:
<select name="author_box[]" id="author_box[]">
<?php for($j=1;$j<=10;$j++){ ?>
<option value="<?php echo $j; ?>" <?=($tmp_rating==$j)?'selected="selected"':null;?>><?php echo $j; ?></option>
<?php } ?>
</select>
This way, if $tmp_rating is the same as $j in your loop, the attribute selected is added and the correct value displays in your selectbox.
By adding this condition
<?php if($j == $tmp_rating) echo 'selected="selected"'; ?>
Full code
<option <?php if($j == $tmp_rating) echo 'selected="selected"'; ?> id="<?php echo $j; ?>" name="<?php echo $j; ?>" value="<?php echo $$j; ?>"><?php echo $j; ?></option>

Categories