博主信息
博文 31
粉丝 3
评论 1
访问量 40642
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
php制作一个表格自动生成器
php学习笔记
原创
6258人浏览过

用php制作一个表格自动生成器,输入表格标题、表格行数、表格列数就能自动生成一个几行几列的表格。

下面用实例来演示一下:

index.php代码如下:

实例

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>表格生成器</title>
	<style type="text/css">
		h3{
			color: green;
			margin-left: 40px;
		}
		button{
			width: 80px;
			height: 30px;
			border:none;
			background-color: green;
			color: white;
			margin-left: 30px;
		}
	</style>
</head>
<body>
	<h3>表格生成器</h3>
	<p><label>输入标题:<input type="text" name="title"></label></p>
	<p><label>输入行:<input type="text" name="rows"></label></p>
	<p><label>输入列:<input type="text" name="cols"></label></p>
	<p><button>生成表格</button><button>重置行列</button></p>
	<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
	<script type="text/javascript">
		var flag = true;
		$('button:first').on('click',function(){
			if($('input:first').val().length == 0){
				$('input:first').after('<span style="color:red">不能为空</span>')
				setTimeout(function(){
                         $('input:first').next().remove()
					},2000)
					return false
			}
			$(':input').not('button').not('input:first').each(function(index,obj){
				if($(obj).val().length == 0){
					$(obj).after('<span style="color:red">不能为空</span>')
					setTimeout(function(){
                         $(obj).next().remove()
					},2000)
					return false

				}else if(isNaN($(obj).val())){
                      $(obj).after('<span style="color:red">必须为数字</span>')
					setTimeout(function(){
                         $(obj).next().remove()
					},2000)
					return false
				}else if($(obj).val() <= 0){
                      $(obj).after('<span style="color:red">必须大于0</span>')
					setTimeout(function(){
                         $(obj).next().remove()
					},2000)
					return false
				}
			})
			if(flag == true){
				$.get('table.php',{
					 title:$('input:first').val(),
                     rows:$('input[name="rows"]').val(),
                     cols:$('input[name="cols"]').val()
				},function(data){
					$('p:last').after(data)
					flag = false
				})
			}
		})
		$('button:last').click(function(){
			$(':input').not('button').val('')
			$(':input:first').focus()
			$('p:last').next().remove()
			flag = true
		})
	</script>
</body>
</html>

运行实例 »

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

table.php代码如下:

<?php
if($_SERVER['REQUEST_METHOD'] == 'GET'){
    if(!empty($_GET['rows']) && !empty($_GET['cols']) && !empty($_GET['title'])){
    	$rows = $_GET['rows'];
    	$cols = $_GET['cols'];
    	$title = $_GET['title'];
    	$table = '<table border="1" cellspacing="0" cellpadding="3" align="center" width="80%">';
    	$table .= '<caption>'.$title.'</caption>';
    	$table .='<tr align="center" bgcolor="lightgreen">';
    	for($i=0;$i<$cols;$i++){
    		$table .= '<th>X</th>';
    	}
    	$table .= '</tr>';
    	for($r=0;$r<$rows;$r++){
    		$table .= '<tr>';
            for($c=0;$c<$cols;$c++){
            	$data = $r*$cols+$c;
                 $table .= '<td align="center">'.++$data.'</td>';
            }
    		$table .= '</tr>';
    	}
    	$table .= '</table>';
    	echo $table;
    }
}else{
	exit('<span style="color:red">非法请求</span>');
}


运行结果:

2018-04-16_152737.png


批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学