php 常用代码集

1、连接MYSQL数据库代码
<?php
$connec=mysql_connect("localhost","root","root") or die("不能连接数据库服务器: ".mysql_error());
mysql_select_db("liuyanben",$connec) or die ("不能选择数据库: ".mysql_error()); 
mysql_query("set names 'gbk'");
?>


2、读取数据库,并实现循环输出
<?php
$sql="select * from liuyan order by ly_id desc";
$conn=mysql_query($sql,$connec);
while($rs=mysql_fetch_array($conn)){ 
?>
<table width="476" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td width="135" height="30"><div align="center">留言主题</div></td>
    <td width="204" height="30"><div align="center">留言时间</div></td>
    <td width="129" height="30"><div align="center">留言人</div></td>
  </tr>
  <tr bgcolor="#CC9999">
    <td height="30"><?= $rs["ly_title"]?>
      <div align="center"></div></td>
    <td height="30"><?= $rs["ly_time"]?>
      <div align="center"></div></td>
    <td height="30"><?= $rs["ly_author"]?>
      <div align="center"></div></td>
  </tr>
  <tr>
    <td height="30"><div align="center"><a href="huifu.php?id=<?php echo $rs["ly_id"]?>">回复留言</a></div></td>
    <td height="30"><div align="center"><a href="viewhuifu.php?id=<?php echo $rs["ly_id"]?>">查看回复信息</a></div></td>
    <td height="30"><div align="center">已有回复</div></td>
  </tr>
</table>
<?php
 }
 ?>

 

3、如何实现分页,包括两个函数,两个调用
1)两个函数
<?
//分页函数

 function genpage(&$sql,$page_size=2)
 {
      global $prepage,$nextpage,$pages,$sums;  //out param
      $page = $_GET["page"];
      $eachpage = $page_size;
      $pagesql = strstr($sql," from ");
      $pagesql = "select count(*) as ids ".$pagesql;
      $conn = mysql_query($pagesql) or die(mysql_error());
      if($rs = mysql_fetch_array($conn)) $sums = $rs[0];
      $pages = ceil(($sums-0.5)/$eachpage)-1;
      $pages = $pages>=0?$pages:0;
      $prepage = ($page>0)?$page-1:0;
      $nextpage = ($page<$pages)?$page+1:$pages; 
      $startpos = $page*$eachpage;
    $sql .=" limit $startpos,$eachpage ";
 }
 //显示分页
function showpage()
{
    global $page,$pages,$prepage,$nextpage,$queryString; //param from genpage function
    $shownum =10/2;
    $startpage = ($page>=$shownum)?$page-$shownum:0;
    $endpage = ($page+$shownum<=$pages)?$page+$shownum:$pages;
  
    echo "共".($pages+1)."页: ";
    if($page>0)echo "<a href=$PHP_SELF?page=0$queryString>首页</a>";
    if($startpage>0)
        echo " ... <b><a href=$PHP_SELF?page=".($page-$shownum*2)."$queryString>«</a></b>";
    for($i=$startpage;$i<=$endpage;$i++)
    {
        if($i==$page)    echo " <b>[".($i+1)."]</b> ";
        else        echo " <a href=$PHP_SELF?page=$i$queryString>".($i+1)."</a> ";
    }
    if($endpage<$pages)
        echo "<b><a href=$PHP_SELF?page=".($page+$shownum*2)."$queryString>»</a></b> ... ";
    if($page<$pages)
        echo "<a href=$PHP_SELF?page=$pages$queryString>尾页</a>";

}

 //显示带分类的分页
function showpage1()
{
  $fenlei=$_GET["fenleiid"];
    global $page,$pages,$prepage,$nextpage,$queryString; //param from genpage function
    $shownum =10/2;
    $startpage = ($page>=$shownum)?$page-$shownum:0;
    $endpage = ($page+$shownum<=$pages)?$page+$shownum:$pages;
  
    echo "共".($pages+1)."页: ";
    if($page>0)echo "<a href=$PHP_SELF?fenleiid=$fenlei&page=0$queryString>首页</a>";
    if($startpage>0)
        echo " ... <b><a href=$PHP_SELF?fenleiid=$fenlei&page=".($page-$shownum*2)."$queryString>«</a></b>";
    for($i=$startpage;$i<=$endpage;$i++)
    {
        if($i==$page)    echo " <b>[".($i+1)."]</b> ";
        else        echo " <a href=$PHP_SELF?fenleiid=$fenlei&page=$i$queryString>".($i+1)."</a> ";
    }
    if($endpage<$pages)
        echo "<b><a href=$PHP_SELF?fenleiid=$fenlei&page=".($page+$shownum*2)."$queryString>»</a></b> ... ";
    if($page<$pages)
        echo "<a href=$PHP_SELF?fenleiid=$fenlei&page=$pages$queryString>尾页</a>";

}
?>

 

