簡體   English   中英

如何將Div放入if語句中

[英]How to put a Div inside an if statement

嗨,我有一個顯示標題,日期和位置的代碼。 我希望它放在div上,不幸的是我的代碼中有一些錯誤,它循環了div。

我只是希望所有may數據都在“ may-container”內部,我只是不知道如何創建此容器。

希望你能幫我這個忙。 謝謝

<div class="may-container">
 <div class="May">
 title
 date 
 location
 </div>

 <div class="May">
 title
 date 
 location
 </div>
</div>

這是我的代碼

 $counter = 0; while ( $startdate <= $enddate) { if ( date("F",strtotime($result['date'])) == "May" && date("m, Y",strtotime($result['date'])) >= date("m, Y") ) { if ($counter < 1) { echo "<div class='May-container'>"; $counter++; } echo "<div class= 'May'>"; echo $result['title'],"<br>"; echo date ('F \\ j,\\ Y',strtotime($result['date']) ), "<br>"; echo $result['location']; echo "</div>"; } $startdate = strtotime("+120 day", $startdate); } 

如果您的代碼date("F",strtotime($result['date']))產生了一個月的文字,那么為什么還要加上if和else?

為什么不:

while ( $startdate <= $enddate) {
  echo "<div class='" . date('F',strtotime($result['date'])) . "'>";
  echo $result['title'],"<br>";
  echo date ('F \ j,\ Y',strtotime($result['date']) ), "<br>";
  echo $result['location'];
  echo "</div>";

  $startdate = strtotime("+120 day", $startdate);
}

編輯:要回答您的評論,您可以嘗試以下代碼:

僅當您的數據按日期排序時,此代碼才適用

$last_month = '';
$is_first = true;
while ( $startdate <= $enddate) {

  if($last_month != date('F',strtotime($result['date']))){
     echo '</div>';
     $is_first = true;
  }

  if($is_first){
     $last_month = date('F',strtotime($result['date']));
     echo "<div class='". strtolower($last_month) . "-container'>";
     $is_first = false;
  }
  echo "<div class='" . date('F',strtotime($result['date'])) . "'>";
  echo $result['title'],"<br>";
  echo date ('F \ j,\ Y',strtotime($result['date']) ), "<br>";
  echo $result['location'];
  echo "</div>";

  $startdate = strtotime("+120 day", $startdate);
}

如果以上代碼的代碼按我的預期運行(對不起,我沒有足夠的時間運行它),將產生如下內容:

<div class="may-container">
  <div class="May">
    title
    date 
    location
  </div>

  <div class="May">
    title
    date 
    location
  </div>
</div>

<div class="june-container">
  <div class="June">
    title
    date 
    location
  </div>

  <div class="June">
    title
    date 
    location
  </div>
</div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM