Blogger Information
Blog 20
fans 0
comment 0
visits 25269
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php与html混编实现数组数据列表
左手Leon的博客
Original
810 people have browsed it

实例-二维数组输出表格-使用函数方式

<!-- 使用函数方法程序 -->
<?php 
// 输入数组信息
	$arr=array(
		[1,'候亮平',30,'男','hlp@php.cn','123456'],
		[2,'赵瑞龙',40,'男','zrl@php.cn','123456'],
		[3,'李达康',50,'男','ldk@php.cn','123456'],
		[4,'祁同伟',45,'男','qtw@php.cn','123456'],
		[5,'高小琴',30,'女','gxq@php.cn','123456']
		);
	$title='用户信息表';
	// 统计数组元素数量
	$count=count($arr);

	function createTable($arr){
		$result='';
 				foreach ($arr as $v) {
 						$result.='<tr>';
 					foreach ($v as $key => $value) {
 						$result.='<td>';
 						
 						if ($key==5) {
 							$result.=sha1($v[$key]);
 						}else{
 							$result.=$v[$key];
 						}
 						$result.='</td>';
 					}
 						$result.='</tr>';
 				}
 				return $result;
	}
 ?>
 <!DOCTYPE html>
 <html>
 <head>
 	<meta charset="UTF-8">
 	<title><?php echo $title ?></title>
 	<style>
 		 table,th,td {
            border: 1px solid #666;
            padding: 8px;
        }

        table {
            border-collapse: collapse;
            width: 80%;
            text-align: center;
            margin: 30px auto;
        }
        thead tr:first-of-type {
            background-color: lightblue;
        }
        tbody tr:hover {
            background-color: #efefef;
        }
        table > caption {
            font-size: 1.2rem;
            margin: 15px auto;
        }
        table + p {
            text-align: center;
        }

}
 		
 	</style>
 </head>
 <body>
        
     
   	<table>
        <caption>
        <?php
            echo '<span style="color:red">' . $title . '</span>';
        ?> 
        </caption>
 		<thead>
 		<tr>
	 		<th>编号</th>
	 		<th>姓名</th>
	 		<th>年龄</th>
	 		<th>性别</th>
	 		<th>邮箱</th>
	 		<th>密码</th>

 		</tr>
 		</thead>
 		
 		<tbody>
 			
 			<?php 
 				echo createTable($arr);
 			 ?>
 		</tbody>
 	</table>
    </div>
 	<p>总计:
    <?php echo $count;  ?>
    人</p>
 </body>
 </html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例-二维数组输出表格-未使用函数方式

<!-- 未使用函数封装程序 -->
<?php 
// 输入数组信息
	$arr=array(
		[1,'候亮平',30,'男','hlp@php.cn','123456'],
		[2,'赵瑞龙',40,'男','zrl@php.cn','123456'],
		[3,'李达康',50,'男','ldk@php.cn','123456'],
		[4,'祁同伟',45,'男','qtw@php.cn','123456'],
		[5,'高小琴',30,'女','gxq@php.cn','123456']
		);
	$title='用户信息表';
	// 统计数组元素数量
	$count=count($arr);
 ?>
 <!DOCTYPE html>
 <html>
 <head>
 	<meta charset="UTF-8">
 	<title><?php echo $title ?></title>
 	<style>
 		 table,th,td {
            border: 1px solid #666;
            padding: 8px;
        }

        table {
            border-collapse: collapse;
            width: 80%;
            text-align: center;
            margin: 30px auto;
        }
        thead tr:first-of-type {
            background-color: lightblue;
        }
        tbody tr:hover {
            background-color: #efefef;
        }
        table > caption {
            font-size: 1.2rem;
            margin: 15px auto;
        }
        table + p {
            text-align: center;
        }

}
 		
 	</style>
 </head>
 <body>
 	<table>
         <caption>
             <?php
        echo '<span style="color:red">' . $title . '</span>';
        ?> 
        </caption>
 		<thead>
 		<tr>
	 		<th>编号</th>
	 		<th>姓名</th>
	 		<th>年龄</th>
	 		<th>性别</th>
	 		<th>邮箱</th>
	 		<th>密码</th>

 		</tr>
 		</thead>
 		
 		<tbody>
 			
 			<?php 
 				$result='';
 				foreach ($arr as $v) {
 						$result.='<tr>';
 					foreach ($v as $key => $value) {
 						$result.='<td>';
 						
 						if ($key==5) {
 							$result.=sha1($v[$key]);
 						}else{
 							$result.=$v[$key];
 						}
 						$result.='</td>';
 					}
 						$result.='</tr>';
 				}
 				echo $result;
 			 ?>
 		</tbody>
 	</table>
 	<p>总计:
    <?php echo $count;  ?>
    人</p>
 </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