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?
Related
I have a categories table where I have different categories. I have also add Other in the category. When I'm getting the categories to populate the select I want the other option to appear at last. how can I do that?
<select name="category" id="category" class="form-control <?php if (isset($errors['category'])) echo 'form-error'; ?>">
<option value="">Select a category...</option>
<?php while ($category = mysqli_fetch_assoc($categories)) { ?>
<option value="<?php echo h($category['id']); ?>" <?php if (isset($_POST['category']) && h($category['id']) === $_POST['category']) echo 'selected'; ?>><?php echo h($category['name']); ?></option>
<?php } ?>
</select>
If the Id of Other category is fixed, then you have to ignore it in your database query.
Let's say that the Id of that option is 1, our query will be like this:
select * from categories where Id <> 1
This query brings all categories except "other".
After that do the following:
<select name="category" id="category" class="form-control <?php if (isset($errors['category'])) echo 'form-error'; ?>">
<option value="">Select a category...</option>
<?php while ($category =
mysqli_fetch_assoc($categories)) { ?>
<option value="<?php echo h($category['id']); ?>" <?php
if (isset($_POST['category']) && h($category['id']) ===
$_POST['category']) echo 'selected'; ?>><?php echo
h($category['name']); ?></option>
<?php } ?>
<option value="1">Other</option>
</select>
hope it helps
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;
Here I am listing all cars.customers want to compare car so they will select from this drop down. A person can select multiple cars. At the first time he is selecting 'Audi' and Saab' I will store it into data base next if he came I need to populate Saab and audi as select how I can do this using php
<select name="cars" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
Here is my code
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<?PHP
if($carslist->num_rows() >0)
{
foreach($carslist->result_array() as $entry):
?> <option value="<?php echo($entry['ID']); ?>" ><?php echo($entry['car_name']); ?></option>
<?php
endforeach;
}
?>
</select>
Following code I tried $resources contain select cars
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<?PHP
if($carslist->num_rows() >0)
{
foreach($carslist->result_array() as $entry):
if($resources->num_rows() >0)
{
foreach($resources->result_array() as $car):
if($entry['ID'] == $employee['car_id'])
{
$select = 'selected="selected"';
}
else
{
$select = '';
}
endforeach;
}
?> <option value="<?php echo($entry['ID']); ?>" <?php echo $select;?> ><?php echo($entry['car_name']); ?></option>
<?php
endforeach;
}
?>
</select>
but it showing error
Here, try something like this, and see if it works:
Here is the controller:
<?php
function something(){
$data = array();
$data['cars'] = $this->some_model->some_function_to_return_cars_array();
$data['selected'] = $some_array_of_selected_cars();
$this->load->view('some_view', $data);
}
?>
And this is the view:
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<option value="">Select:</option>
<?php
foreach( $cars as $key => $val ){
?>
<option value="<?php echo $val['some_id'] ?>"
<?php
if( in_array( $val['some_id'], $selected ) ) echo ' selected';
?>
><?php echo $val['some_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>