首页 > php代码

php无限级分页代码

php无限级分页代码
class Tree
{
        public $data=array();
        public $cateArray=array();
        
        function Tree()
        {
               
        }
        function setNode ($id, $parent, $value)
    {
        $parent = $parent?$parent:0;
        $this->data[$id]                = $value;
        $this->cateArray[$id]        = $parent;
    }
    function getChildsTree($id=0)
    {
            $childs=array();
            foreach ($this->cateArray as $child=>$parent)
            {
                    if ($parent==$id)
                    {
                            $childs[$child]=$this->getChildsTree($child);
                    }
                    
            }
            return $childs;
    }
    function getChilds($id=0)
    {
            $childArray=array();
            $childs=$this->getChild($id);
            foreach ($childs as $child)
            {
                    $childArray[]=$child;
                    $childArray=array_merge($childArray,$this->getChilds($child));
            }
            return $childArray;
    }
    function getChild($id)
    {
            $childs=array();
            foreach ($this->cateArray as $child=>$parent)
            {
                    if ($parent==$id)
                    {
                            $childs[$child]=$child;
                    }
            }
            return $childs;
    }
    //单线获取父节点
    function getNodeLever($id)
    {
            $parents=array();
            if (key_exists($this->cateArray[$id],$this->cateArray))
            {
                    $parents[]=$this->cateArray[$id];
                    $parents=array_merge($parents,$this->getNodeLever($this->cateArray[$id]));
            }
            return $parents;
    }
    function getLayer($id,$preStr='|-')
    {
            return str_repeat($preStr,count($this->getNodeLever($id)));
    }
    function getValue ($id)
    {
        return $this->data[$id];
    } // end func
}

/*$Tree = new Tree("请选择分类");
//setNode(目录ID,上级ID,目录名字);
$Tree->setNode(1, 0, '目录1');
$Tree->setNode(2, 1, '目录2');
$Tree->setNode(5, 3, '目录5');
$Tree->setNode(3, 0, '目录3');
$Tree->setNode(4, 2, '目录4');
$Tree->setNode(9, 4, '目录9');
$Tree->setNode(6, 2, '目录6');
$Tree->setNode(7, 2, '目录7');
$Tree->setNode(8, 3, '目录8');

//print_r($Tree->getChildsTree(0));
//print_r($Tree->getChild(0));
//print_r($Tree->getLayer(2));

$category = $Tree->getChilds();

//遍历输出
foreach ($category as $key=>$id)
{
    echo $id.$Tree->getLayer($id, '|-').$Tree->getValue($id)."n";
}*/

?>

阅读全文

正则表达式一

4.per正则函数
    1.preg_grep函数
        preg_grep(pattern,array input);
        输入数组input中寻找匹配模式pattern的字串,并将所有的匹配字符串返回。返回值就是所有匹配的字符串组成的数组。
    2.preg_match函数
        preg_match(pattern,string subject,[array matches])
        该函数在subject字符串中寻找匹配pattern的字符串。如果找到则返回一个非零值,否则返回零值。如果选用了可选项matches,那么匹配的字符串将被放到第一个元素的位置,可以用$matches[0]来读取,圆括号匹配的结果也按顺序放在这个数组中,第一个是$matches[1],第二个是$matches[2],依次类推。
    3.preg_match_all函数
        preg_match_all(pattern,subject,array matches,[int order])
        该函数在subject字符串中寻找匹配pattern的互不重叠的文本,找到了匹配的文本则返回匹配文本的个数,否则返回0。匹配文本被放在二维数组matches中,matches[0]中存放的是所有符合的字符串。各种嵌入的子模式匹配的结果依次放在数组matches[1]~[n]中。
        order参数可选,可取的值为PREG_PATTERN_ORDER和PREG_SET_ORDER。
    4.preg_replace函数
        preg_replace(pattern,replacement,subject,[int limit])
        该函数将subject中符合pattern模式的部分替换成replacement,返回值类型和subject类型一样,如果有替换,则返回替换后的值,反之则返回原来的值。
        参数可以是数组也可以是变量,有几种情况:
            <1>如果subject参数是数组类型。函数对每一个数组元素进行替换操作;
            <2>如果pattern是数组则函数根据每一个pattern中的类型进行替换;
            <3>如果pattern和replacement都是数组,则按两个数组中的元素对应完成替换;
            <4>如果replacement中的元素个数少于pattern中的元素个数。那么不够的部分将有空字符串来代替。
    5.preg_split函数
        preg_split(pattern,subject,[int limit][flages])
        该函数以pattern定义的模式为分隔符将subject字符串分隔为若干个部分,返回数组,其中存放被分隔后的字符串。limit可限制返回字符串的数目,如果设置为-1表示对返回的字符串数目不加任何限制。flags也是可选项,其有两个值:PREG_SPLIT_NO_EMPTY设定函数不返回空字符串,PERG_SPLIT_DELIM_CAPTURE,该选项设定pattern中的嵌入子模式也会被函数匹配。

