Blogger Information
Blog 27
fans 0
comment 0
visits 22341
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
for循环做表格的三种方式
不乖的博客
Original
2684 people have browsed it
<?php
	$title='员工信息表';
	$arr = array(
		array(
			'id'=>1,
			'name'=>'朱一龙',
			'age' =>18,
			'sex'=>'男'
		),
		array(
			'id'=>2,
			'name'=>'般若',
			'age' =>20,
			'sex'=>'女'
		),
		array(
			'id'=>3,
			'name'=>'阿护',
			'age' =>20,
			'sex'=>'男'
		),
		array(
			'id'=>4,
			'name'=>'明玉',
			'age' =>30,
			'sex'=>'女'
		)
	)
?>
<!DOCTYPE html>
<html>
<head>
	<title>员工信息表</title>
	<style>
			*{
				margin: 0;
				padding: 0;
			}
			table{
				widows: 400px;
				margin:100px auto;
				border-collapse:collapse
			}
			th{
				width: 100px;
				height: 30px;
				background: paleturquoise;
				border: 1px solid black
			}
			td{
				width: 100px;
				height: 30px;
				border: 1px solid black;
				text-align: center;
			}
	</style>
</head>
<body>
		<h1><?php echo $title;?></h1>
		<table cellpadding="0" cellspacing="0">
			<tr>
				<th>编号</th>
				<th>姓名</th>
				<th>年龄</th>
				<th>性别</th>
			</tr>
			<!-- 第一种循环方式 -->
			<!-- <?php
				foreach ($arr as $key => $value) {?>
					<tr>
						<td><?php echo $value['id']; ?></td>
						<td><?php echo $value['name']; ?></td>
						<td><?php echo $value['age']; ?></td>
						<td><?php echo $value['sex']; ?></td>
					</tr> 
				<?php } ?> -->

				<!-- 第二种循环方式 -->
				<!-- <?php
					$length=count($arr);
					for( $i=0; $i<$length; $i++){ ?>
					<tr>
							<td><?php echo $arr[$i]['id']; ?></td>
							<td><?php echo $arr[$i]['name']; ?></td>
							<td><?php echo $arr[$i]['age']; ?></td>
							<td><?php echo $arr[$i]['sex']; ?></td>
						</tr> 
				<?php } ?> -->

				<!-- 第三种方式 -->
				<?php
					$res='';
					foreach ($arr as $key => $value) {
						$res.='<tr><td>'.$value['id'].'</td>';
						$res.='<td>'.$value['name'].'</td>';
						$res.='<td>'.$value['age'].'</td>';
						$res.='<td>'.$value['sex'].'</td>';
						$res.='</tr>';
					}
					echo $res;
				?>
		</table>
</body>
</html>


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post