I have a dropdown menu that need to be sorted alphabeticaly by name.
Here's the code
<?php foreach ($option['option_value'] as $option_value) { ?>
<option value="<?php echo $option_value['product_option_value_id']; ?>"><?php
echo $option_value['name'];
if ($option_value['price']) {
?>(<?php echo $option_value['price_prefix']; ?><?php echo $option_value['price']; ?>)<?php
}
?>
</option>
<?php
}
?>
I want the $option_value to be sorted by the $option_value['name']. How can I achieve this ?
Update
Using sort() or asort() before don't fix the problem, the order I get is
<option value="14598">Medium - red (0588390) [0] </option>
<option value="14599">large - WHITE (0592167) [1] </option>
<option value="14600">Medium - WHITE (0592168) [3] </option>
<option value="14601">large - THE ROYALS (0592169) [0] </option>
<option value="14602">Medium - THE ROYALS (0592170) [2] </option>
It sort by the value ID but I need to be sorted by it's name
Try this before your foreach:
function mysort($a, $b)
{
return strcmp($a['option_value']['name'], $b['option_value']['name']);
}
usort($option, 'mysort');
$option['option_value'] is the array containing option values and it needs to be sorted before the for...each:
function mysort($a, $b) {
return strcmp($a['name'], $b['name']);
}
usort($option['option_value'], 'mysort');
Then iterate just as before:
foreach ($option['option_value'] as $option_value) {
}
You can refer here
that is you can directly use
asort
function
asort($option['option_value']) or any other from options which ever you feel feasible to you.
asort will sort array based on first column entry i.e if your inner arrays first key is name it will help
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have 3 courses on my site, so when you click sign up you are going to the sign up page where I have one drop down menu and I need to show course which is selected. Now I did everything but I am using echo, and than I have problem because I have 2 same courses.
You will see in the code:
$myCourse = intval($_GET['course_id']);
if ($myCourse) { ?>
<select id='course' class='form-text'>
<?php
foreach ( $courses as $course ) {
if ( $myCourse == $course->id ):
?>
<option selected data-price="<?php print $course->price ?>"><?php print $course->post_name ?></option>
<?php
endif;
?>
<option data-price="<?php echo $course->price ?>"><?php echo $course->post_name ?></option>
<?php
}
?>
</select>
<?php
} else {
?>
<pre>
<select id='select-course' class='signup__form-text'>
<option selected disabled>-- Select Course --</option>
<?php
foreach ( $courses as $course ) {
?>
<option data-price="<?php echo $course->price ?>"><?php echo $course->post_name ?></option>
<?php
}
?>
</select>
<?php
}
return ob_get_clean();
SO on the spot where is "print" I need something to select that choice not to print it out. Because I echo my 3 courses later and now I add existing course.
So I need select instead of print.
Can someone give me some tip?
check this code
<select id='select-course' class='signup__form-text'>
<!-- If have id select it in drop down -->
<?php
foreach ( $courses as $course ) {
if ( $myCourse == $course->id ) { ?>
<option selected data-price="<?php echo $course->price ?>"><?php echo $course->post_name ?></option>
<?php } else { ?>
<option data-price="<?php echo $course->price ?>"><?php echo $course->post_name ?></option>
<?php } ?>
<?php } ?>
</select>
not tested
you can also use this code
<select id='select-course' class='signup__form-text'>
<?php
foreach ( $courses as $course ) { ?>
<option <?php if ( $myCourse == $course->id ) { ?> selected <?php } ?> data-price="<?php echo $course->price ?>"><?php echo $course->post_name ?></option>
<?php } ?>
</select>
also this is work
<select id='select-course' class='signup__form-text'>
<?php
foreach ($courses as $course) { ?>
<option <?php if ($myCourse == $course->id) {
echo 'selected="selected"';
} ?> data-price="<?php echo $course->price ?>"><?php echo $course->post_name ?></option>
<?php } ?>
</select>
Use ternary operators
<select>
<?php
foreach($courses as $course){
echo ($myCourse == $course->id ? "<option selected value='$course->price'</option>" : "<option value='$course->price'</option>"
}
?>
</select>
In my select box I am trying to do this:
Maincategory name
subcat
subcat
subcat
anothermain category
subcat
subcat
This is the code I have at the moment:
<select name="categories" class="form-control col-sm-12">
<?php foreach(ProductModel::categories() as $category): ?>
<option value="<?=System::escape($category->sub_category); ?>"><?=System::escape($category->sub_category); ?></option>
<?php endforeach; ?>
</select>
I don't want to show the main cate name on all the sub cats like
maincate:subcat
maincate:subcat
maincate:subcat
This is my table:
How would I go about doing this? I cant do if there's 5 inside the foreach show cat name because I'll never have the same amount of subcats in each main cats
If I'm understanding correctly, before your for each loop, you could assign a variable to the current main category name (starting it as null), then check it every time you iterate to see if it matches the current entry's main category. If it doesn't, have it close the previous optgroup and open a new optgroup for the main category and then another for the current sub category. Then assign the variable to the current main category value and you should be good to go. You'd probably have it skip the close tag if the variable was undefined (i.e., the first time 'round).
Something like this:
So:
<select name="categories" class="form-control col-sm-12">
<?php
$i = 0;
$mainCatName = '';
foreach(ProductModel::categories() as $category):
if($category->main_category != $mainCatName {
$mainCatName = $category->main_category;
if($i = 0) { ?>
<optgroup label="<?php echo $category->main_category ?>">
<?php } else {
</optgroup>
<optgroup label="<?php echo $category->main_category ?>">
<?php }
}
?>
<option value="<?=System::escape($category->sub_category); ?>"><?=System::escape($category->sub_category); ?></option>
<?php if($i = count(count(ProductModel::categories()) { ?>
</optgroup>
<?php }
$i++;?>
<?php endforeach; ?>
</select>
i have searched the forums but am not able to solve this problem:
i have a selection which displays Manufacturers names.
this is working correctly, but all names are not in sort-order.
this is my code:
<select name="manufacturer_id" id="manufacturer_id" data-inline = "true" style="width: 8.4em;">
<option <?php if(!isset($brand)) { echo 'selected="yes"' ; } ?> ></option>
<?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>
is there any way to sort the output? or can someone point me in the right direction?
thanks!
Your array is a 2-d array.... sort works on a 1-d array
Use
usort(
$manufacturers,
function($a, $b) {
return strcmp($a['name'], $b['name']);
}
)
I am using a script that runs on the YII framework...
I have this select box that lets a user view content based on their country. But right now, when a user selects their country, after the selection, it just shows the country name...
And what I want is, after a user selected their country... it must still show the select box, but have the selected country at the top of the list.
I have tried so many things but I'm still new to this this.
<?php if ( $_country == '' ) { ?>
<select onchange="changeCountry(this)" name="country" id="country">
<?php if ( count ($c) != 0 ) { ?>
<?php if ( $_country == NULL ) ?>
<?php foreach ($c as $id => $val) { ?>
<option<?php if ($id == $countryId) {?> selected="selected"<?php } ?> value="<?php echo mbCoreFunctions::to_seo_ad_name ($val, $id) ?>"><?php echo $val; ?></option>
<?php } ?>
<?php } ?>
</select>
<?php } else { ?>
<span class="countryName"><?php echo $countryName; ?></span>
<?php } ?>
I didn't try but following should work
<select onchange="changeCountry(this)" name="country" id="country">
<?php foreach ($c as $id => $val) { ?>
<option<?php if ($id == $countryId) {?> selected="selected"<?php } ?> value="<?php echo mbCoreFunctions::to_seo_ad_name ($val, $id) ?>"><?php echo $val; ?></option>
<?php } ?>
</select>
I have an array of all options that will be in a select box:
Array
(
[2836] => 4 16:40:00
[2835] => 3 13:20:00
)
There is a second array of options that have a particular flag in the DB:
Array
(
[2835] => 3 13:20:00
)
How can I compare these two arrays to generate a select list that applies a particular class to the matches found in the arrays? I'd love some help thanks!
The view for my select list which receives the first array:
<select>
<?php foreach ($courses as $key=>$course): ?>
<option id="<?php echo $key;?>">
<?php echo $course; ?>
</option>
<?php endforeach;?>
</select>
You can use array_intersect to find those values common for both arrays. Then in your foreach loop you can check if the current key (or value) is from matches array.
$matches = array_intersect($arr1, $arr2);
In the view file:
<select>
<?php foreach ($courses as $key=>$course): ?>
<option id="<?php echo $key;?>" class="<?php echo isset($matches[$key]) ? 'match' : '' ?>">
<?php echo $course; ?>
</option>
<?php endforeach;?>
</select>