2)两个调用
第一个
<?php
$sql="select * from liuyan order by ly_id desc";
genpage($sql);  //只需要正常代码加上这一行就ok。
$conn=mysql_query($sql,$connec);
while($rs=mysql_fetch_array($conn)){ 
?>
第二个
<?php
 }
 ?>
 <?php 
  showpage(); //显示页
?>
  <?php
  mysql_close();
       
  ?>


4、服务器端包含
<?php require_once('conn.php'); ?>

 

5、如何将一条记录写入数据库,然后提示并跳转页面
<?php
$ly_title=$_POST["ly_title"];
$ly_content=$_POST["ly_content"];
$ly_time=$_POST["ly_time"];
$ly_author=$_POST["ly_author"];
$ly_email=$_POST["ly_email"];

$sql="insert into liuyan(ly_title,ly_content,ly_time,ly_author,ly_email) values('".$ly_title."','".$ly_content."','".$ly_time."','".$ly_author."','".$ly_email."')";
mysql_query($sql,$connec);
echo("<script type='text/javascript'> alert('添加成功!');location.href='index.php';</script>");
?>

 

6、弹出对话框,并发生页面跳转
<?php
echo("<script type='text/javascript'> alert('添加成功!');location.href='index.php';</script>");
?>

 

7、信息查看页面(有条件读取数据库)
1)有条件读取数据库
<?php
$sql="select * from liuyan where ly_id=$_GET[id]";
$conn=mysql_query($sql,$connec);
$rs=mysql_fetch_array($conn);
?>
2)将某个字段输出
<?=$rs[ly_title]?>
3)关闭数据库
<?php
  mysql_close();     
  ?>

 

8、对数据库中某一条记录进行更新操作,并作提示跳转
<?php
$ly_title=$_POST["ly_title"];
$ly_content=$_POST["ly_content"];
$ly_time=$_POST["ly_time"];
$ly_author=$_POST["ly_author"];
$ly_email=$_POST["ly_email"];

$sql="update liuyan set ly_title='$ly_title',ly_content='$ly_content',ly_time='$ly_time',ly_author='$ly_author',ly_email='$ly_email' where ly_id=$_GET[id]";
mysql_query($sql,$connec);
echo("<script type='text/javascript'> alert('更新成功!');location.href='../index.php';</script>");
?>

 

9、如何删除数据库中的一条记录
<?php

$sql="delete from liuyan where ly_id=$_GET[id]";
mysql_query($sql,$connec);
echo("<script type='text/javascript'> alert('删除成功!');location.href='../index.php';</script>");
?>


10、如何进行会员登录验证
<?php
session_start();
$username=$_POST["username"];
$password=$_POST["password"];

$sql="select * from admin where username='".$username."' && password='".$password."'";
$result=mysql_query($sql,$connec);
if($row=mysql_fetch_array($result)){
session_register("admin");
$admin=$username;
echo("<script type='text/javascript'> alert('登录成功!');location.href='admin.php';</script>");}
else
{
echo("<script type='text/javascript'> alert('你输入的用户名或密码错误,请重新输入!');location.href='login.php';</script>");
}

mysql_close();

?>
11、如何对SESSION进行检验(后台检查页面的制作)
<?php
session_start();
if(!isset($_SESSION["admin"])){
header("location:login.php");
exit;
}
?>


12、验证用户名及密码是否填写(javascript)
<SCRIPT language=javascript>
<!--
 function confirmlogin()
 {
  if (document.frmmain.username.value.length<4 || document.frmmain.username.value=="")
  {
     document.frmmain.username.focus();
     document.frmmain.username.select;
     window.alert("请输入你的用户名!");
     return false;
  }
  if (document.frmmain.password.value.length<4)
  {
     document.frmmain.password.focus();
     document.frmmain.password.select;
     window.alert("请输入你的密码!");
     return false; 
  }
    return true;
 }
 
//-->
</SCRIPT>

 

13、在PHP中调用编辑器的方法

  1)将编辑器文件夹放置后台管理文件夹内。
  2)利用以下语句进行引入操作。

 <input name="content" type="hidden" value=''>
<IFRAME ID="eWebEditor1" src="eWebEditorPHP38/ewebeditor.htm?id=content&style=coolblue" frameborder="0" scrolling="no" width="550" height="350"></IFRAME>
注:eWebEditorPHP38编辑器文件夹的名称。
    id=content中content为上面隐藏域的名称

 

