批改状态:合格
老师批语:
一.课上笔记以及作业
1.for循环实现表格生成器
主要生成器代码(js部分值得学习)
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>表单生成器</title>
<script type="text/javascript" src="jquery.js"></script>
<style>
h3 {
color: green;
margin-left:40px;
}
button {
width: 80px;
height: 30px;
border: none;
background-color: green;
color:white;
margin-right: 30px;
}
button:hover {
background-color: coral;
cursor: pointer;
}
</style>
</head>
<body>
<h3>表格生成器</h3>
<p><label>输入行:<input type="text" name="rows"></label></p>
<p><label>输入列:<input type="text" name="cols"></label></p>
<p><button>生成表格</button><button>重置行列</button></p>
</body>
<script type="text/javascript">
//设置标志位,防止重复提交请求
var flag=true;//这里的true表示还没有提交,false表示提交过了
/** 第一点:处理数据输入的合法化 **/
$('button:first').on('click',function () {
$(':input').not('button').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;
}
});
/**** 第二点处理用户的请求 使用ajax实现***/
if(flag==true){
$.get(
'demo2.php',//提交到 哪里
{//提交的数据
rows:$('input[name="rows"]').val(),
cols:$('input[name="cols"]').val(),
},
function (data) {//返回的回调函数
//先删除原来的生成的表格
$('p:last').next().remove();
//生成新的表格
$('p:last').after(data);
//防止重复提交
flag=false;
}
);
}
/** 重置按钮 *****/
$('button').eq(1).click(function () {
//清空输入框的数据
$(':input').not('button').val('');
$(':input:first').focus();
//将上次生成的表格删除
$('p:last').next().remove();
flag=true;//允许用户重新发起请求
});
});
</script>
</html>点击 "运行实例" 按钮查看在线实例
二:do while() while具体实例
<?php
/**
* while(): 入口判断型
* do ~ while(): 出口判断型
*/
for ($i=0; $i<10; $i++) {
echo $i<9 ? $i.',' : $i;
}
echo '<hr>';
// while()
$i=10; //初始化条件
while($i<10) {
echo $i<9 ? $i.',' : $i;
$i++; //更新条伯
}
echo '<hr>';
//do ~ while()
$i=10; //初始化条件
do {
echo $i<9 ? $i.',' : $i;
$i++; //更新条伯
} while($i<10);点击 "运行实例" 按钮查看在线实例
三.函数的定义的作用域和使用调用
<?php
/**
* 函数的基本知识
* 1.声明的语法
* 2.参数设置
* 3.返回值
* 4.作用域
*/
//声明
function hello()
{
return '欢迎来到php中文网学习';
}
//调用: 按名调用,名称后跟上一对圆括号
echo hello(),'<hr>';
function hello1($siteName)
{
return '欢迎来到'.$siteName.'学习';
}
echo hello1('php中文网'),'<hr>';
//当有可选参数的时候,必须把必选参数往前放
function hello2($lang,$siteName='php中文网')
{
return '欢迎来到'.$siteName.'学习'.$lang;
}
echo hello2('php'),'<hr>';
//参数实际就是一个占位符,仅供参考,可以没有
function hello3()
{
// return print_r(func_get_args(),true);
// return func_get_arg(2);
// return func_num_args(); //获取参数的数量
return (func_get_arg(0) + func_get_arg(1) + func_get_arg(2));
}
echo hello3(4,5,6),'<hr>';
$siteName = 'php中文网';
// php中只有函数作用域,函数外部声明的变量在函数内部不能直接使用
function hello4 ()
{
// global $siteName;
return $GLOBALS['siteName'];
}
echo hello4();点击 "运行实例" 按钮查看在线实例
四.数组常用的键值操作与指针操作
<?php
/**
* 1.常用的键值操作
* 2.数组内部指针操作(巡航)
*/
$user = ['id'=>5, 'name'=>'peter','gender'=>'male','age'=>20];
echo '<pre>',print_r($user,true);
//in_array()判断数组中是否存在某个值
//echo in_array('peter',$user) ? '存在<br>' : '不存在<br>';
//array_key_exists():判断某个键名是否存在于数组中?
//echo array_key_exists('salary',$user) ? '存在<br>' : '不存在<br>';
// array_values():以索引方式返回数组的值组成的数组
//print_r(array_values($user));
// array_keys()
//print_r(array_keys($user));
// array_search():以字符串的方式返回指定值的键
//echo $user[array_search('peter',$user)];
//键值对调
//print_r(array_flip($user));
//数组的内部操作
echo count($user),'<br>';
//key()返回当前元素的键
echo key($user),'<br>';
//current()返回当前元素的值
echo current($user),'<hr>';
//next()指针下移
next($user);
echo key($user),'<br>';
echo current($user),'<hr>';
next($user);
echo key($user),'<br>';
echo current($user),'<hr>';
//复位
reset($user);
echo key($user),'<br>';
echo current($user),'<hr>';
//尾部
end($user);
echo key($user),'<br>';
echo current($user),'<br>';
reset($user);
// each()返回当前元素的键值的索引与关联的描述,并自动下移
print_r(each($user));
//print_r(each($user));
//list() //将索引数组中的值,赋值给一组变量
list($key, $value) = each($user);
echo $key, '******', $value,'<hr>';
// while,list(),each() 遍历数组
reset($user);
while (list($key, $value) = each($user)) {
echo $key , ' => ', $value, '<br>';
}点击 "运行实例" 按钮查看在线实例
五.使用数组中的系统函数模拟 栈和队列操作
<?php /** * 使用数组来模拟堆栈和队列操作 */ $user = ['id'=>5, 'name'=>'peter','gender'=>'male']; echo '<pre>',print_r($user,true); //echo '当前长度: '. count($user), '<br>'; // 入栈:array_push()返回新数组的长度= count() //echo array_push($user, 'php中文网'); //echo '当前长度: '. count($user), '<br>'; //print_r($user); //echo array_pop($user),'<br>'; //echo array_pop($user),'<br>'; //echo array_pop($user),'<br>'; //print_r($user); //队: shift(),unshift() //echo array_unshift($user, 'www.php.cn','peterzhu'); //print_r($user); //echo array_shift($user),'<br>'; //print_r($user); //模拟队列操作: 增删只能在二端进行,不允许同一端进行 array_push($user, 'php'); //尾部进队 print_r($user); array_shift($user); // 头部出队 print_r($user); array_unshift($user, 'html'); // 头部进队 print_r($user); array_pop($user); // 尾部出队 print_r($user);
点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号