0

我正在修改一个用于以列表格式输出每周课程表的 wordpress 小部件。就像现在的代码一样,它将输出每个工作日,然后是当天的班级信息。问题是即使有一天没有课,它仍然会呼应那一天的名字。我想跳过回显任何不包含其数组中数据的 $weekday 。这是代码:

<?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);一个空的 $weekday。

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" } 
4

3 回答 3

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

于 2013-03-20T04:42:47.067 回答
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; ?>
于 2013-03-20T04:37:26.040 回答
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 循环中检查是否从您的模型返回任何内容并从那里开始。

于 2013-03-20T04:43:59.210 回答