14、循环输出(能够实现分列)
   1)首先插入一行一列表格
<?php
$i=1;
?>  
<table>
   <tr>
    <?php
  while($rs=mysql_fetch_array($conn)){ 
  ?>     
   <td>
    被循环的其它表格和输出
   </td>
   <?php
      if ($i % 2==0) {
    echo "</tr><tr>";
    }
    $i++;
     }
    ?>  
   </tr>
  </table>

 

15、给下拉列表框绑定数据(并且在修改时默认选中)
<select name="fenleiid">
<?php
$sql="select * from fenleibiao";
$conn=mysql_query($sql,$connec);
 while($rs1=mysql_fetch_array($conn)){ 
?>
     
<option value="<?=$rs1["fenleiid"]?>"
<?
if ($rs["fenleiid"]==$rs1["fenleiid"]){
  echo "selected" ;
  }
  ?>>
  <?=$rs1["flname"]?>
  </option>
        <?php>
}
  ?>
          </select>

 

16、获取字符长度函数
strlen($c)>12

 

17、定义一个字符截取函数
用法:<?=substrgb($rs["title"],10)?>
function substrgb($in,$num){
    $pos=0;
    $out="";
    while($c=substr($in,$pos,1)){
      if($c=="\n") break;
      if(ord($c)>128){
        $out.=$c;
        $pos++;
        $c=substr($in,$pos,1);
   
    $out.=$c;
      }else{
        $out.=$c;
      }
      $pos++;
      if($pos>=$num) break;
    }
    if($out!=$in) $out = $out . "...";
    return $out;
  }

 

18、判断是否是数字
!is_numeric(qq)

 

19、PHP技术中获取当前日期
$ptime=date("y-m-d");

 

20、用户注册时所使用的PHP验证程序
if ($admin==""  or (strlen($admin)>16) or (strlen($admin)<2)) {
     echo "<SCRIPT language=JavaScript>alert('请输入用户名(不能大于16小于2)');";
     echo"this.location.href='vbscript:history.back()';</SCRIPT>";
}

if ($password=="" or strlen($password)>16 or strlen($password)<6) {
     echo "<SCRIPT language=JavaScript>alert('密码长度为6-16个字符');";
     echo"this.location.href='vbscript:history.back()';</SCRIPT>";
    
}

if ($password=="") {
     echo "<SCRIPT language=JavaScript>alert('确认密码不能为空');";
     echo"this.location.href='vbscript:history.back()';</SCRIPT>";
    
}else{
  if ($password!=$password1) {
     echo "<SCRIPT language=JavaScript>alert('密码和确认密码不一致');";
     echo"this.location.href='vbscript:history.back()';</SCRIPT>";
    
  }
}

if ($wt="") {
     echo "<SCRIPT language=JavaScript>alert('密码问题不能为空');";
     echo"this.location.href='vbscript:history.back()';</SCRIPT>";
    
}

if ($da="") {
     echo "<SCRIPT language=JavaScript>alert('问题答案不能为空');";
     echo"this.location.href='vbscript:history.back()';</SCRIPT>";
    
}

if ($qq!="") {
  if (!is_numeric($qq)) {
     echo "<SCRIPT language=JavaScript>alert('QQ号码必须是数字');";
     echo"this.location.href='vbscript:history.back()';</SCRIPT>";
    
  }
}

if ($youbian==""  or strlen($youbian)!=6)  {
     echo "<SCRIPT language=JavaScript>alert('请正确输入邮编');";
     echo"this.location.href='vbscript:history.back()';</SCRIPT>";
    
}

if ($youbian!="") {
  if (!is_numeric($youbian)) {
     echo "<SCRIPT language=JavaScript>alert('邮编必须是数字');";
     echo"this.location.href='vbscript:history.back()';</SCRIPT>";
    
  }
}

if ($dizhi="") {
     echo "<SCRIPT language=JavaScript>alert('住址不能为空');";
     echo"this.location.href='vbscript:history.back()';</SCRIPT>";
    
}