阅读全文

php操作access数据


<?php
//--------------------------------------------------------------------
//FileName:class.php
//Summary: Access数据库操作类
//CreateTime: 2006-8-10    
//LastModifed:
//copyright (c)2006 freeweb.nyist.net/~chairy  [email]chaizuxue@163.com[/email]
//   使用范例:
//$databasepath="database.mdb";
//$dbusername="";
//$dbpassword="";
//include_once("class.php");
//$access=new Access($databasepath,$dbusername,$dbpassword);
//--------------------------------------------------------------------
    class Access
    {
         var $databasepath,$constr,$dbusername,$dbpassword,$link;
         function Access($databasepath,$dbusername,$dbpassword)
         {
               $this->databasepath=$databasepath;
        $this->username=$dbusername;
        $this->password=$dbpassword;
        $this->connect();
          }
       
    function connect()
    {
        $this->constr="DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath($this->databasepath);
        $this->link=odbc_connect($this->constr,$this->username,$this->password,SQL_CUR_USE_ODBC);
        return $this->link;
        if($this->link) echo "恭喜你,数据库连接成功!";
        else echo "数据库连接失败!";
    }
       
    function query($sql)
    {
        return @odbc_exec($this->link,$sql);
    }
       
    function first_array($sql)
    {
        return odbc_fetch_array($this->query($sql));
    }
       
    function fetch_row($query)
    {
        return odbc_fetch_row($query);
    }
       
    function total_num($sql)//取得记录总数
    {
        return odbc_num_rows($this->query($sql));
    }
       
    function close()//关闭数据库连接函数
    {   
        odbc_close($this->link);
    }
           
    function insert($table,$field)//插入记录函数
    {
        $temp=explode(',',$field);
        $ins='';
        for ($i=0;$i<count($temp);$i++)
        {
            $ins.="'".$_POST[$temp[$i]]."',";
        }
        $ins=substr($ins,0,-1);
        $sql="INSERT INTO ".$table." (".$field.") VALUES (".$ins.")";
        $this->query($sql);
    }
       
    function getinfo($table,$field,$id,$colnum)//取得当条记录详细信息
    {
        $sql="SELECT * FROM ".$table." WHERE ".$field."=".$id."";
        $query=$this->query($sql);
        if($this->fetch_row($query))
        {
            for ($i=1;$i<$colnum;$i++)
            {
          $info[$i]=odbc_result($query,$i);
             }
         }
         return $info;
    }
       
    function getlist($table,$field,$colnum,$condition,$sort="ORDER BY id DESC")//取得记录列表   
    {
         $sql="SELECT * FROM ".$table." ".$condition." ".$sort;
         $query=$this->query($sql);
         $i=0;
         while ($this->fetch_row($query))
         {
        $recordlist[$i]=getinfo($table,$field,odbc_result($query,1),$colnum);
        $i++;
          }
          return $recordlist;
    }
       
    function getfieldlist($table,$field,$fieldnum,$condition="",$sort="")//取得记录列表
    {
         $sql="SELECT ".$field." FROM ".$table." ".$condition." ".$sort;
         $query=$this->query($sql);
         $i=0;
         while ($this->fetch_row($query))
         {
         for ($j=0;$j<$fieldnum;$j++)
        {
                   $info[$j]=@odbc_result($query,$j+1);
        }   
        $rdlist[$i]=$info;
        $i++;
         }
         return $rdlist;
    }
       
    function updateinfo($table,$field,$id,$set)//更新记录
    {
        $sql="UPDATE ".$table." SET ".$set." WHERE ".$field."=".$id;
        $this->query($sql);
    }
       
    function deleteinfo($table,$field,$id)//删除记录
    {
         $sql="DELETE FROM ".$table." WHERE ".$field."=".$id;
         $this->query($sql);
    }
       
    function deleterecord($table,$condition)//删除指定条件的记录
    {
         $sql="DELETE FROM ".$table." WHERE ".$condition;
         $this->query($sql);
    }
       
    function getcondrecord($table,$condition="")// 取得指定条件的记录数
    {
         $sql="SELECT COUNT(*) AS num FROM ".$table." ".$condition;
         $query=$this->query($sql);
         $this->fetch_row($query);
         $num=odbc_result($query,1);
         return $num;           
    }
     }
