PHP Programming Basics Experiment 2: PHP Basic Syntax Programming (4)

Experiment 2-4 PHP basic syntax programming (4)

1. Experimental hours

2 hours

2. Experimental purpose

1. Become more familiar with the basic syntax of PHP;

2. Master the definition and common processing methods of string data in PHP;

3. Master the definition and common processing methods of array data in PHP;

4. Master the predefined arrays in PHP and how to use them.

3. Experimental equipment

    PC computer, equipped with Win10 operating system, Word2019, PHPStudy+eclipse for php

4.Experimental content and procedures

( 1 ) Experimental steps

① Create a new Project File—new—Local PHP Project and name it PHP2_4

②Create two new php files, right-click the project name—new—PHP File

③Enter the code and save it

④Verify the correctness of the code in the browser and output the running results

(2) Experimental content

1)

Loop to generate 4 inputs, enter student number,

After clicking the submit button, output all student numbers. Find the student numbers starting with 1811, replace them with 1810, and then output them. The format is as shown in the figure:

 

Source code: 2_4_1.php

<?php
	echo "请输入学号:</br>";
	echo "<form method=post>";
	for ($i = 1 ; $i < 5 ; $i ++){
		if ($i >= 2) echo "-";
		echo "<input type='text' name='id[]' size='6'>";
	}
	echo "<input type='submit' name='sub' value='提交'>";
	echo "</form>";
	if (isset($_POST['sub'])){
		$k = 0;
		$jsj = array();
		$stu = $_POST['id'];//将文本框的值赋给数组$xuehao不用在变量后面加[]
		for ($i = 0 ; $i < count($stu) ; $i++){
			for ($j = $i + 1 ; $j < count($stu) ; $j++){
				if (strcmp($stu[$i], $stu[$j]) == 0)
					array_splice($stu, $i,1);//将数组中重复的值删除
			}
		}
		$str = implode(",", $stu);//使用逗号作为连接符将数组转化为字符串
		echo "所有的学生学号如下:</br>";
		echo $str."</br>";
		foreach ($stu as $value){
			if (strstr($value, "1811")){
				$string = str_replace("1811", "1810", $value);
				$jsj[$k] = $string;
				$k++;
			}
		}
		echo "计算机专业的学号如下:</br>";
		echo implode(",", $jsj);
	}
?>

Result screenshot:

Before clicking Submit:

 After clicking submit:

2) Create an interface similar to the one shown below. The complaint information can be customized. After clicking the submit button, the selected and entered complaint information will be output.

Source code: 2_4_2.php

<!DOCTYPE HTML>
<html>
<head>
	<meta charset="UTF-8">
	<title>投诉页面</title>
	<style type="text/css">
		#tijiao {
			background-color: rgb(0, 150, 138);
			color: white;
			width: 50px;
			height: 30px;
			border-color: rgb(0, 150, 138);
			border-style: dashed;
		}
		#chongzhi {
			width: 50px;
			height: 30px;
		}
	</style>
	
</head>
<body>
	<form name="form" method="POST" action="">
		<table>
			<tr>
				<td valign="top" align="right">投诉类型</td>
				<td><input id="check" type="checkbox" name="like[0]" value="态度不好">态度不好 <br>
					<input id="check" type="checkbox" name="like[1]" value="业务不熟">业务不熟 <br>
					<input id="check" type="checkbox" name="like[2]" value="没人接电话">没人接电话
				</td>
			</tr>
			<tr>
				<td valign="top" align="right">详情</td>
				<td><textarea rows="5" cols="30" name="desc"></textarea></td>
			</tr>
			<tr>
				<td></td>
				<td><input id="tijiao" type="submit" value="提交" name="ok">
					&nbsp;
					<input id="chongzhi" type="reset" value="重置">
				</td>
			</tr>
		</table>
	</form>
</body>
</html>
<?php
$like=array(
		array('态度不好'),
		array('业务不熟'),
		array('没人接电话')
);
	if (isset($_POST['ok'])){	
		$like = $_POST['like'];
		$desc = $_POST['desc'];	
		foreach($like as $k=>$v){
			echo $k.'->'.$v.'</br>';
		}
// 		while(list($key,$value)=@each($like)){//用while循环也是可以的
// 			echo $key.'->'.$value.'</br>';
// 		}
		echo $desc;
	}
?>

Result screenshot:

After clicking submit:

If nothing is checked, that is, when the array passed is empty, an error will be reported.

We need to add a precondition to the foreach loop:

if(is_array($like) && !emptyempty($like)){
			foreach($like as $k=>$v){
				echo $k.'->'.$v.'</br>';
			}
			// 		while(list($key,$value)=@each($like)){//用while循环也是可以的
			// 			echo $key.'->'.$value.'</br>';
			// 		}
		}

I didn’t finish the third question yesterday. I’ll update it today.


3) The known array data is as follows:

$books=array(

           array("name"=>"我的2020","price"=>20.0,"author"=>"wang"),

           array("name"=>"PHP Programming","price"=>48.0,"author"=>"zhang"),

           array("name"=>"C++ Object-Oriented Programming", "price"=>30.0, "author"=>"li"),

           array("name"=>"python programming","price"=>40.0,"author"=>"zhou"),

           array("name"=>"A brief history of time","price"=>25.0,"author"=>"sun"),

       );

The coding is required to implement a graph-like interface, and the data is output in tabular form. The tabular format is not limited, and the array content can be output.

Source code: 2_4_4.php

<?php 
$books=array(
	       array("name"=>"我的2020","price"=>20.0,"author"=>"wang"),
	       array("name"=>"PHP程序设计","price"=>48.0,"author"=>"zhang"),
	       array("name"=>"C++面向对象程序设计","price"=>30.0,"author"=>"li"),
	       array("name"=>"python程序设计","price"=>40.0,"author"=>"zhou"),
	       array("name"=>"时间简史","price"=>25.0,"author"=>"sun"),
	   );
//创建表格将数组循环输入
	echo '<table border="1" width="600" align="center">';
	echo '<tr>';
	echo '<td>序号</td><td>书名</td><td>价格</td><td>作者</td>';
	echo '</tr>';
	foreach ($books as $k=>$v){
		echo '<tr>';
		echo "<td>".($k + 1)."</td>";
		echo "<td>".$v['name']."</td>";
		echo "<td>".$v['price']."</td>";
		echo "<td>".$v['author']."</td>";
		echo '</tr>';
	}
	echo '</table>';
?>

 Result screenshot:

As for the table being a bit ugly, this is a problem with page rendering. I won’t write about it here. I’ll update it when there are improvements!

By the way, finally, I would like to ask, when you put the html code and the php code together, in which format do you insert the code snippet?

Guess you like

Origin blog.csdn.net/pzcxl/article/details/127062993