if ($mail=="") {
  echo "<SCRIPT language=JavaScript>alert('E-mail不能为空!');";
  echo "this.location.href='vbscript:history.back()';</SCRIPT>";
 

if ($textarea=="") {
  echo "<SCRIPT language=JavaScript>alert('个人说明不能为空!');";
  echo "this.location.href='vbscript:history.back()';</SCRIPT>";
 
}

if ($textarea==""  or strlen(textarea)>150) {
   echo "<SCRIPT language=JavaScript>alert('个人说明为150个字符');";
   echo"this.location.href='vbscript:history.back()';</SCRIPT>";
  
}


21、在线调查前台页面参考
<table width="100%"  border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td background="images/bj_r1_c1.jpg">
       <?php
$sql1="select * from voteclass where active=1";
$conn1=mysql_query($sql1,$connec);
$rs1=mysql_fetch_array($conn1);
?>
      <form name="form3" method="post" action="vote_save.php">
     
            <table width="80%" border="0" align="center" cellpadding="0" cellspacing="0">
              <tr>
                <td><div align="center"><?=$rs1[voteclass]?></div></td>
              </tr>
               <?php
$sql2="select * from votexiang where vc_id=".$rs1[vc_id];
$conn2=mysql_query($sql2,$connec);
?>
        <tr>
                <td>
        
            <?php
          if ($rs1["class"]==="单选"){
          while($rs2=mysql_fetch_array($conn2)){  ?>
               <input name="vote" type="radio" value="<?=$rs2[votexiang]?>">
            
            <?=$rs2[votexiang]?>
            <br>
            <?php
          }
          }else{
          ?>
         
           <?php
           $i=1;
           while($rs2=mysql_fetch_array($conn2)){ 
           ?>
               <input name="vote<?=$i?>" type="checkbox" value="<?=$rs2[votexiang]?>">
            
            <?=$rs2[votexiang]?>
          <input name="shu" type="hidden" value="<?=$i?>">
            <br>
            <?php
          $i++;
          }
          }
          $id=$rs1["vc_id"]
          ?>
             </td>
              </tr>
              <tr>
                <td><input name="imageField2" type="image" src="images/votetj.jpg" width="59" height="22" border="0"> 
                  <a href="voteview.php?id=<?=$id?>" target="_blank">查看结果</a></td>
              </tr>
            </table>
                    </form>
          </td>
        </tr>
      </table>

 

22、在线调查处理页面参考代码
<?php
$shu=$_POST["shu"];
$vote=$_POST["vote"];
if ($shu!=""){
for ($i=1;$i<=$shu;$i++){
$votexiang.=$_POST["vote$i"];
}
if ($votexiang!=""){
    for ($i=1;$i<=$shu;$i++){
    if ($_POST["vote$i"]!=""){
    $sql="update votexiang set voteshu=voteshu+1 where votexiang='".$_POST["vote$i"]."'";
    mysql_query($sql,$connec);
    echo("<script type='text/javascript'> alert('添加成功!');location.href='index.php';</script>");
    }
    }
}else{
echo("<script type='text/javascript'> alert('没有进行投票项目选择!');location.href='index.php';</script>");
}
}
if ($vote!="") {
$sql="update votexiang set voteshu=voteshu+1 where votexiang='".$_POST["vote"]."'";
mysql_query($sql,$connec);
echo("<script type='text/javascript'> alert('添加成功!');location.href='index.php';</script>");
}else{
echo("<script type='text/javascript'> alert('没有进行投票项目选择!');location.href='index.php';</script>");
}
?>

 

23、在线调查查看结果页面参考代码
<p align="center">欢迎查看投票结果</p>
<?php
$sql="select * from votexiang where vc_id=".$_GET["id"];
$conn=mysql_query($sql,$connec);
?>
<table width="600" border="1" bordercolordark="#0000CC" bordercolorlight="#FFFFFF" cellspacing="0" cellpadding="0">
  <tr>
    <td width="103" height="25"><div align="center"><span class="STYLE1">项目名称</span></div></td>
    <td width="89" height="25"><div align="center"><span class="STYLE1">票数</span></div></td>
    <td width="408" height="25"><div align="center"><span class="STYLE1">比例</span></div></td>
  </tr>
  <?php
  $sum=0;
  while($rs=mysql_fetch_array($conn)){
  $sum+=$rs["voteshu"];
  }
 
  $conn=mysql_query($sql,$connec);
   while($rs1=mysql_fetch_array($conn)){
  $voteshu=(int)($rs1["voteshu"]/$sum*100);
  $pic_width=$voteshu*778/100;
?>
  <tr>
    <td height="25"><?=$rs1["votexiang"]?></td>
    <td height="25"><?=$rs1["voteshu"]?></td>
    <td height="25"><img src="images/left1.gif" height="20" width="<?=$pic_width?>">
  <?=$voteshu?>%
   </td>
  </tr>
  <?php }?>
</table>

 

24、对输出的内容进行判断,从而输出其它结果
<?php
    if ($rs["active"]==1) {
    echo "<font color='#ff0000'>激活</font>";
    }else{
    echo "禁用";
    }    
    ?>

 

25、 限制标题字数的:     
     <?=substrgb($rs1["news_title"],26)?>

 

 

posted @ 2008-08-22 13:34  阿无  阅读(488)  评论(0编辑  收藏  举报