?>
 

阅读全文

简单php 图片上传代码

简单php 图片上传代码 php 上传图片代

<?php

$zpname=addslashes(isset($_POST['zpname'])?$_POST['zpname']:'');
$zpurl=isset($_POST['zpurl'])?$_POST['zpurl']:'';
$zpsay=addslashes(isset($_POST['zpsay'])?$_POST['zpsay']:'');
$blueidea=isset($_POST['blueidea'])?$_POST['blueidea']:'';
$up_path="../zp/";
$up_size=100000;
$up_name=md5(date("Y:m:d H:i:s"));
$up_type=$_FILES['file']['type'];
$up_exten="start:image/gifimage/pjpegimage/x-png";
echo $up_type;
echo(strrpos($up_exten,$up_type));
//exit();
//=$up_wh['mime'];
$up_sava='';
//echo($_FILES['file']['size']);
//echo($_FILES['file']['type']);
//exit();

阅读全文

php删除目录和文件


deleteDir($dir) //这段删除文件和目录的代码可以放在win lin u系统删除,
{
if (rmdir($dir)==false && is_dir($dir)) {
 if ($dp = opendir($dir)) {
  while (($file=readdir($dp)) != false) {
   if (is_dir($file) && $file!='.' && $file!='..') {
    deleteDir($file);
   } else {
    unlink($file);
   }
  }
  closedir($dp);
 } else {
  exit('Not permission');
 }

}
下面再来一种删除目录与文件的方法,不过这种只能在win下执行了.
function del_dir($dir)
{
if(strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
    $str = "rmdir /s/q " . $dir;
} else {
    $str = "rm -Rf " . $dir;
}
}

阅读全文

php生成 google map代码

php生成 google map代码

<title>Google Sitemap生成器</title>
<style type="text/css">
<!--
.agreement {
 border: 1px solid #666666;
}
.green {
 color: #00CC00;
 font-weight: bold;
}
.red {color: #FF0000; font-weight: bold; }
#scroll {
 border: 1px solid #0000FF;
 line-height: 25px;
 height: 25px;
 width: 280px;
 text-align: left;
}
.red{
 background-color:#999999;
 text-align: center;
 filter:Alpha(opacity=80);/*透明度*/
}
#act {
 background-image: url(http://pic1.phprm.com/2015/07/04/img].jpg);
 height: 27px;
 width: 282px;
}
-->
</style>
<script>
 var MyMar;
 function monitoring()
 {
  if(document.readyState =='complete')
  {
      window.location='?i='+query_get();
  }
 }
 
 function query_get()
 {   
  var querystr = window.location.href.split("=");
  
  if(!Number(querystr[1]))
  {
   var value=0;
  }else{
   var value=Number(querystr[1]);
  }
  
  return Number(value)+1;  
 }
 
 MyMar=setInterval('monitoring()',3000);
</script>
<div style="text-align: center;" id=all_a>
 <div align="center" id=act>
  <div align="center" id=scroll>
   <div class="red" style="width:1%;"></div>
  </div>
 </div>
</div>
<div align="center">
 正在检索:<span id=link></span>
</div>
<div id="logs" class="agreement" style="height:420px; overflow:auto;">
<?
    /*===========================================================
    = 版权协议:
    =    GPL ()
    =------------------------------------------------------------
    = 摘    要:URL收集函数 PHP5
    = 版    本:1.0
    =------------------------------------------------------------
    = 开源stal 项目组
    = 更新作者:jd808
    = 最后日期:2008-4-18
    ============================================================*/
   
$file='sitemap.xml'; //GOOGLE 需要的文件 执行时则做首页的临时URL存储文件
$temp_file='temp.xml';//内页URL临时存储文件
http://pic1.phprm.com/2015/07/04/$url.jpg="http://www.gyqpw.com/"; //要搜索的网站
$timea=time();//开始时间 用户无需理它 只管上面3个参数即可
if(!$_GET['i'])
{
 file_put_contents($file,'');
 file_put_contents($temp_file,'');
 file_put_contents($file,con($url,$timea));
 
 echo "<script>
    window.location='?i=bak';
  </script>";
}else{
 consts($_GET['i'],$timea,$file,$url);
}
function con($url,$timea) //控制
{
 echo "<script>
    document.getElementById('link').innerHTML='正在收集 ".$url." 的信息!';
 </script>";
 
   $str = file_get_contents($url);
 
   $collection_url=collection_url(http://pic1.phprm.com/2015/07/04/http://pic1.phprm.com/2015/07/04/$str,$url.jpg.jpg);
  
   $collection_url=array_flip($collection_url);
    foreach($collection_url as $key=>$value)
    {
  if(count(explode($url,$key))==2)
  {
   $strurl.=$key."n";
  }
    }
 
    return  $strurl;
}
function consts($i,$timea,$file,$urlys)
{
   $str =file_get_contents($file); //读取页面数据并生产字符串
  
   $url=explode("n",$str);
  
   $sum=count($url)-1;
  
   if($i=='bak')
   {
    $i=0;
   }
  
   /*进度条*/
    $wid=round($i/$sum*100,2)."%";
  $div="<div class='red'  style='width:".$wid.";'>$wid</div>";
  echo '<script>
    document.getElementById("scroll").innerHTML="'.$div.'";
   </script>';
   
  ob_flush();//释放缓存
  flush(); //将不再缓存里的数据发送到浏览器去
   /*进度条END */
  
   for($j=$i;$j<$sum;$j++)
   {
     if(!http://pic1.phprm.com/2015/07/04/$url[$j].jpg)
   {
    continue;
   }
   
     if(!detection_url($url[$j])) //检测URL是否合法
   {
    continue;
   }
   
     $timeb=time();//跟踪时间
   if(($timeb-$timea)>=25)
   {
    memory($collection_url,$j); //存储数据
   }
   /* URL显示跟踪*/
   echo "<script>
      document.getElementById('link').innerHTML='".$url[$j]."';
     </script>";
   ob_flush();//释放缓存
   flush(); //将不再缓存里的数据发送到浏览器去
   /* URL显示跟踪END*/
   $urlstr=@file_get_contents($url[$j]);
   
   $collection_url[]=collection_url(http://pic1.phprm.com/2015/07/04/$urlstr,$urlys.jpg);
   
   $timec=time();//跟踪时间
   if(($timec-$timea)>=25)
   {
    memory($collection_url,$j); //存储数据
   }
   
   if($j==$sum-1)
   {
    memorys(); //存储数据 主要是生成正式的xml
   }
   }
}
function collection_url($str,$url) //收集URL并返回一个数组(以页面为主)
{
 preg_match_all('/<a.*?href="(.+?)"/is',$str,$matches);
 
    $urlexp=$matches[1];
 
    for($j=0;$j<count($urlexp);$j++)
    {
  $urlexp[$j]=ltrim(str_replace("rn",'',$urlexp[$j]));
  $urlexp[$j]=ltrim(str_replace("n",'',$urlexp[$j]));
  $urlexp[$j]=ltrim(str_replace("r",'',$urlexp[$j]));
  
  if($urlexp[$j]=='#')
  {
   continue;
  }
  
  if($urlexp[$j]=='/#')
  {
   continue;
  }
  
        if(!strchr($urlexp[$j],'http://'))
        {//没有http://
   $urlall[]=$url.$urlexp[$j];
   echo $url.$urlexp[$j].'<br>';
   print "<script>document.getElementById('logs').scrollTop = document.getElementById('logs').scrollHeight;</script>";
   ob_flush();//释放缓存
   flush(); //将不再缓存里的数据发送到浏览器去
   
        }else{
   if(count(explode($url,$urlexp[$j]))==2)
   {
    $urlall[]=$urlexp[$j];
    
    echo $urlexp[$j].'<br>';
    print "<script>document.getElementById('logs').scrollTop = document.getElementById('logs').scrollHeight;</script>";
    ob_flush();//释放缓存
    flush(); //将不再缓存里的数据发送到浏览器去
   }else{
    unset($urlexp[$j]);
   }
  }
    }
    return $urlall; //返回本页面搜索所得到的数组
}
function memory($collection_url,$i)
{
 
 global $temp_file;
 
 if(is_array($collection_url))
 {
  for($h=0;$h<count($collection_url);$h++)
  {
   for($l=0;$l<count($collection_url[$h]);$l++)
   {
    $strts.=$collection_url[$h][$l]."n";
   }
   
  }
  
  $wstr=file_get_contents($temp_file);
  
  file_put_contents($temp_file,$wstr.$strts);
  
  if($i==0)
  {
   $i=2;
  }
  
  $k=$i-1;
  
  echo "<script>
     window.location='?i=".$k."';
   </script>";
  exit;
 }
}
function memorys() //主要是生成正式的xml
{
 global $temp_file,$file;
 
 $file_arr=array_flip(file($file));
 
 $temp_file_arr=array_flip(file($temp_file));
 
 $xmla='<?xml version="1.0" encoding="UTF-8"?>'."rn".'<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">';
 
 $xmlc="rn</urlset>";
 
 foreach($file_arr as $keya=>$valuea)
 {
  $keya=ltrim(str_replace("rn",'',$keya));
  $keya=ltrim(str_replace("n",'',$keya));
  $keya=ltrim(str_replace("r",'',$keya));
  
  $xml.='<url>
    <loc>'.$keya.'</loc>
    <lastmod>'.date("Y-m-d",time()).'</lastmod>
    <changefreq>daily</changefreq>
   </url>';
 }
 
 foreach($temp_file_arr as $keyb=>$valueb)
 {
  $keyb=ltrim(str_replace("rn",'',$keyb));
  $keyb=ltrim(str_replace("n",'',$keyb));
  $keyb=ltrim(str_replace("r",'',$keyb));
  
  $xml.='<url>
    <loc>'.$keyb.'</loc>
    <lastmod>'.date("Y-m-d",time()).'</lastmod>
    <changefreq>daily</changefreq>
   </url>';
 }
 
 $strts=$xmla.$xml.$xmlc;
 
 file_put_contents($file,$strts);
 
 echo "<script>
   clearInterval(MyMar);
    document.getElementById('link').innerHTML='URL已经收集完成!';
      document.getElementById('all_a').innerHTML='<b>XML生成已完成!</b>';
   </script>";
}
function detection_url($url)
{
 if(strrchr($url,'='))
 {
  return true;
 }
 
 if(substr($url,strlen($url)-1,1)=='/')
 {
  return true;
 }
 
 $postfix= strrchr($url,'.');
 
 switch ($postfix)
 {
  case ".php":
   return true;
  break;
  
  case ".html":
   return true;
  break;
  
  case ".htm":
   return true;
  break;
  
  case ".asp":
   return true;
  break;
  
  case ".aspx":
   return true;
  break;
  
  case ".shtml":
   return true;
  break;
 }
 return false;
}
?>
</div>

阅读全文

ajax仿google搜索下拉提示

<script type="text/javascriptsrc="prototype.js"></script>
<script type="text/javascript">
//定义变量lastindex 表示为鼠标在查询结果上滑动所在位置,初始为-1
var lastindex=-1;
//定义变量flag 表示是否根据用户输入的关键字进行ajax查询,flase为允许查询 true为禁止查询
var flag=false;
//返回的查询结果生成的数组长度
var listlength=0;
function StringBuffer(){//定义对象StringBuffer
       this.data=[];//创建属性,自定字符串
      } 
StringBuffer.prototype.append=function(){//声明StringBuffer的方法
           this.data.push(arguments[0]);return this;//方法实现代码,赋值
          } 
StringBuffer.prototype.tostring=function(){//返回结果,或是说输出结果
           return this.data.join("");
           } 
String.prototype.Trim = function(){//滤过空格
         return this.replace(/(^s*)|(s*$)/g, "");
         }
function hiddensearch(){//隐藏函数 主要是隐藏显示的提示下拉层和iframe,关于iframe下面在说其作用
       $('rlist').style.display="none";
       $('rFrame').style.display="none";
      }
function showsearch(num){//显示函数 主要是显示的提示下拉层和iframe 参数num,根据该参数控制要显示提示层和iframe的高度
        $('rlist').style.display='';
        $('rFrame').style.display='';
       //这里我定义每个返回查询结果的提示高度为20px,其中提示层总高度又加了num,是因为我在定义样式时使用了padding一个像素
        $('rlist').style.height=num*20+num+'px';
         //同样定位iframe的高度
        $('rFrame').style.height=num*20+num+'px';
      }
function getposition(element,offset){
      //返回文本输入框的坐标函数,参数element为要返回的对象,参数offset可选为offsetLeft|offsetTop 分别表示为该对象距离左窗口上角的绝对位置
      //利用这个函数可以定位我们要显示的提示层位置,使提示层正确的显示在文本输入框下面
          var c=0;
          while(element){
               c+=element[offset];
               element=element.offsetParent
             }
          return c;
         }
/********************************DOM*************************************************/
function createlist(){//创建提示层
      var listDiv=document.createElement("div");//createElement()方法可创建元素节点
      listDiv.id="rlist"; //提示层id       
      listDiv.style.zIndex="2";//z-index 属性设置元素的堆叠顺序。拥有更高堆叠顺序的元素总是会处于堆叠顺序较低的元素的前面,这个设置的是后便
      listDiv.style.position="absolute"; //position 属性把元素放置到一个静态的、相对的、绝对的、或固定的位置中。这个和下边的那个是对应的,也就是说和下边的框架是对应的,位置设置为 absolute 的元素,可定位于相对于包含它的元素的指定坐标。此元素的位置可通过 "left"、"top"、"right" 以及 "bottom" 属性来规定。下边的出现也是因为选择了这个absolute数值
      listDiv.style.border="solid 1px #000000";//设置边框样式
      listDiv.style.backgroundColor="#FFFFFF";//设置背景颜色
      listDiv.style.display="none"; //此元素不会被显示
      listDiv.style.width=$('keyword').clientWidth+"px";//只读属性,声明了窗口的文档显示区的宽度
      listDiv.style.left=getposition($('keyword'),'offsetLeft')+1.5+"px";//设置定位元素左外边距
      listDiv.style.top =(getposition($('keyword'),'offsetTop')+$('keyword').clientHeight +3)+"px";//设置一个定位元素的上外边距边界与其包含块上边界之间的偏移。提示:如果 "position" 属性的值为 "static",那么设置 "top" 属性不会产生任何效果。
      var listFrame=document.createElement("iframe");
      listFrame.id="rFrame";//提示层id
      listFrame.style.zIndex="1";//z-index 属性设置元素的堆叠顺序。拥有更高堆叠顺序的元素总是会处于堆叠顺序较低的元素的前面,这个设置的是前边
      listFrame.style.position="absolute";//和上边的那个设置是对应的
      listFrame.style.border="0";//设置边框为0
      listFrame.style.display="none"; //此元素不会被显示
      listFrame.style.width=$('keyword').clientWidth+"px";//只读属性,声明了窗口的文档显示区的宽度
      listFrame.style.left=getposition($('keyword'),'offsetLeft')+1+"px";//设置定位元素左外边距
      listFrame.style.top =(getposition($('keyword'),'offsetTop')+$('keyword').clientHeight +3)+"px";//设置一个定位元素的上外边距边界与其包含块上边界之间的偏移。提示:如果 "position" 属性的值为 "static",那么设置 "top" 属性不会产生任何效果。
      document.body.appendChild(listDiv); //向节点的子节点列表的末尾添加新的子节点。开头结尾相呼应。
      document.body.appendChild(listFrame);//向节点的子节点列表的末尾添加新的子节点。开头结尾相呼应 。
     }
function setstyle(element,classname){
  switch (classname){
        case 'm':
          element.style.fontSize="12px";//设置字体大小
          element.style.fontFamily="arial,sans-serif";//fontFamily 属性定义用于元素文本显示的字体。
          element.style.backgroundColor="#3366cc";//背景
          element.style.color="black";//颜色
          element.style.width=$('keyword').clientWidth-2+"px";//设置只读属性的宽
          element.style.height="20px";//设置高度
          element.style.padding="1px 0px 0px 2px";//padding 属性设置元素的内边距。
          if(element.displaySpan)element.displaySpan.style.color="white"
          break;
        case 'd':
          element.style.fontSize="12px";
          element.style.fontFamily="arial,sans-serif";
          element.style.backgroundColor="white";
          element.style.color="black";
          element.style.width=$('keyword').clientWidth-2+"px";
          element.style.height="20px";
          element.style.padding="1px 0px 0px 2px";
          if(element.displaySpan)element.displaySpan.style.color="green"
          break;
        case 't':
          element.style.width="80%";
          if(window.navigator.userAgent.toLowerCase().indexOf("firefox")!=-1)element.style.cssFloat="left";//判断头的
          else element.style.styleFloat="left";
          element.style.whiteSpace="nowrap";
          element.style.overflow="hidden";
          element.style.textOverflow="ellipsis";
          element.style.fontSize="12px";
          element.style.textAlign="left";
          break;
        case 'h':
          element.style.width="20%";
          if(window.navigator.userAgent.toLowerCase().indexOf("firefox")!=-1)element.style.cssFloat="right";
          else element.style.styleFloat="right";
          element.style.textAlign="right";
          element.style.color="green";
          break;
       }
     }
function focusitem(index){
       if($('item'+lastindex)!=null)setstyle($('item'+lastindex),'d');
       if($('item'+index)!=null){
                setstyle($('item'+index), 'm');
                lastindex=index;
              }
       else {
         $("keyword").focus();
        }
      }
function searchclick(index){
         $("keyword").value=$('title'+index).innerHTML;
         flag=true;
       }
function searchkeydown(e){
       if($('rlist').innerHTML=='')return;
        var keycode=(window.navigator.appName=="Microsoft Internet Explorer")?event.keyCode:e.which;
  //down
  if(keycode==40)
  {
if(lastindex==-1||lastindex==listlength-1)
{
  focusitem(0);
  searchclick(0);
}
else{
  focusitem(lastindex+1);
  searchclick(lastindex+1);
}
  }
if(keycode==38)
{
if(lastindex==-1)
{
  focusitem(0);
  searchclick(0);
}
else{
  focusitem(lastindex-1);
  searchclick(lastindex-1);
}
}
if(keycode==13)
{
  focusitem(lastindex);
  $("keyword").value=$('title'+lastindex).innerText;
}    
if(keycode==46||keycode==8){flag=false;ajaxsearch($F('keyword').substring(0,$F('keyword').length-1).Trim());}
}
function showresult(xmlhttp)
{
var result=unescape(xmlhttp.responseText);
if(result!=''){
    var resultstring=new StringBuffer();
       var title=result.split('$')[0];
       var hits=result.split('$')[1];
    for(var i=0;i<title.split('|').length;i++)
    {
    resultstring.append('<div id="item'+i+'">');
    resultstring.append('<span id=title'+i+'>');
    resultstring.append(title.split('|')[i]);
    resultstring.append('</span>');
    resultstring.append('<span id=hits'+i+'>');
    resultstring.append(hits.split('|')[i]);
    resultstring.append('</span>');
    resultstring.append('</div>');
    }
    $('rlist').innerHTML=resultstring.tostring();
    for(var j=0;j<title.split('|').length;j++)
    {
     setstyle($('item'+j),'d');
  $('item'+j).displaySpan=$('hits'+j);
     setstyle($('title'+j),'t');
     setstyle($('hits'+j),'h');
    }
    showsearch(title.split('|').length);
    listlength=title.split('|').length;
    lastindex=-1;
}
else hiddensearch();
}
function ajaxsearch(value)
{
new Ajax.Request('search.php',{method:"get",parameters:"action=do&keyword="+escape(value),onComplete:showresult});
}
function main()
{
$('keyword').className=$('keyword').className=='inputblue'?'inputfocus':'inputblue';
if($F('keyword').Trim()=='')hiddensearch();
else
{
      if($F('keyword')!=''&&flag==false)ajaxsearch($F('keyword').Trim());
   if(listlength!=0)$('keyword').onkeydown=searchkeydown;
   else hiddensearch();
}
}
function oninit()
{
$('keyword').autocomplete="off";
$('keyword').onfocus=main;
$('keyword').onkeyup=main;
$('keyword').onblur=hiddensearch; 
createlist();
}
Event.observe(window,'load',oninit);
</script>

阅读全文