我正在修改一个wordpress小部件,用于以列表格式输出每周课程表。现在这个代码,它将输出每个工作日,然后输出当天的类信息。问题是,即使有一天没有课程,它仍然会回应当天的名字。我想跳过回显任何不包含数组内的数据的工作日。这是代码:
<?php
/**
* @file
* Output view template.
*
* Available Variables:
* - $weekday_names: Array of weekday names to be used in table output.
* - $weekdays: Array of used weekdays based on user preference.
* - $start_hours: Array of unique start hours.
* - $classes: Multi-dimensional array in the structure of $classes[weekday][start_hour].
*/
?>
<div id="wcs-schedule-list">
<?php foreach ( $weekdays as $weekday ): ?>
<h3><?php echo $weekday; ?></h3>
<div class="list-container">
<ul class="wcs-schedule-list">
<?php foreach ( $start_hours as $start_hour ): ?>
<?php echo WcsSchedule::model()->renderListItem( $classes, $start_hour, $weekday ) ?>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>
</div>
非常感谢任何帮助!
这是空的$工作日的var_dump($start_hours);
。
Tuesday
array(6) { [1]=> string(8) "12:00 pm" [6]=> string(7) "2:00 pm" [4]=> string(7) "3:30 pm" [5]=> string(7) "6:00 pm" [2]=> string(7) "7:00 pm" [0]=> string(7) "8:15 pm" } array(6) { [1]=> string(8) "12:00 pm" [6]=> string(7) "2:00 pm" [4]=> string(7) "3:30 pm" [5]=> string(7) "6:00 pm" [2]=> string(7) "7:00 pm" [0]=> string(7) "8:15 pm" } array(6) { [1]=> string(8) "12:00 pm" [6]=> string(7) "2:00 pm" [4]=> string(7) "3:30 pm" [5]=> string(7) "6:00 pm" [2]=> string(7) "7:00 pm" [0]=> string(7) "8:15 pm" } array(6) { [1]=> string(8) "12:00 pm" [6]=> string(7) "2:00 pm" [4]=> string(7) "3:30 pm" [5]=> string(7) "6:00 pm" [2]=> string(7) "7:00 pm" [0]=> string(7) "8:15 pm" } array(6) { [1]=> string(8) "12:00 pm" [6]=> string(7) "2:00 pm" [4]=> string(7) "3:30 pm" [5]=> string(7) "6:00 pm" [2]=> string(7) "7:00 pm" [0]=> string(7) "8:15 pm" } array(6) { [1]=> string(8) "12:00 pm" [6]=> string(7) "2:00 pm" [4]=> string(7) "3:30 pm" [5]=> string(7) "6:00 pm" [2]=> string(7) "7:00 pm" [0]=> string(7) "8:15 pm" }
答案 0 :(得分:1)
另外两个答案是错误的。他们仍然输出这一天,即使它是空的。你走了:
<div id="wcs-schedule-list">
<?php foreach ( $weekdays as $weekday ): ?>
<?php if (empty($classes[$weekday])) continue; ?>
<h3><?php echo $weekday; ?></h3>
<div class="list-container">
<ul class="wcs-schedule-list">
<?php foreach ( $start_hours as $start_hour ): ?>
<?php echo WcsSchedule::model()->renderListItem( $classes, $start_hour, $weekday ) ?>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>
</div>
此答案假设在给定的$classes[$weekday]
上没有安排任何内容时$weekday
为空。
答案 1 :(得分:0)
<?php foreach ( $weekdays as $weekday ): ?>
<h3><?php echo $weekday;
if(isset($weekday) && !empty($weekday)) {
?></h3>
<div class="list-container">
<ul class="wcs-schedule-list">
<?php foreach ( $start_hours as $start_hour ): ?>
<?php echo WcsSchedule::model()->renderListItem( $classes, $start_hour, $weekday ) ?>
<?php endforeach; ?>
</ul>
</div>
<?php } endforeach; ?>
答案 2 :(得分:0)
<?php
echo '<div id="wcs-schedule-list"> ';
foreach ( $weekdays as $weekday ){
//you need to add a check with WcsSchedule first, to see if this $weekday has any classes before you begin outputting anything
echo "<h3>$weekday</h3>";
echo '<div class="list-container">';
foreach ( $start_hours as $start_hour ) {
echo '<ul class="wcs-schedule-list">';
echo WcsSchedule::model()->renderListItem( $classes, $start_hour, $weekday );
}
echo "</ul>";
echo "</div>";
}
echo "</div>";
?>
这还不完整,你需要调整它,因为我不熟悉你的使用方法,但是你需要首先检查是否有类使用你的模型,我想。
你需要想出一种方法来检查当天是否有课程并从那里开始,或者如上所述,在输出任何内容之前循环开始你的start_hours。在那个for循环检查中,是否从模型返回任何内容并从那里开始。