PHP零散小项目
瞎鼓捣的录入和回显
主要灵感来源于之前在搞python时,要固定一个永恒变量(把变量的值另外找一个文件存起来),防止变量变成程序执行完成就销毁的临时变量。
想要存储和读取数据我知道的有两种,一种是我这种直接写入文件的,另外一种是数据库。
数据库暂且不说,毕竟还没鼓捣,最近还得考试嘛……
鼓捣下文件存储变量的,具体如下:
- 准备1个php文件和1个txt文件,放在同一个目录下
- php文件取名为“file.php”,txt文件取名为“file.txt”
PHP文件内容如下
<!DOCTYPE html> <html lang="en">
<head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>小东西</title> <style> * { margin: 0; padding: 0; }
table { border: 1px solid red; margin: 10px 30px; }
table tr td { border: 1px solid black; }
form { margin: 10px 30px; } </style> </head>
<body> <form action="file.php" method="post"> 填写表格录入信息:<br> 班级:<input type="text" name="Sclass"><br> 姓名:<input type="text" name="Sname"><br> 座号:<input type="text" name="Sid"><br> <input type="submit" value="提交" name="submit">
<?php if (isset($_POST['submit'])) { $file = fopen('file.txt', 'a+'); $str = $_POST['Sclass'] . ',' . $_POST['Sname'] . ',' . $_POST['Sid'] . "\r\n"; fwrite($file, $str); echo $str; fclose($file); } ?> </form> <form action="y.php" method="post"> <input type="submit" name="zhanshi" value="点击展示数据"> </form> <table> <tr> <td><strong>id</strong></td> <td><strong>班级</strong></td> <td><strong>姓名</strong></td> <td><strong>座位号</strong></td> </tr> <?php if (isset($_POST["zhanshi"])) { $file_r = fopen('file.txt', 'r'); $xs = 0; while (!feof($file_r)) { global $xs; echo '<tr>'; $x = fgets($file_r); $y = explode(',', $x); $xs += 1; echo "<td>{$xs}</td>"; for ($i = 0; $i < count($y); $i++) { echo "<td>{$y[$i]}</td>"; } echo '</tr>'; } fclose($file_r); } ?> </table> </body> </html>
|
txt文件内容留空就行,毕竟本身就是存储数据的容器。
下面是效果:


排序的小作业
emmmmm……行吧,这东西还真……简单。
题目本意是这样的:展示一个二维数组,并且成绩部分要进行降序排序。
具体什么的也没啥好说的,看下面展示吧。勉强算好玩……
<!DOCTYPE html> <html lang="zh-CN">
<head> <meta charset="utf-8"> <title>站点创建成功-phpstudy for windows</title> <style> table, table tr td { border: solid 1px red; } </style> </head>
<body> <table> <tr> <td><b>座位号</b></td> <td><b>名字</b></td> <td><b>性别</b></td> <td><b>分数</b></td> </tr> <?php $arr = array( array(11, "tom", "man", 334), array(12, "sasa", "wman", 336), array(13, "jack", "man", 339), array(14, "lisa", "wman", 335), array(15, "tmx", "man", 360) ); $x = array(); for ($i = 0; $i < count($arr); $i++) { $x[$i] = $arr[$i][3]; } rsort($x); $y = array(); for ($i = 0; $i < 5; $i++) { for ($j = 0; $j < 5; $j++) { if ($x[$i] == $arr[$j][3]) { $y[$i] = $arr[$j]; } } } for ($i = 0; $i < count($y); $i++) { echo "<tr>"; for ($j = 0; $j < count($y[1]); $j++) { echo "<td>{$y[$i][$j]}</td>"; } echo "</tr>"; } ?> </table> </body> </html>
|

质数判断器
壳部分的逻辑:
- 输入两个值,通过post传参得到php处理程序
- 如果两值不输入,默认为1和5
- 对比两值大小,保证恒为前小后大
- 调用函数对范围内的数字挨个进行判断
- 回显所有判断的结果,且一行仅限输出8个结果
质数的判断函数遵循的逻辑:
- 传入数值,如果为1时,恒返回1(为质数)
- 传入数值大于1,会取这个数前面的所有数字与之相余
- 余数为0:恒返回0,为非质数
- 余数非0:退出循环时返回1,为质数
<!DOCTYPE html> <html lang="en">
<head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>质数判断器</title> <style> * { margin: 0; padding: 0; }
body { background-color: orange; }
form { margin: 50px auto; background-color: width: 300px; text-align: center; }
.word { width: 200px; height: auto; text-align: left; } </style> </head>
<body> <form action="x2.php" method="post"> <h1>质数判断器</h1> 请输入俩值进行质数判断: <br>值1:<input type="number" name="val1" value="1"> <br>值2:<input type="number" name="val2" value="5"> <br><input type="submit" value="提交" name="submit"> <input type="reset" value="重置" name="reset"><br> <?php if (isset($_POST["submit"])) { function prenum($num) { $n = 1; if ($num == 1) { return 1; } for ($i = 2; $i < $num; $i++) { if ($num % $i == 0) { $n = 0; break; } } return $n; } $num1 = $_POST['val1']; $num2 = $_POST['val2']; if ($num1 > $num2) { $x = $num1; $num1 = $num2; $num2 = $x; } echo "您所输入的从{$num1}到{$num2}范围的质数有:<br>"; $arr = array(); for (; $num1 <= $num2; $num1++) { $n = prenum($num1); if ($n == 1) { array_push($arr, $num1); } } echo '<div class="word">'; for ($i = 0; $i < count($arr); $i++) { if (($i + 1) % 8 != 0) { echo "{$arr[$i]},"; } else { echo "{$arr[$i]}<br>"; } } echo '</div>'; } ?> </form>
</body>
</html>
|
