PHP常用代码

<?php
ob_start(); //打开缓冲区

header("location:index.php"); //把浏览器重定向到index.php

echo "<script>window.location.href='../error.html';</script>";exit;

date_default_timezone_set("Etc/GMT-8"); // 中国时区

header("Content-type: text/html;charset=GB2312"); //定义PHP编码

?>
 �瞟铷����� mysql操作

<?php $sql="update news set hits=hits+1 where id=$id";  //浏览次数加一?>


本月统计(MySQL)
select * from booking where month(booking_time) = month(curdate()) and year(booking_time) = year(curdate())

本周统计(MySQL)
select * from spf_booking where month(booking_time) = month(curdate()) and week(booking_time) = week(curdate())

�瞟铷�����session 与 cookie 设置
         session_start();
          $_SESSION["GOD"]=$demon;
          echo  $_SESSION["GOD"];

    // $_SESSION["GOD"]="";

    
        setcookie("God",$demon); //上帝是魔鬼
        echo $_COOKIE['God'];    
      // setcookie("god", "", time()-3600);


 �瞟铷����� 如果没有获得B值,BID为cookie值,如有获得B值,BID为B值,并改变cookie值  

    if($_GET["b"]==""){
        $BID=$_COOKIE['BID'];
        }else{
      $BID=$_GET["b"];
   setcookie('BID',$BID);
        }

�瞟铷�����前台显示和后台文本框一样
  <?php echo str_replace(array(CHR(13),"\r\n",CHR(32)),array("<br>","<br>","&nbsp;"),$a15); ?>


 �瞟铷�����弹出窗口
echo "<script language=javascript>window.open('rows.aspx','newwindow','width=200,height=200')</script>";

刷新父窗口
<a onclick="window.parent.location.reload()">close</a>

<li><a href="javascript:void()" onclick="showModalDialog('index.php',window,'dialogWidth:520px;dialogHeight:418px;help:no;scroll:yes;status:no');"><img src="" height="315" width="210"></a></li>


 �瞟铷�����保留数字小数点后两位  
$abc=58;
number_format($abc,2); // 58.00
round($abc,2);  //58

substr_count(字符串1, 字符串2)
返回的是字符串2在字符串1中出现的次数. 


�瞟铷�����去掉字符串最后一位和第一位
// substr($str, 0, -1)
// rtrim($str, ",")
$abc=ltrim($abc,",");



 �瞟铷�����输出中文星期 和 英文星期
           <?php $weekarray=array("日","一","二","三","四","五","六");
                 echo "星期".$weekarray[date("w")];
           ?>
           <?php echo date('Y年m月d日',strtotime(now));?>

            <?php  $ENweekarray=array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
            echo $ENweekarray[date("w")];
            echo "&nbsp;";
            echo date('M d Y');
            ?>

 �瞟铷����� 判断某个复选框是否选中

<FORM onSubmit="return chkAgree();" action="#">
同意:<INPUT TYPE="checkbox" id="agree" name="AGREE">
</FORM>

<script language="javascript">
function chkAgree(){
 if(document.getElementById("agree").checked){
  return true;
 }
 alert("请同意协议内容才能注册");
 return false;
}
</script>
 


�瞟铷�����JS 把复选框的值 组成一定规则的字符串 写进隐藏域,让PHP好获取
<input type="submit" value="提交表单">
<script type="text/javascript">
function chk(){    
 var obj=document.getElementsByName('test');  //选择所有name="'test'"的对象,返回数组    
 //取到对象数组后,我们来循环检测它是不是被选中    
  var s='';    
  for(var i=0; i<obj.length; i++){    
    if(obj[i].checked) s+=obj[i].value+',';  //如果选中,将value添加到变量s中    
  }    
  //那么现在来检测s的值就知道选中的复选框的值了    
  //alert(s==''?'你还没有选择任何内容!':s);
  document.getElementById('showLA').value=s;
  }    
</script>




�瞟铷�����获取完整的url
echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
#http://localhost/blog/testurl.php?id=5


<a href="javascript:form1.submit();">提交</a>
<a href="javascript:if(chk(myform)){myform.submit()};" >GO</a>


<?php if($_SERVER['PHP_SELF']=="/anxin/m_2.php"){ echo "class='on'";}?>


�瞟铷�����数组写进数据库
<?php
$stooges = array('Moe','Larry','Curly');
$new = serialize($stooges);
print_r($new);echo "<br />";

echo "<hr>";

print_r(unserialize($new));搜索


//结果:a:3:{i:0;s:3:"Moe";i:1;s:5:"Larry";i:2;s:5:"Curly";}
//Array ( [0] => Moe [1] => Larry [2] => Curly )


?>


 echo "<script>alert('您的密码为'+$find);</script>";    


�瞟铷�����鼠标经过变色.txt

 <tr id="zzjs_net" onmouseover="this.style.backgroundColor='#ffcccc'" onmouseout="this.style.backgroundColor='#eeeeee'">



�瞟铷�����echo diffent

<?PHP
for($i=1;$i<10;$i++){
            echo "\$a{$i}=\$_GET[\"\$a{$i}\"];";
            echo "<br>";
            }

?>


�瞟铷�����下拉列表链接网页
<select   onchange= "window.location.href=this.options[this.selectedIndex].value ">

 <script language="javascript">
function show(form){
  var myIndex = form.sites.selectedIndex;
//window.open(form.sites.options[myIndex].value,'','width=auto,height=auto')
location = form.sites.options[myIndex].value;
}
</script>
 <button type="button" onclick="show(this.form)" >go</button>


你可能感兴趣的:(PHP,企业站)