PHP常用命令

Select Code
1
2
3
4
5
6
7
8
9
[root@localhost ~]# php --help
Usage: php [options] [-f] <file> [--] [args...]

-h               This help
-i               PHP information
-m               Show compiled in modules
-v               Version number

--ini            Show configuration file names

php -v 查看版本
php -h 帮助
php -i 等同于phpinfo()
php –ini 查看配置文件

php -m 查看安装好的模块

PHP实现无限级分类实例数据分析演示

无限级分类的数据库结构设计:
unlimited_catagories1

测试数据下载地址:
https://github.com/webjust/case-study/tree/master/unlimited%20catagories

测试数据库表结构:

Select Code
1
2
3
4
5
6
7
8
9
CREATE TABLE `shop_catalog` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `pid` int(11) unsigned NOT NULL,
  `name` varchar(255) NOT NULL,
  `title` varchar(255) NOT NULL,
  `description` varchar(255) NOT NULL,
  `path` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

在网页上显示的效果:
unlimited_catagories2

部分源代码如下:

第1步:从数据库遍历pid=0的分类

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php 
$conn = mysqli_connect('localhost', 'root', '', 'ishop') or die("数据库连接失败");
mysqli_set_charset($conn, 'utf8');

// 一级分类
$sql = "SELECT id,pid,name,path FROM `shop_catalog` WHERE `pid` = 0";
$res = mysqli_query($conn, $sql);
if ($res) {
	while($row = mysqli_fetch_assoc($res)){
		$cata1_list[] = $row;
	}
}
?>

第2步:嵌套循环遍历全部的分类

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php foreach($cata1_list as $val): ?>
<div style="width:180px; margin: 5px; float:left">
<div class="level01"><h1><?php echo $val['name'] ?></h1></div>
<div>
<?php 
	// 嵌套:遍历2级分类
	// 遍历父ID为上一级的ID的分类
	$sql2 = "SELECT id,pid,name,path FROM `shop_catalog` WHERE `pid` = {$val['id']}";
	// echo $sql;
	$res2 = mysqli_query($conn, $sql2);
	$cata2_list = [];
	if ($res2) {
		while ($row2 = mysqli_fetch_assoc($res2)){
			$cata2_list[] = $row2;
		}
	}
	foreach($cata2_list as $val2):
?>
	<div style="border:1px solid #ddd; margin-bottom:10px; padding:10px;">
	<div class="level02"><h2><?php echo $val2['name']; ?></h2></div>

	<?php 
		// 嵌套:遍历3级分类
		$sql3 = "SELECT id,pid,name,path FROM `shop_catalog` WHERE `pid` = {$val2['id']}";
		$res3 = mysqli_query($conn, $sql3);
		$cata3_list = [];
		if ($res3) {
			while ($row3 = mysqli_fetch_assoc($res3)){
				$cata3_list[] = $row3;
			}
		}
		// print_r($cata2_list);
		foreach($cata3_list as $val3):
	?>
		<a href="./catalog.php?cid=<?php echo $val3['id'] ?>" class="level03"><?php echo $val3['name'] ?></a>
	<?php endforeach ?>
	</div>
<?php endforeach; ?>
</div>
</div>
<?php endforeach;?>

遍历文件名称遇到目录名为0的情况,写判断条件使用!==

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
    var_dump("false"==0);
    //返回 boolean true

    var_dump(false=="0");
    //返回 boolean true

    var_dump(false!=="0");
    //返回 boolean true

    var_dump(false!="0");
    //返回 boolean false

    /*
        当读取文件名称时,while (false !== ($file = readdir($handle))) 条件为true时,输出文件名称。

        如果遇到文件夹名称为“0”时,只有var_dump(false!=="0")才可以完整的遍历当前目录下得所有文件
    */

    /*程序应用如下*/

    if ($handle = opendir('./mycncart')) {        //遍历目标文件目录
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                echo "$file<br />";
            }
        }
        closedir($handle);
    }

?>