It's written in PHP. My if is working fine, but my else is not echoing. $start and $end are working variables.
<select name="trip" class="select-trip">
<?php foreach ( $wp_trips as $trip ) {
if ($trip->available && $is_day_trip){
$start = date_i18n("d/m/Y", strtotime($trip->start_date));
$end = date_i18n("d/m/Y", strtotime($trip->end_date));?>
<option value="<?php echo $trip->remote_ID; ?>">
<?php echo $start;
} else {
echo $start . ' T/M ' . $end;
}
} ?>
</select>
You should declare your $start
and $end
variables before if statement and you should put your option
tag before if statement
and also close it after if statement
<select name="trip" class="select-trip">
<?php
foreach ( $wp_trips as $trip ) {
$start = date_i18n("d/m/Y", strtotime($trip->start_date));
$end = date_i18n("d/m/Y", strtotime($trip->end_date));
?>
<option value="<?php echo $trip->remote_ID; ?>">
<?php
if ($trip->available && $is_day_trip){
echo $start;
} else {
echo $start . ' T/M ' . $end;
}
?>
</option>
<?php
}
?>
</select>
The problem in this code is $start and $end has only scope with in that if statement. For getting the value of $start and $end outside the if statement, variable declaration should obviously been done outside the if statement.
As example you can do it like this one:
foreach ($wp_trips as $trip) {
$checker = $trip->available && $is_day_trip;
$start = '';
$end = '';
if ($checker) {
$start = date_i18n("d/m/Y", strtotime($trip->start_date));
$end = date_i18n("d/m/Y", strtotime($trip->end_date));
}
if ($checker && !empty($start)) {?>
<option value="<?php echo $trip->remote_ID; ?>">
<?php echo $start;?>
</option>
} else {
echo $start . ' T/M ' . $end;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.