php 无限分类
php 无限分类
创建数据库以及表:
复制代码
CREATE DATABASE `sortclass`DEFAULT CHARSET utf8;
CREATE TABLE IF NOT EXISTS `class` (
`cid` mediumint(8) unsigned NOT NULL auto_increment,
`pid` mediumint(8) unsigned NOT NULL,
`cname` varchar(50) NOT NULL,
PRIMARY KEY (`cid`),
KEY `pid` (`pid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
处理文件示例:
php代码
复制代码
<?php
header("Content-type: text/html; charset=utf-8");
//连接数据库
$link = mysql_connect('localhost','root','eric') or die(mysql_error());
mysql_select_db('sortclass',$link);
//无限分类类库
class SortClass{
var $data = array();
var $child = array(-1=>array());
var $layer = array(-1=>-1);
var $parent = array();
var $link;
var $table;
function SortClass($link, $table){
$this->setNode(0, -1, '顶极节点');
$this->link = $link;
$this->table = $table;
$node = array();
$results = mysql_query('select * from '.$this->table.'',$this->link);
while($node = mysql_fetch_assoc($results)){
$this->setNode($node['cid'],$node['pid'],$node['cname']);
}
}
function setNode ($id, $parent, $value){
$parent = $parent?$parent:0;
$this->data[$id] = $value;
$this->child[$id] = array();
$this->child[$parent][] = $id;
$this->parent[$id] = $parent;
$this->layer[$id] = !isset($this->layer[$parent])? 0 : $this->layer[$parent] + 1;
}
function getList (&$tree, $root= 0){
foreach ($this->child[$root] as $key=>$id){
$tree[] = $id;
if ($this->child[$id]) $this->getList($tree, $id);
}
}
function getValue ($id){return $this->data[$id];}
function getLayer ($id, $space = false){
return $space?str_repeat($space, $this->layer[$id]):$this->layer[$id];
}
function getParent ($id){return $this->parent[$id];}
function getParents ($id){
while ($this->parent[$id] != -1){
$id = $parent[$this->layer[$id]] = $this->parent[$id];
}
ksort($parent);
reset($parent);
return $parent;
}
function getChild ($id){return $this->child[$id];}
function getChilds ($id = 0){
$child = array($id);
$this->getList($child, $id);
return $child;
}
function addNode($name,$pid){
mysql_query("insert into $this->table (`pid`,`cname`) values ('$pid','$name')",$this->link);
}
function modNode($cid, $newName){
mysql_query("update $this->table set `cname`='$newName' where `cid` = $cid",$this->link);
}
function delNode($cid){
$allChilds = $this->getChilds($cid);
$sql ='';
if(empty($allChilds)){
$sql = "delete from $this->table where `cid` = $cid";
}else{
$sql = 'delete from '.$this->table.' where `cid` in ('.implode(',',$allChilds).','.$cid.')';
}
mysql_query($sql,$this->link);
}
function moveNode($cid, $topid){
mysql_query("update $this->table set `pid`=$topid where `cid` = $cid", $this->link);
}
}
//函数
function back(){
echo '<script language="java script">window.location.href="test.php?"+new Date().getTime();</script>';
exit;
}
//声成select
function makeSelect($array,$formName){
global $tree;
$select = '<select name="'.$formName.'">';
foreach ($array as $id){
$select.='<option value="'.$id.'">'.$tree->getLayer($id, '|-').$tree->getValue($id)."</option>";
}
return $select.'</select>';
}
$tree = new SortClass($link,'`class`');
$op = !empty($_POST['op']) ? $_POST['op'] : $_GET['op'];
if(!empty($op)){
if($op=='add'){
$tree->addNode($_POST['cname'],$_POST['pid']);
back();
}
if($op=='mod'){
$tree->modNode($_POST['cid'],$_POST['cname']);
back();
}
if($op=='del'){
$tree->delNode($_GET['cid']);
back();
}
if($op=='move'){
$tree->moveNode($_POST['who'],$_POST['to']);
back();
}
}
$category = $tree->getChilds();
?>
css样式
复制代码
<style type="text/css">
body{font-size:12px;}
ul{list-style:none;}
a{cursor:pointer;}
</style>
JS代码
复制代码
<script language="java script">
function $(e){return document.getElementById(e);}
function mod(cid){
$('cid').value=cid;
$('op').value='mod';
$('name').style.border='1px solid red';
}
</script>
HTML代码部分
复制代码
<form action="test.php" method="post">
名称:<input type="text" id="name" name="cname" /> 添加到:<?=makeSelect($category,'pid')?><br />
<input type="hidden" id="op" name="op" value="add" />
<input type="hidden" id="cid" name="cid" />
<input type="submit" value="Submit" />
</form>
<h3>移动分类</h3>
<form action="test.php" method="post">
<?=makeSelect($category,'who')?>移动到:<?=makeSelect($category,'to')?>
<input type="hidden" id="op" name="op" value="move" />
<input type="submit" value="Submit" />
</form>
<ul>
<?php
foreach ($category as $id){
echo '<li>'.$tree->getLayer($id, '|- ').$tree->getValue($id).' <a href="test.php?op=del&cid='.$id.'">Del</a> <a onclick="mod('.$id.')">Edit</a> </li>';
}
?>
</ul>
php 中英文验证码程序
$code="";
for($i=0;$i<4;$i++){
$Xi=mt_rand(0,strlen($str)/2);
if($Xi%2) $Xi+=1;
$code.=substr($str,$Xi,2);
}
$_SESSION['code']=$code; //赋值给SESSION
$im=imagecreatetruecolor($w,$h);
$bkcolor=imagecolorallocate($im,250,250,250);
imagefill($im,0,0,$bkcolor);
/***添加干扰***/
for($i=0;$i<15;$i++){
$fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagearc($im,mt_rand(-10,$w),mt_rand(-10,$h),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
}
for($i=0;$i<255;$i++){
$fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagesetpixel($im,mt_rand(0,$w),mt_rand(0,$h),$fontcolor);
}
/***********内容*********/
for($i=0;$i<4;$i++){
$fontcolor=imagecolorallocate($im,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120)); //这样保证随机出来的颜色较深。
$codex=iconv("GB2312","UTF-8",substr($code,$i*2,2));
imagettftext($im,mt_rand(20,24),mt_rand(-60,60),40*$i+20,mt_rand(30,35),$fontcolor,$fontface,$codex);
}
imagepng($im);
?>
do... while语句将始终执行的代码块一次,然后检查的条件,并重复循环,而条件是真实的。
语法
do
{
code to be executed;
}
while (condition);
例如
下面的例子定义了一个循环,开始与i = 1。然后它将增加1我和写一些输出。然后,条件是检查,循环将继续运行像我只要小于或等于5:
<html>
<body>
<?php
$i=1;
do
{
$i++;
echo "The number is " . $i . "<br />";
}
while ($i<=5);
?>
</body>
</html>
<?php
/* example 1 */
$i = 1;
while ($i <= 10) {
echo $i++; /* the printed value would be
$i before the increment
(post-increment) */
}
/* example 2 */
$i = 1;
while ($i <= 10):
echo $i;
$i++;
endwhile;
?>
<?php
$q1 = 'some query on a set of tables';
$q2 = 'similar query on a another set of tables';
if ( ($r1=mysql_query($q1)) && ($r2=mysql_query($q2)) ) {
while (($row=mysql_fetch_assoc($r1))||($row=mysql_fetch_assoc($r2))) {
/* do something with $row coming from $r1 and $r2 */
}
}
?>
<select name="whatever">
<?php
while ($data = mysql_fetch_assoc($requeteID))
{
$menu .= '<option value="'.$data['id'].'"';
$menu .= ($data['id'] == $_GET['id'] ? ' selected>' :'>');
$menu .= $data['name'].'</option>';
}
echo $menu;
?>
</select>
php while loop next
循环执行的代码块中指定的次数,或在指定的条件为真。
-------------------------------------------------- ------------------------------
PHP的循环
很多时候,当您编写代码,您希望相同的代码块来运行十多年,再行。而不是添加在脚本中我们可以使用循环来执行这样的任务数几乎相等线。
在PHP中,我们有以下循环语句:
同时通过一个代码块 - 循环,而指定的条件为真
不...而 - 遍历一个代码块一次,然后重复像长循环指定的条件为真
为 - 循环通过一个代码块中指定的次数
Foreach源 - 通过对每个元素的代码块循环中的一个数组
-------------------------------------------------- ------------------------------
while循环
while循环执行的代码块,而条件是正确的。
语法
while (condition)
{
code to be executed;
}
例如
下面的例子定义了一个循环,开始与i = 1。循环将继续运行,因为我只要小于或等于5。我将增加1每次循环运行:
html>
<body>
<?php
$i=1;
while($i<=5)
{
echo "The number is " . $i . "<br />";
$i++;
}
?>
</body>
</html>
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
php loop continue执行的代码块中指定的次数,或在指定的条件为真。
-------------------------------------------------- ------------------------------
对于循环
循环使用的是当你事先知道多少次的脚本应该运行。
语法
for (init; condition; increment)
{
code to be executed;
}
参数:
初始化:主要用于设置一个计数器(但可以是任何代码执行在一次循环的开始)
条件:评估每个循环迭代。如果值为TRUE,则循环继续。如果值为FALSE,则循环结束。
增量:主要用于增加一个计数器(但可以是任何代码将在循环结束时执行)
注:以上可以是空的,或者有多个表达式每个参数(以逗号分隔)。
例如
下面的例子定义了一个循环,开始与i = 1。循环将继续运行,因为我只要小于或等于5。我将增加1每次循环运行:
<html>
<body>
<?php
for ($i=1; $i<=5; $i++)
{
echo "The number is " . $i . "<br />";
}
?>
</body>
</html>
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
实例二
<?php
while (list($key, $value) = each($arr)) {
if (!($key % 2)) { // skip odd members
continue;
}
do_something_odd($value);
}
$i = 0;
while ($i++ < 5) {
echo "Outer<br />n";
while (1) {
echo " Middle<br />n";
while (1) {
echo " Inner<br />n";
continue 3;
}
echo "This never gets output.<br />n";
}
echo "Neither does this.<br />n";
}
?>
<?php
for ($i = 0; $i < 5; ++$i) {
if ($i == 2)
continue
print "$in";
}
?>
相关文章
- 这篇文章主要介绍了源码分析系列之json_encode()如何转化一个对象,对json_encode()感兴趣的同学,可以参考下...2021-04-22
- PHP去除html、css样式、js格式的方法很多,但发现,它们基本都有一个弊端:空格往往清除不了 经过不断的研究,最终找到了一个理想的去除html包括空格css样式、js 的PHP函数。...2013-08-02
- 本文实例讲述了JS+CSS实现分类动态选择及移动功能效果代码。分享给大家供大家参考,具体如下:这是一个类似选项卡功能的选择插件,与普通的TAb区别是加入了动画效果,多用于商品类网站,用作商品分类功能,不过其它网站也可以用,...2015-10-21
- index.php怎么打开?初学者可能不知道如何打开index.php,不会的同学可以参考一下本篇教程 打开编辑:右键->打开方式->经文本方式打开打开运行:首先你要有个支持运行PH...2017-07-06
- 无限级分类在开发中经常使用,例如:部门结构、文章分类。无限级分类的难点在于“输出”和“查询”,例如 将文章分类输出为<ul>列表形式; 查找分类A下面所有分类包含的文章。1.实现原理 几种常见的实现方法,各有利弊。其中...2015-10-23
- 在一些复杂的系统中,要求对信息栏目进行无限级的分类,以增强系统的灵活性。那么PHP是如何实现无限级分类的呢?我们在本文中使用递归算法并结合mysql数据表实现无限级分类。 递归,简单的说就是一段程序代码的重复调用,当把...2015-10-23
PHP中func_get_args(),func_get_arg(),func_num_args()的区别
复制代码 代码如下:<?php function jb51(){ print_r(func_get_args()); echo "<br>"; echo func_get_arg(1); echo "<br>"; echo func_num_args(); } jb51("www","j...2013-10-04- ecshop商品无限级分类代码 function cat_options($spec_cat_id, $arr) { static $cat_options = array(); if (isset($cat_options[$spec_cat_id]))...2016-11-25
- 这篇文章主要介绍了PHP编程 SSO详细介绍及简单实例的相关资料,这里介绍了三种模式跨子域单点登陆、完全跨单点域登陆、站群共享身份认证,需要的朋友可以参考下...2017-01-25
- 这篇文章主要介绍了PHP实现创建以太坊钱包转账等功能,对以太坊感兴趣的同学,可以参考下...2021-04-20
- 这篇文章主要为大家详细介绍了php微信公众账号开发之五个坑,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-10-02
ThinkPHP使用心得分享-ThinkPHP + Ajax 实现2级联动下拉菜单
首先是数据库的设计。分类表叫cate.我做的是分类数据的二级联动,数据需要的字段有:id,name(中文名),pid(父id). 父id的设置: 若数据没有上一级,则父id为0,若有上级,则父id为上一级的id。数据库有内容后,就可以开始写代码,进...2014-05-31- 这篇文章主要介绍了PHP如何通过date() 函数格式化显示时间,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-11-13
- 一、数据库设计 -- -- Table structure for table `category` -- CREATE TABLE `category` ( `id` int(11) NOT NULL auto_increment, `catpath` varchar(255) default NULL, `name` varchar(255) default NULL...2015-11-08
- 今天我给大家分享的是在不刷新页面的前提下,使用PHP+jQuery+Ajax实现多图片上传的效果。用户只需要点击选择要上传的图片,然后图片自动上传到服务器上并展示在页面上。...2015-03-15
- 这篇文章主要介绍了Python深度学习之简单实现猫狗图像分类,文中有非常详细的代码示例,对正在学习python的小伙伴们有非常好的帮助,需要的朋友可以参考下...2021-04-29
- 这篇文章主要介绍了golang与php实现计算两个经纬度之间距离的方法,结合实例形式对比分析了Go语言与php进行经纬度计算的相关数学运算技巧,需要的朋友可以参考下...2016-07-29
- 这篇文章主要介绍了PHP如何使用cURL实现Get和Post请求,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-07-11
- 这篇文章主要介绍了PHP正则表达式过滤html标签属性的相关内容,实用性非常,感兴趣的朋友参考下吧...2016-05-06
- 经常看到有人踩在了PHP路径的坑上面了,感觉有必要来说说PHP中相对路径的一些坑,以及PHP中绝对路径的使用,下面一起来看看。 ...2016-08-24