put a for loop inside a echo statement - php

I want to put a for loop inside the echo but I can´t, is this possible?
<?php
$limite_may=2014;
$limite_inf=1914;
echo"
<select name='anio_a1' class='select' id='anio_a1'>
<option selected='selected' value='--'>year</option>".
for($i=$limite_may; $i>$limite_inf; $i--){
echo $i;
}.
"</select>";
?>

You want to echo inside a for loop, not put a for loop in the echo. We go though and echo out each item as an <option> with the value $i.
...
echo "<select name='anio_a1' class='select' id='anio_a1'>";
for($i = $limite_may; $i>$limite_inf; $i--) {
echo "<option value=".$i.">".$i."</option>";
}
?>
echo "</select>";
...

<?php
$limite_may=2014;
$limite_inf=1914;
echo "<select name='anio_a1' class='select' id='anio_a1'>";
for($i=$limite_may; $i>$limite_inf; $i--){
echo "<option value='--'>".$i."</option>";
}
echo "</select>";
?>

Remove your HTML from your PHP code blocks entirely, like this:
<?php
$limite_may=2014;
$limite_inf=1914;
?>
<select name='anio_a1' class='select' id='anio_a1'>
<?php
for($i=$limite_may; $i>$limite_inf; $i--){
?>
<option value='<?php echo $i; ?>'><?php echo $i; ?></option>
<?php
}
?>
</select>
If your selected value is stored in a variable named $selected (for example) you can use it like this (HTML5):
<option<?php echo $i==$selected ? ' selected' : ''; ?> value='<?php echo $i; ?>'><?php echo $i; ?></option>
or like this (XHTML):
<option<?php echo $i==$selected ? ' selected="selected"' : ''; ?> value='<?php echo $i; ?>'><?php echo $i; ?></option>

Related

WordPress: How to order child pages in a select menu?

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.

Preselect option dynamically dropdown PHP

I have an array $faecher to create an SELECT Field with options. My problem is that I want to pre-select an option which is saved in variable f_name1.
<select>
<?php
for ($i = 0; $i <= count($faecher); $i++)
echo "<option" if ($faecher[$i] == f_name1) echo 'selected="selected"'; ">".$faecher[$i]."</option>";
?>
</select>
When you'll learn to indent you code, it will be easier:
echo "<option";
if ($faecher[$i] == f_name1) {
echo ' selected="selected"';
}
echo ">".$faecher[$i]."</option>";
Try:
echo "<option".($faecher[$i] == $f_name1 ? ' selected="selected"' : null).">".$faecher[$i]."</option>";
or
echo "<option".($faecher[$i] == $f_name1 ? ' selected="selected"' : '').">".$faecher[$i]."</option>";
What is f_name1 here? You should use direct string here. Like below
<select>
<?php for ($i = 0; $i <= count($faecher); $i++) { ?>
<option <?php echo $faecher[$i] == "Matching String" ? "selected" : ""; ?>><?php echo $faecher[$i]; ?></option>
<?php } ?>
</select>

I need to select instead of echo/print [closed]

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>

php foreach sort before results

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']);
}
)

increament by 5 for loop in php

I want to display some number in a dropdown options with the increment 5 which will be started to 5 and ended at 1005. The result should be 5, 10, 15, .... 1000, 1005.
So I wrote the loop as
<?php for ($i=5; $i < 1005; $i++) { ?>
<option value = "<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
But it gives me as 5,6,7,.... . How can I resolve it?
<?php for ($i=5; $i < 1005; $i+=5) { ?>
<option value = "<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
$i+=5 <-- This is the only change.
How about this?

Categories