如何动态打开/关闭每个x的ul和li?

时间:2016-11-19 16:43:44

标签: php

我正在尝试构建一个php循环,它会生成一个像这样的年份的html导航:

<ul>
  <li>
    1900
    <ul>
      <li>1901</li>
      <li>1902</li>
    </ul>
  </li>
  <li>
    2000
    <ul>
      <li>2001</li>
      <li>2002</li>
    </ul>
  </li>
</ul>

Php我正在尝试:

foreach (range(0, 1000, 100) as $number) { ?>
    <ul>
        // Here I should be checking 
        // if it is year 1942 or 2000 or 1080 
        // and place ti correctly
        <?php echo $number; ?>
    </ul>
<?php }

以上作品,每100分钟给我一个新的ul

但我不明白如果我们有&#34; 子年,我应该如何设置php以打开新的<ul><li> &#34;在本世纪本身,我也应该检查我输出的是哪一年,以便将其放在正确的<ul><li>

2 个答案:

答案 0 :(得分:2)

echo "<ul>";
  for ($century=1900; $century <= 2000 ; $century =$century + 100) {
    echo "<li>". $century;
      echo "<ul>";
      for ($year = $century; $year < $century + 100; $year++) {
        echo "<li>". $year . "</li>";
      }
      echo "</ul>";
    echo "</li>";
  }
echo "</ul>";

答案 1 :(得分:1)

我希望这就是你所需要的:

<?php
$start_year=1900;
$end_year=2100;
for($year=$start_year;$year<=$end_year;$year++){
    if($year%10==0) //Replace 10 (for decades) with 100 (for centuries)
    {
        if($year!=$start_year){
            echo '</ul></li></ul>';
        }
        echo '<ul><li>'.$year.'<ul>';
    }
    else{
        echo '<li>'.$year.'</li>';
    }
}
?>