php 总结(7) while for 循环控制 深层应用 两种方式实现日历

一:总日子设定好 ,最外面来一个大循环  tr,每7天 来一个循环th 这个循环 的时候给大循环 i++ , 相当于大循环每隔 7个数字来一次循环

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
	table{ 460px;}
	tr{float: left}
	th{border: 1px solid black; 60px;float: left}
	
</style>
</head>
<body>
	<table  cellspacing="0" >
		<tr>
			<th>周日</th>
			<th>周一</th>
			<th>周二</th>
			<th>周三</th>
			<th>周四</th>
			<th>周五</th>
			<th>周六</th>
		</tr>
		<?php 
		$days=31;
		for ($j=1; $j <=$days ; ) { 
			echo "<tr>";
			for ($i=1; $i <=7 ; $i++,$j++) { 
			
				echo "<th>";
				if ($j>31) {
					echo "&nbsp";
				}else{
					echo "$j ";
				}
				echo "</th>";
			}
			echo "</tr>";

		}

		?>
	</table>
</body>
</html>

 二:在外面的循环来五次  里面循环7次   里面的循环根据 外面循环的次数 记录下来 然后根据次数 输出当前的值 .

<table  cellspacing="0" >
		<tr>
			<th>周日</th>
			<th>周一</th>
			<th>周二</th>
			<th>周三</th>
			<th>周四</th>
			<th>周五</th>
			<th>周六</th>

		</tr>
		<?php 
		for ($j=1; $j <=5 ; $j++) { 
           	# code...
			
			
			echo "<tr>";
			for ($i=1; $i <=7 ; $i++) { 
				echo "<th>";
				switch ($j) {
					case 1:
					$s=$i ;
					break;
					case 2:
					$s=$i+7 ;
					break;
					case 3:
					$s=$i+14 ;
					break;
					case 4:
					$s=$i+21 ;
					break;
					case 5:
					$s=$i+27 ;
					break;
					
				}
				if ($s>31) {
					$s=' &nbsp';
				}
				echo "$s ";
				echo "</th>";
			}
			echo "</tr>";

		}

		?>
	</table>

  

 

原文地址:https://www.cnblogs.com/nice2018/p/10362353.html