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["l_node][$i]"]!=0))
{
//echo "向左下一级";
//echo "<br>";
$i=$this->find($this->items["l_node][$i]"]);
$this->d_tree($this->items["d_id][$i]"],$base_id);
//exit;
}
if(($this->items["r_node][$i]"]!=0)&&($this->items["l_node][$i]"]==0))
{
//echo "向右下一级";
//echo "<br>";
$i=$this->find($this->items["r_node][$i]"]);
$this->d_tree($this->items["d_id][$i]"],$base_id);
//exit;
}
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;
}
if($this->items["l_node][$i]"]==$this->items["d_id][$j]"])
{
//echo "右上一级";
//echo "<br>";
$this->items["l_node][$i]"]=0;
$this->d_tree($this->items["d_id][$i]"],$base_id);
//exit;
}
//end----------------------------------------------------------
}
}
}
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;
}
if(($this->items["l_node][$m]"]==0))
{
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]"])//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);
?>
==================================================
file name:
addnode.inc.php
codes:
==================================================
<?
class addnode{
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]"]);
$this->update($this->items["d_id][$j]"],0,$this->items["l_node][$j]"],$this->items["r_node][$j]"]);
$this->insertnode($this->items["d_id][$i]"],$node_value,$this->items["d_id][$j]"]);
$c_d_id=$this->search_id();
$this->update($this->items["d_id][$i]"],$this->items["f_node][$i]"],$c_d_id,$this->items["r_node][$i]"]);
$this->update($this->items["d_id][$j]"],$c_d_id,$this->items["l_node][$j]"],$this->items["r_node][$j]"]);
}
if(($this->items["l_node][$i]"]==0))
{
$this->insertnode($this->items["d_id][$i]"],$node_value,0);
$this->update($this->items["d_id][$i]"],$this->items["f_node][$i]"],$this->search_id($this->items["d_id][$i]"]),$this->items["r_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");
}
}
?>
学习了六个多月PHP了,还是个新手,在这里受到了很多人的帮助,谢谢你们! Ps:以上纯属原创,如有雷同,纯属巧合 多看优秀程序员编写的代码,仔细理解他们解决问题的方法,对自身有很大的帮助。 我还是强烈建议自己搭建php环境。因为在搭建的过程中你会遇到一些问题,通过搜索或是看php手册解决问题后,你会更加深刻的理解它们的工作原理,了解到php配置文件中的一些选项设置。 再就是混迹于论坛啦,咱们的phpchina的论坛就很强大,提出的问题一般都是有达人去解答的,以前的帖子也要多看看也能学到不少前辈们的经验。别的不错的论坛例如php100,javaeye也是很不错的。 这些中手常用的知识,当你把我说的这些关键字都可以熟练运用的时候,你可以选择自己 刚开始安装php的时候,我图了个省事,把php的扩展全都打开啦(就是把php.ini 那一片 extension 前面的冒号全去掉啦),这样自然有好处,以后不用再需要什么功能再来打开。 开发工具也会慢慢的更专业,每个公司的可能不一样,但是zend studio是个大伙都会用的。 这些中手常用的知识,当你把我说的这些关键字都可以熟练运用的时候,你可以选择自己 遇到出错的时候,我经常把错误信息直接复制到 google的搜索栏,一般情况都是能搜到结果的,不过有时候会搜出来一大片英文的出来,这时候就得过滤一下,吧中文的弄出来,挨着式方法。 在学习的过程中不能怕麻烦,不能有懒惰的思想。学习php首先应该搭建一个lamp环境或者是wamp环境。这是学习php开发的根本。虽然网络上有很多集成的环境,安装很方便,使用起来也很稳定、 个人呢觉得,配wamp 最容易漏的一步就是忘了把$PHP$目录下的libmysql.dll拷贝到windows系统目录的system32目录下,还有重启apache。 有位前辈曾经跟我说过,phper 至少要掌握200个函数 编起程序来才能顺畅点,那些不熟悉的函数记不住也要一拿手册就能找到。所以建议新手们没事就看看php的手册(至少array函数和string函数是要记牢的)。 开发工具也会慢慢的更专业,每个公司的可能不一样,但是zend studio是个大伙都会用的。 首先我是坚决反对新手上来就用框架的,因为对底层的东西一点都不了解,造成知识上的真空,会对以后的发展不利。我的观点上手了解下框架就好,代码还是手写。当然啦如果是位别的编程语言的高手的话,这个就另当别论啦。 首先声明:我是一个菜鸟,是一个初学者。学习了一段php后总是感觉自己没有提高,无奈。经过反思我认为我学习过程中存在很多问题,我改变了学习方法后自我感觉有了明显的进步。 ,熟悉html,能用div+css,还有javascript,优先考虑linux。我在开始学习的时候,就想把这些知识一起学习,我天真的认为同时学习能够互相呼应,因为知识是相通的。 开发工具也会慢慢的更专业,每个公司的可能不一样,但是zend studio是个大伙都会用的。 对于初学者来说不推荐去拿钱买的。当然如果一个网站你经常去用,而且里面的资料也比较有用,最好还是买个会员比较好,毕竟那些也是别人的工作成果。 如果你可以写完像留言板这样的程序,那么你可以去一些别人的代码了,
页:
[1]
2