标题: PHP教程之写不下了 [打印本页] 作者: 愤怒的大鸟 时间: 2015-2-4 00:16 标题: PHP教程之写不下了 刚开始写页面程序,调试完书中的例子。然后就可以尝试编写留言板了, ==================================================
file name:
dboper.inc.php
codes:
==================================================
<?
class dboper {
var $items;
var $result=0;
function find($l_value) {
for($j=1;$j<=$this->items["info_all"];$j++) {
if($l_value==$this->items["d_id][$j]"])
{ return($j);exit; }
}
}
function del($id) {
//echo "del"." ".$id."<br>";
include("conn_db.php");
$Sql_Query="delete from department where d_id=$id";
$Query_Db=mysql_query($Sql_Query,$Connect);
//echo $Sql_Query;
include("conn_close.php");
}
function update($d_id,$f_node,$l_node,$r_node) {
include("conn_db.php");
$Sql_Query="update department set f_node=".$f_node.",l_node=".$l_node.",r_node=".$r_node." where d_id=".$d_id;
$Query_Db=mysql_query($Sql_Query,$Connect);
//echo $Sql_Query;
include("conn_close.php");
}
function d_tree($id,$base_id){//
//echo "do<br>";
$m=$this->find($base_id);//m is the position of $base_id in arrays
$i=$this->find($id);
if(($this->items["r_node][$i]"]==0)&&($this->items["l_node][$i]"]==0))
{
if($this->result==1){//very important method to exit my function
return true;
}
$this->del($this->items["d_id][$i]"]);
$j=$i;//save this node information
$i=$this->find($this->items["f_node][$i]"]);//find it's parent node
//begin-----------------------------------------------------
//if this node is the node you want to delete,delete it and exit function
if(($this->items["r_node][$i]"]==$base_id)||($this->items["l_node][$i]"]==$base_id))
{
$this->result=1;
return true;
}
//end--------------------------------------------------------
else {
//begin------------------------------------------------------
//if this node is the child node of you wanted to deleted node
//up to parent node and continue find
if($this->items["r_node][$i]"]==$this->items["d_id][$j]"])
{
//echo "左上一级";
//echo "<br>";
$this->items["r_node][$i]"]=0;
$this->d_tree($this->items["d_id][$i]"],$base_id);
//exit;
}
function d_node_tree($id,$base_id) {
$m=$this->find($base_id);//m is the position of $base_id in arrays
$i=$this->find($id);
if(($this->items["l_node][$m]"]!=0))
{
$i=$this->find($this->items["l_node][$m]"]);//current node's left child node
$j=$this->find($this->items["f_node][$m]"]);//current node's parent
$this->d_tree($this->items["d_id][$i]"],$this->items["d_id][$i]"]);//del whole left child tree
if($this->items["r_node][$m]"]!=0) {
if($this->items["r_node][$j]"]==$this->items["d_id][$m]"])//if it's parent's right child node
{
$i=$this->find($this->items["r_node][$m]"]);
$this->items["r_node][$j]"]=$this->items["d_id][$i]"];
$this->update($this->items["d_id][$j]"],$this->items["f_node][$j]"],$this->items["l_node][$j]"],$this->items["r_node][$j]"]);
//update where id = $this->items["d_id][$j]"]
$this->items["f_node][$i]"]=$this->items["d_id][$j]"];
$this->update($this->items["d_id][$i]"],$this->items["f_node][$i]"],$this->items["l_node][$i]"],$this->items["r_node][$i]"]);
//update where id = $this->items["d_id][$i]"]
}
if($this->items["l_node][$j]"]==$this->items["d_id][$m]"])//if it's parent's right child node
{
$i=$this->find($this->items["r_node][$m]"]);
$this->items["l_node][$j]"]=$this->items["d_id][$i]"];
$this->update($this->items["d_id][$j]"],$this->items["f_node][$j]"],$this->items["l_node][$j]"],$this->items["r_node][$j]"]);
//update where id = $this->items["d_id][$j]"]
$this->items["f_node][$i]"]=$this->items["d_id][$j]"];
$this->update($this->items["d_id][$i]"],$this->items["f_node][$i]"],$this->items["l_node][$i]"],$this->items["r_node][$i]"]);
//update where id = $this->items["d_id][$i]"]
}
$this->del($this->items["d_id][$m]"]);
}
if($this->items["r_node][$m]"]==0) {
if($this->items["r_node][$j]"]==$this->items["d_id][$m]"])//if it's parent's right child node
{
$this->items["r_node][$j]"]=0;
$this->update($this->items["d_id][$j]"],$this->items["f_node][$j]"],$this->items["l_node][$j]"],$this->items["r_node][$j]"]);
//update where id = $this->items["d_id][$j]"]
}
if($this->items["l_node][$j]"]==$this->items["d_id][$m]"])//if it's parent's right child node
{
$this->items["l_node][$j]"]=0;
$this->update($this->items["d_id][$j]"],$this->items["f_node][$j]"],$this->items["l_node][$j]"],$this->items["r_node][$j]"]);
//update where id = $this->items["d_id][$j]"]
}
$this->del($this->items["d_id][$m]"]);//del node it self
}
return true;
}
$j=$this->find($this->items["f_node][$m]"]);//current node's parent
if($this->items["r_node][$j]"]==$this->items["d_id][$m]"])//if it's parent's right child node
{
$i=$this->find($this->items["r_node][$m]"]);//current node's right child node
$this->update($this->items["d_id][$j]"],$this->items["f_node][$j]"],$this->items["l_node][$j]"],$this->items["d_id][$i]"]);
//update where id = $this->items["d_id][$j]"]
$this->update($this->items["d_id][$i]"],$this->items["d_id][$j]"],$this->items["l_node][$i]"],$this->items["r_node][$i]"]);
//update where id = $this->items["d_id][$i]"]
}
if($this->items["l_node][$j]"]==$this->items["d_id][$m]"])//if it's parent's right child node
{
$i=$this->find($this->items["r_node][$m]"]);
$this->update($this->items["d_id][$j]"],$this->items["f_node][$j]"],$this->items["d_id][$i]"],$this->items["r_node][$j]"]);
//update where id = $this->items["d_id][$j]"]
$this->items["f_node][$i]"]=$this->items["d_id][$j]"];
$this->update($this->items["d_id][$i]"],$this->items["d_id][$j]"],$this->items["l_node][$i]"],$this->items["r_node][$i]"]);
//update where id = $this->items["d_id][$i]"]
}
}
if($this->items["r_node][$m]"]==0) {
$j=$this->find($this->items["f_node][$m]"]);//current node's parent
if($this->items["r_node][$j]"]==$this->items["d_id][$m]"])
{
$this->items["r_node][$j]"]=0;
$this->update($this->items["d_id][$j]"],$this->items["f_node][$j]"],$this->items["l_node][$j]"],$this->items["r_node][$j]"]);
//update where id = $this->items["d_id][$j]"]
}
if($this->items["l_node][$j]"]==$this->items["d_id][$m]"])
{
$this->items["l_node][$j]"]=0;
$this->update($this->items["d_id][$j]"],$this->items["f_node][$j]"],$this->items["l_node][$j]"],$this->items["r_node][$j]"]);
//update where id = $this->items["d_id][$j]"]
}
}
$this->del($this->items["d_id][$m]"]);
return true;
}
}
function showinfo_0($status){//
include("conn_db.php");
$Sql_Query="select * from department order by d_id";
$Query_Db=mysql_query($Sql_Query,$Connect);
$info_all=mysql_num_rows($Query_Db);
$this->items["info_all"]=$info_all;
if($status==1) {//when status=1 show all data gets from database
$MNums=$info_all;
}
if($status==0) {//when status=0 show 6 messages gets from database
$MNums=6;
}
for($i=1;$i<=$MNums;$i++)
{
$Messages=mysql_fetch_array($Query_Db);
$this->items["d_id][$i]"]=$Messages["d_id"];
$this->items["f_node][$i]"]=$Messages["f_node"];
$this->items["node_value][$i]"]=$Messages["node_value"];
$this->items["l_node][$i]"]=$Messages["l_node"];
$this->items["r_node][$i]"]=$Messages["r_node"];
}
include("conn_close.php");
}
}
?>
==================================================
file name:
conn_db.php
codes:
==================================================
<?
$Connect = @mysql_connect('localhost','root','') or die("could not connet to server please try later");
mysql_select_db("testxxx",$Connect);
?>
function find($l_value) {
for($j=1;$j<=$this->items["info_all"];$j++) {
if($l_value==$this->items["d_id][$j]"])
{ return($j);exit; }
}
}
function search_id() {
include("conn_db.php");
$Sql_Query="select * from department order by d_id desc";
$Query_Db=mysql_query($Sql_Query,$Connect);
$Messages=mysql_fetch_array($Query_Db);
include("conn_close.php");
return $Messages["d_id"];
}
function insertnode($f_node,$node_value,$r_node) {
include("conn_db.php");
$Sql_Query="insert into department values('','$f_node','$node_value','0','$r_node')";
$Query_Db=mysql_query($Sql_Query,$Connect);
include("conn_close.php");
}
function update($d_id,$f_node,$l_node,$r_node) {
include("conn_db.php");
$Sql_Query="update department set f_node=".$f_node.",l_node=".$l_node.",r_node=".$r_node." where d_id=".$d_id;
$Query_Db=mysql_query($Sql_Query,$Connect);
//echo $Sql_Query;
include("conn_close.php");
}
function add($id,$node_value) {
$i=$this->find($id);
if(($this->items["l_node][$i]"]!=0))
{
$j=$this->find($this->items["l_node][$i]"]);
function showinfo_0($status){//
include("conn_db.php");
$Sql_Query="select * from department order by d_id";
$Query_Db=mysql_query($Sql_Query,$Connect);
$info_all=mysql_num_rows($Query_Db);
$this->items["info_all"]=$info_all;
if($status==1) {//when status=1 show all data gets from database
$MNums=$info_all;
}
if($status==0) {//when status=0 show 6 messages gets from database
$MNums=6;
}
for($i=1;$i<=$MNums;$i++)
{
$Messages=mysql_fetch_array($Query_Db);
$this->items["d_id][$i]"]=$Messages["d_id"];
$this->items["f_node][$i]"]=$Messages["f_node"];
$this->items["node_value][$i]"]=$Messages["node_value"];
$this->items["l_node][$i]"]=$Messages["l_node"];
$this->items["r_node][$i]"]=$Messages["r_node"];
}
include("conn_close.php");
}