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>
Related
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>
I'm using a date range on a web page to aggregate MySQL data and present it based on the selected time frame. For some reason the values for each option in the drop-down menus are not displaying. Here's the PHP I'm using:
<select name="date1" title="<?=$date1 ?>">
<?php foreach($availableDates as $date) { ?>
<option value="<?=$date ?>"<?php if($date == $date1) { ?> selected="selected"<?php } ?><?=$date ?></option>
<?php } ?>
</select>
And here's the HTML output:
<option value="2015-01-03" selected="selected" 2015-01-03<="" option=""></option>
The weirdest part is this was working for the longest time and suddenly the dates in both menus vanished. Any ideas why?
This happened becuase you are missing the ending > of tag
Modified code:
<select name="date1" title="<?=$date1 ?>">
<?php foreach($availableDates as $date) { ?>
<option value="<?=$date ?>"<?php if($date == $date1) { ?> selected="selected"<?php } ?>>
<?=$date ?>
</option>
<?php } ?>
</select>
Your PHP snippet is missing a closing >:
<select name="date1" title="<?=$date1 ?>">
<?php foreach($availableDates as $date) { ?>
<option
value="<?=$date ?>"
<?php if($date == $date1) { ?> selected="selected"<?php } ?>
>
<?=$date ?>
</option>
<?php } ?>
</select>
Try this way also :
<select name="per1" id="per1">
<option selected="selected">Choose one</option>
<?php
foreach($names as $name) { ?>
<option value="<?= $name['name'] ?>"><?= $name['name'] ?></option>
<?php
} ?>
</select>
Now you can put your code here.
1. Add > tag closer after selected attribute
2. Remove shorthanded <?= ?> tag which is unwanted while you also using <?php ?>
<select name="date1" title="<?php print $date1; ?>">
<?php foreach($availableDates as $date) {
?><option value="<?php print $date; ?>"<?php if($date == $date1) { ?> selected="selected"<?php } ?>>
<?php print $date; ?>
</option>
<?php } ?>
</select>
I suggest this less error prone code.
$selHTML = '<select name="date1" title="'.$date1.'">';
foreach($availableDates as $date) {
$sel = ($date == $date1)?" selected":"";
$selHTML .= '<option value="'.$date.'"'.$sel.'>'.$date.'</option>';
}
$selHTML .= '</select>';
echo $selHTML;
I have a listing site in wordpress where the irems are automatically sorted by date posted (newest first) when you go to the search results page. There are already options to sort by price (high to low, low to high) but I ned to add the option to sort by date (newest first, oldest first)
The developer of theme told me the files i need to modify but wouldn't give me any help other than that.
I decided to try duplicating the code for sort by price and changing "price" to "date"
Here is the code i added into "Search-listings.php"
/*———————————————————————————–*/
/* Sort By date posted */
/*———————————————————————————–*/
function sort_by_date() {
$extraVars = “”;
foreach($_GET AS $key=>$value) {
$extraVars .= ”;
} ?>
<form action="” name=”order “class=”formsrch” method=”get”>
<option value="DESC" selected=”selected” >
<option value="ASC" selected=”selected” >
<?php }
and here is the code i added to the "theme-functions.php"
/*-----------------------------------------------------------------------------------*/
/* Sort By date posted */
/*-----------------------------------------------------------------------------------*/
function sort_by_date() {
$extraVars = "";
foreach($_GET AS $key=>$value) {
$extraVars .= '<input type="hidden" name="'.$key.'" value="'.$value.'" />';
} ?>
<form action="<?php get_site_url(); ?>" name="order "class="formsrch" method="get">
<?php echo $extraVars;?>
<select class="ct_orderby_price" id="ct_orderby_date" name="ct_orderby_date">
<option value=""><?php _e('Sort By', 'contempo'); ?></option>
<option value="DESC" <?php if($_GET['ct_orderby_date'] == 'DESC'){ ?> selected="selected" <?php } ?>><?php _e('Date - Newest', 'contempo'); ?></option>
<option value="ASC" <?php if($_GET['ct_orderby_date'] == 'ASC'){ ?> selected="selected" <?php } ?>><?php _e('Date - Oldest', 'contempo'); ?></option>
</select>
</form>
<?php }
Unsurprisingly that didn't work, I'm a beginner at php so any advice
would be appreciated
OK, so i've figured out i need to add the code to the existing function like so
/*-----------------------------------------------------------------------------------*/
/* Sort By Price */
/*-----------------------------------------------------------------------------------*/
function sort_by_price() {
$extraVars = "";
foreach($_GET AS $key=>$value) {
$extraVars .= '<input type="hidden" name="'.$key.'" value="'.$value.'" />';
} ?>
<form action="<?php get_site_url(); ?>" name="order "class="formsrch" method="get">
<?php echo $extraVars;?>
<select class="ct_orderby_price" id="ct_orderby_price" name="ct_orderby_price">
<option value=""><?php _e('Sort By', 'contempo'); ?></option>
<option value="DESC" <?php if($_GET['ct_orderby_price'] == 'DESC'){ ?> selected="selected" <?php } ?>><?php _e('Price - Highest', 'contempo'); ?></option>
<option value="ASC" <?php if($_GET['ct_orderby_price'] == 'ASC'){ ?> selected="selected" <?php } ?>><?php _e('Price - Lowest', 'contempo'); ?></option>
<option value="" <?php if($_GET['the_date'] == 'ASC'){ ?> selected="selected" <?php } ?>><?php _e('Date - Newest First', 'contempo'); ?></option>
</select>
</select>
</form>
<?php }
I still can't make the "order by date" work as i don't know what to put in in place of "ct_orderby_price" and if I use the ASC or the DESC option values it just jumps to the price options?
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>
This select box below does remember and highlight -one- selection after submitting the form. But when i make it multiple, it doesn't highlight any of the selectionS after submitting.
Any idea about how to achieve this?
Thanks in advance.
<?php
$options_amount = array("0","1","2","3","4","5","6","7","8","9","10+");
$no_way = $_GET['no_way'];
?>
<select class="postform" name="no_way[]" multiple size="5">
<option <?php if ($no_way == 'all') { ?>selected="selected"<?php }?> value="all">Any</option>
<?php
foreach ($options_amount as $option) {
?><option <?php if ($no_way == $option) { ?>selected="selected"<?php }?> value="<?php echo $option; ?>"><?php echo $option; ?></option><?php }?>
</select>
$_GET['no_way'] only handles single parameters you have to use $_GET['no_way[]'] and in_array($option, $no_way)
This works for me:
<?php
$options_amount = array("0","1","2","3","4","5","6","7","8","9","10+");
$no_way = $_GET['no_way'];
?>
<select class="postform" name="no_way[]" multiple size="5">
<option <?php if (in_array("all",$no_way)) { ?>selected="selected"<?php }?> value="all">Any</option>
<?php
foreach ($options_amount as $option) {
?><option <?php if (in_array($option,$no_way)) { ?>selected="selected"<?php }?> value="<?php echo $option; ?>"><?php echo $option; ?></option><?php }?>
</select>
I'm not sure if this will help or not, but is there any chance you've tried just using selected instead of selected="selected"?
<option <?php if ($no_way == $option) { ?> selected<?php }?> value="<?php echo $option; ?>"><?php echo $option; ?></option>