PHP - While 和包装 div

标签 php html while-loop

我需要在一天内包装日期和所有事件以划分“obal_date”,但这不起作用。事件的数量可能不同,但我需要包装日期 + 所有事件。请问我该怎么做。非常感谢你。

<?php

# This will hold what group we're in
$current_header = '';

# The Loop
while ( $the_query->have_posts() ) :
    $the_query->the_post();

    $temp_date = get_post_meta( get_the_ID(), 'datum_eventu', true );

    echo '<div class="obal_over">'; 

    if ( $temp_date != $current_header ) {
        $current_header = $temp_date;
    /*****************output date******************/
    ?>

        <div class="obal_date">

        <?php
            echo "<span class='date'>".date("d", strtotime($current_header))."</span>";
            echo "<span class='month'>".date("M", strtotime($current_header))."</span>";
        ?>

        </div>     
    <?php
    /*****************date end******************/
    }
    /*****************All event action1, action2....******************/
    $title = get_post_meta( get_the_ID(), 'nazev_eventu', true );
    $cas = get_post_meta( get_the_ID(), 'cas_eventu', true );

    echo '<div class="obal_single">';   
        echo "<span class='cas'>".$cas."</span>";
        echo "<span class='title'>".$title."</span>";
    echo '</div>';  
    echo '</div>';  
    /*****************End event action1, action2....******************/
endwhile;

?>

编辑1:

我需要得到:

div -> date -> action1 -> action2 -> /div

<div class="obal_over">
  <div class="obal_date">
  01Sep
  </div>
  <div class="obal_single">City1 title</div>
  <div class="obal_single">City1 title2</div>
  <div class="obal_single">City3 title</div>
</div>
//..... other date and action again start class="obal_over"

但是我明白了

div -> date -> action -> /div -> div -> action2 -> /div

<div class="obal_over">
  <div class="obal_date">
  01Sep
  </div>
  <div class="obal_single">City1 title</div>
</div>   
<div class="obal_over"> 
  <div class="obal_single">City1 title2</div>
</div>
<div class="obal_over"> 
  <div class="obal_single">City3 title</div>
</div>

Previous question

最佳答案

为了方便您列出日期和城市名称的方式,我在 while 循环外添加了一个 div 标签。然后在检查日期相同或不同的 if 条件中使用 obal_over 类(以及结束 div 标记)启动 div。还在 while 循环外添加了结束 div 标记,以提供正确的打开和关闭 div 标记。我认为这会起作用。我无法测试...

    <?php

# This will hold what group we're in
$current_header = '';
echo '<div>';
# The Loop
while ( $the_query->have_posts() ) :
    $the_query->the_post();       

    $temp_date = get_post_meta( get_the_ID(), 'datum_eventu', true );        

    if ( $temp_date != $current_header ) {
        $current_header = $temp_date;
        echo '</div>';
        echo '<div class="obal_over">'; 
    /*****************output date******************/
    ?>
        <div class="obal_date">

        <?php
            echo "<span class='date'>".date("d", strtotime($current_header))."</span>";
            echo "<span class='month'>".date("M", strtotime($current_header))."</span>";
        ?>

        </div>     
    <?php
    /*****************date end******************/
    }
    /*****************All event action1, action2....******************/
    $title = get_post_meta( get_the_ID(), 'nazev_eventu', true );
    $cas = get_post_meta( get_the_ID(), 'cas_eventu', true );

    echo '<div class="obal_single">';   
        echo "<span class='cas'>".$cas."</span>";
        echo "<span class='title'>".$title."</span>";
    echo '</div>';  

    /*****************End event action1, action2....******************/



endwhile;
echo '</div>';
?>

关于PHP - While 和包装 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47049762/

相关文章:

html - CSS 边框在 div 外溢出

bash 同时 [: too many arguments

c - (C 编程) while 循环仅按特定顺序工作

php - 尖叫 : Error suppresion ignored for Parse error: syntax error PHP

php - if 语句不适用于 php 中的 false

html - 如何在一个类中调用多个CSS类?

c - 在c中查找回文,变量困惑

php - 为什么我的 MySQL TIMESTAMPDIFF 不工作?

Php,为什么 "global container"不好,如何避免?

html - 如何覆盖文本对齐