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>
Related
I have a select box, which allows you to navigate between parent pages and their children, they are given a specific order, in order to display them in a certain way on the website.
I believe that the line of code that interest you is perhaps this one
$children = get_pages("title_li=&child_of=" . $parent . "&echo=0");
The full code is below. You can see that the order of the pages on the site is different than the order of the pages in the select menu, that is because wordpress sorts per order, my pages are sorted alphabetically. How to sort them by order, so that what you see on the website matches the select box?
<?php
// determine parent of current page
if ($post->post_parent)
{
$ancestors = get_post_ancestors($post->ID);
$parent = $ancestors[count($ancestors) - 1];
}
else
{
$parent = $post->ID;
}
$children = get_pages("title_li=&child_of=" . $parent . "&echo=0");
?>
<div id="info-select-wrap" class="single-france-select">
<select id="info-select"class="fr-select" >
<?php
if (get_the_ID() == $parent->ID): ?>
<option value="<?php
echo get_the_permalink($parent->ID); ?>" selected> <?php
echo get_the_title($parent->ID); ?></option>
<?php
else: ?>
<option value="<?php
echo get_the_permalink($parent); ?>"> <?php
echo get_the_title($parent); ?></option>
<?php
endif;
foreach($children as $child):
if (has_children($child))
{
if (get_the_ID() == $child->ID)
{ ?>
<option value="<?php
echo get_the_permalink($child->ID); ?>" selected> <?php
echo get_the_title($child->ID); ?></option>
<?php
}
else
{ ?>
<option value="<?php
echo get_the_permalink($child->ID); ?>"> <?php
echo get_the_title($child->ID); ?></option>
<?php
}
}
else
{
if (get_the_ID() == $child->ID)
{ ?>
<option value="<?php
echo get_the_permalink($child->ID); ?>" selected> <?php
echo get_the_title($child->ID); ?></option>
<?php
}
else
{ ?>
<option value="<?php
echo get_the_permalink($child->ID); ?>"> <?php
echo get_the_title($child->ID); ?></option>
<?php
}
}
endforeach; ?>
</select>
</div>
I tried
$children = get_pages("title_li=&child_of=" . $parent . "&echo=0&orderby='menu_order'&order='ASC'");
Didn't solve it.
Issue solved by adding sort_column=menu_order
$children = get_pages("title_li=&child_of=" . $parent . "&echo=0&sort_column=menu_order");
Just a quick note, &echo=0 is useless, you can remove it.
I am using Opencart 2.2.0 on Journal theme. I am using super filter to display attributes. The problem is - this filter only displays as check box. I need it to display as drop down. The theme maker told me that module only shows in check box option, but I am wondering if I can make it display as drop down instead. The code for super_filter_attributes.tpl is:
<div class="box sf-attribute sf-attribute-<?php echo $attribute['attribute_id']; ?> sf-<?php echo $attribute['type']; ?>">
<div class="box-heading"><?php echo $attribute['attribute_name']; ?></div>
<div class="box-content">
<ul class="<?php echo $this->journal2->settings->get('filter_show_box') ? '' : 'hide-checkbox'; ?>">
<?php foreach ($attribute['values'] as $value) { ?>
<li><label><input data-keyword="<?php echo $value['keyword']?>" type="checkbox" name="attribute[<?php echo $attribute['attribute_id']?>]" value="<?php echo $value['text']; ?>"><?php echo $value['name']; ?></label></li>
<?php } ?>
</ul>
</div>
I edited a bit and now my code looks like this:
<div class="box sf-attribute sf-attribute-<?php echo $attribute['attribute_id']; ?> sf-<?php echo $attribute['type']; ?>">
<div class="box-heading"><?php echo $attribute['attribute_name']; ?></div>
<div class="box-content">
<ul class="<?php echo $this->journal2->settings->get('filter_show_box') ? '' : 'hide-checkbox'; ?>">
<div class="box sf-attribute sf-attribute-<?php echo $attribute['attribute_id']; ?> sf-<?php echo $attribute['type']; ?>">
<div class="box-heading"><?php echo $attribute['attribute_name']; ?></div>
<div class="box-content">
<ul class="<?php echo $this->journal2->settings->get('filter_show_box') ? '' : 'hide-checkbox'; ?>">
<?php foreach ($attribute['values'] as $value) { ?>
<select>
<option><?php echo $value['name']; ?></option>
<?php foreach ($value['filter'] as $filter) { ?>
<?php if (in_array($filter['filter_id'], $filter_category)) { ?>
<option value="<?php echo $filter['filter_id']; ?>" id="filter<?php echo $filter['filter_id']; ?>" selected>
<?php echo $filter['name']; ?>
</option>
<?php } else { ?>
<option value="<?php echo $filter['filter_id']; ?>" id="filter<?php echo $filter['filter_id']; ?>">
<?php echo $filter['name']; ?>
</option>
<?php } ?>
<?php } ?>
</select>
<?php } ?>
</ul>
</div>
I am not doing everything good, because I can see only partial drop down now, and all is mixed up.
Any suggestions to edit my code? Thank you all in advance!
Put select tag outside foreach. This may help.
<select> <?php foreach ($attribute['values'] as $value) { ?>
<option><?php echo $value['name']; ?></option>
<?php foreach ($value['filter'] as $filter) { ?>
<?php if (in_array($filter['filter_id'], $filter_category)) { ?>
<option value="<?php echo $filter['filter_id']; ?>" id="filter<?php echo $filter['filter_id']; ?>" selected>
<?php echo $filter['name']; ?>
</option>
<?php } else { ?>
<option value="<?php echo $filter['filter_id']; ?>" id="filter<?php echo $filter['filter_id']; ?>">
<?php echo $filter['name']; ?>
</option>
<?php } ?>
<?php } ?>
<?php } ?>
</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']);
}
)
Hi I'm trying to create a function to show a list of posts in my admin menu of wordpress but because I'm calling the function a few times on the same page I needed to add some extra statements but it breaks the output and I don't know why
Here is my current code that outputs the basics:
function test() {
// The Query
query_posts( array ('posts_per_page' => -1 ) );
// The Loop
while ( have_posts() ) : the_post();
?>
<option value="<?php the_permalink() ?>"><?php the_title(); ?></option>
<?php endwhile;
// Reset Query
wp_reset_query();
}
Output code:
<select>
<?php test(); ?>
</select>
Returned Output:
<select>
<option value="http://website/posttitle1">POST TITLE 1</option>
<option value="http://website/posttitle2">POST TITLE 2</option>
<option value="http://website/posttitle3">POST TITLE 3</option>
</select>
But I need to add a select option on like this:
function test($select) {
// The Query
query_posts( array ('posts_per_page' => -1 ) );
// The Loop
while ( have_posts() ) : the_post();
if ($select == the_permalink()) { $selected = " selected"; }
?>
<option value="<?php the_permalink() ?>"><?php the_title(); ?></option><?php echo "\n"; ?>
<?php endwhile;
// Reset Query
wp_reset_query();
}
Output code:
<select>
<?php test("..GET Permalink from Database.."); ?>
</select>
But then this is my output:
<select>
http://website/posttitle1<option value="http://website/posttitle1">POST TITLE 1</option>
http://website/posttitle2<option value="http://website/posttitle2">POST TITLE 2</option>
http://website/posttitle3<option value="http://website/posttitle2">POST TITLE 3</option>
</select>
I dont understand?
the_permalink() print the value and get_permalink() returns the value.
Try this.
Change the following line
if($select == the_permalink()) { $selected = " selected"; }
to
if ($select == get_permalink()) { $selected = " selected"; }
And this line to
<option value="<?php the_permalink() ?>"><?php the_title(); ?></option><?php echo "\n"; ?>
this
<option value="<?php echo get_permalink() ?>"><?php echo get_the_title(); ?></option><?php echo "\n"; ?>
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>