简体   繁体   中英

How to put a Div inside an if statement

Hi i have a code that displays title, date and location. I want it to put on a div, unfortunately there is some wrong in my code and it loops my div.

I just want all may data to be inside "may-container" i just don't know how to create this container.

Hope you could help me with this. Thanks

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

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

Here is my code

 $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); } 

if your code date("F",strtotime($result['date'])) produces a month in words then why do you have to put if and else?

why not:

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);
}

EDIT: to answer your comment, you can try this code:

This code is only applicable if your Data is sorted out by date

$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);
}

if the code above code runs as what i expected(sorry i didnt run it for i dont have enough time to) it will yields something like:

<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>

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM