Highlighting multiple selections on a form after submitting - php

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>

Related

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>

PHP Adding an option to sort by date

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?

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>

Retrive select option from database php

The below code show the retrived data from database and it also repeat the same option
For example: if you select employed from dropdown, it repeat the same employed twice in dropdown
<select class="txtbox" name="currentstatus">
<option value="<?php echo "$currentstatus"; ?>"><?php echo "$currentstatus"; ?></option>
<option value="Employed"<?php if(isset($_POST["currentstatus"]) && $_POST["currentstatus"] == "Employed") echo "selected"; ?>>Employed</option>
<option value="Unemployed"<?php if(isset($_POST["currentstatus"]) && $_POST["currentstatus"] == "Unemployed") echo "selected"; ?>>Unemployed</option>
</select>
What about this? This displays two options and selects one according to the database or by $_POST:
<select class="txtbox" name="currentstatus">
<?php foreach (array('Employed', 'Unemployed') as $status): ?>
<option value="<?php echo $status; ?>"<?php if ($currentstatus == $status || (isset($_POST["currentstatus"]) && $_POST["currentstatus"] == $status)) echo ' selected'?>><?php echo $status; ?></option>
<?php endforeach; ?>
</select>

Change selected value of <select> after it's created (via PHP) IF there's a value on the URL

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>

Categories