php學習日記

1.數據庫連接

$conn=mysql_connect("localhost","root","");

mysql_select_db("baoming");

mysql_query("set names gbk");

-------------------------------------------------------------------

2。smarty,fck配置。

-------------------smarty配置:-------------------------------

<?php

session_start();

include_once("libs/smarty/Smarty.class.php");

$smarty=new Smarty();

$smarty->left_delimiter="^";

$smarty->right_delimiter="^";

$smarty->compile_dir="libs/smarty/templates_c";

$smarty->template_dir = "templates";

$conn=mysql_connect("localhost","root","");

mysql_select_db("baoming");

mysql_query("set names gbk");

?>

------------------fck配置--------------------------------------------

<?php

include_once "libs/fck/fckeditor.php" ;

$FCK= new FCKeditor('content');

$FCK->BasePath="libs/fck/";

$FCK->ToolbarSet = 'Basic';

//$FCK->ToolbarSet = 'Default';

$FCK->Width='500px'; //設置顯示寬度

$FCK->Height='400px';

$FCK->Value="";

$content_area=$FCK->CreateHtml();

?>

----------------------------------------------------------------------


3。查,插,改,刪,

--------------查詢(一維數組)------------------------------------

<?php

$sql="select * from infos where id='$id'";

$result=mysql_query($sql);

$array=mysql_fetch_array($result);

$smarty->assign("array",$array);

?>

html中使用:直接用點連接相應字段名

<input type="text" name="names" value="^$array.names^" />

--------------查詢(二維數組)---------------------------------------------

<?php

$sql="select * from infos where userid='$userid'";

$result=mysql_query($sql);

$row=mysql_num_rows($result);//使用mysql_num_rows判斷只適用於select語句

if($row>0){

$i=0; //下面是查詢的二維數組賦值給$array[$i]

while($arr=mysql_fetch_array($result)){

$arr[time]=date("Y-m-d H:i:s",$arr[time]);//改變時間的格式

$array[$i]=$arr;

$i++;

}

}

?>

html中使用:用foreach遍歷數組,並顯示。

^foreach from=$array item=info^

<tr>

<td>姓名:^$info.names^</td>

</tr>

<tr>

<td>性別:^$info.sex^</td>

</tr>

^/foreach^

------------------插入---------------------------------------------------

$sql="insert into infos (userid,names,sex,birth,emails,edus,time,imgs,info,kecheng) values ('$userid','$names','$sex','$birth','$email','$edus','$time','$uploaddir','$content','$kecheng')"; //插入數據庫

$result=mysql_query($sql);

if($result){

echo "<script type='text/javascript'> alert('報名成功!');location.href='selectinfo.php';</script>";

}else{

echo "<script type='text/javascript'> alert('報名失敗!');location.href='selectinfo.php';</script>";

}

--------------------修改-------------------------------------------------------

$sql="update infos set names='$names',sex='$sex',birth='$birth',emails='$emails',edus='$edus',time='$time',imgs='$imgs',info='$content',kecheng='$kecheng'where id='$id'";

$result=mysql_query($sql);

if($result){

echo "<script type='text/javascript'> alert('修改報名成功!');location.href='selectinfo.php';</script>";

}else{

echo "<script type='text/javascript'> alert('修改報名失敗!');location.href='updateinfo.php';</script>";

}

----------------------------------------------------------------------------------------


4,分頁,

------------------------------------分頁-------------------------------------------------------

$sql="SELECT * FROM tb_message";

$result=mysql_query($sql);$datanum=mysql_num_rows($result);//總記錄數

$pagesize=5;//每頁顯示5條

$page=$_GET['page'];

if($page=="" or $page<1){$page=1;----------$page=($page<1)?1:$page;

$pagemax=ceil($datanum/$pagesize);//求可以分得幾頁

if($page>$pagemax){$page=$pagemax;}--------$page=($page>$pagemax)?$pagemax:$page;

$offset=($page-1)*$pagesize;//每頁從第幾條開始

$sql="select * from tb_message limit $offset,$pagesize";每次從$offset開始,向下查出5條信息


<a href="online.php?page=1">首頁</a>

<a href="online.php?page=<?php echo $page-1 ?>">上一頁</a>

<a href="online.php?page=<?php echo $page+1 ?>">下一頁</a>

<a href="online.php?page=<?php echo $pagemax; ?>">尾頁</a>---------


5,文件上傳,

-------------------文件上傳-----------------------------------------------------

<form name="from1" method="post" id="from1" enctype="multipart/form-data">

個人圖片:<input type="file" name="imgs" />

</form>


<?php

$filename=$_FILES['imgs']['name'];//上傳文件

$uploaddir="upload/".$filename;

move_uploaded_file($_FILES['imgs']['tmp_name'],$uploaddir);

?>

----------------------------------------------------------------------------------

6,縮略圖

------------------------------------------------------------------------------------------------------------------

$file_name爲圖片的路徑

$img=null;

$ext=strtolower(end(explode(".",$file_name)));

if($ext == 'jpg' || $ext == 'jpeg'){//判斷圖片的類型,創建相應的圖片

$img = @imagecreatefromjpeg($updir_file);

}elseif($ext == 'png'){

$img = @imagecreatefrompng($updir_file);

}elseif($ext=='gif'){

$img=imagecreatefromgif($updir_file);

}

list($RealWidth, $RealHeight) = getimagesize($img); //獲取圖片的寬高。

$shuoluetu=imagecreatetruecolor(100,100);//創建真彩圖

imagecopyresized($shuoluetu,$img,0,0,0,0,100,100,$RealWidth,$RealHeight);//創建100*100的縮略圖

-----------------------------------------------創建縮略圖------------------------------------------------------------------------------------------------------------

<?php

//$imgfile = 'smp.jpg'; //一張圖片。

//$percent = 0.2;

header('Content-type: image/jpeg');//定義頭信息。

list($width, $height) = getimagesize($imgfile); //獲取圖片的寬高。

$newwidth = $width * $percent; //縮略圖片的寬和高,

$newheight = $height * $percent;

$thumb = ImageCreateTrueColor($newwidth,$newheight);//新建一張原始真彩色圖像,

$source = imagecreatefromjpeg($imgfile); //以圖片爲基礎創建一張圖像。

imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); //創建縮略圖,第一個參數爲新建圖像,第二個爲原圖片圖像,第三四五六參數爲,兩頭像的起始座標,後四個爲兩圖像的寬高。

imagejpeg($thumb); //輸出縮略圖

?>



7,表單JS驗證

-------------------JS驗證(此處定義一個函數放在JS文件中)---------------------------------------------------------

function check(){

if(document.form1.userid.value==""){

alert("請輸入用戶名!");

document.form1.userid.focus();

return false;

}

if(document.form1.userid.value.length<5||document.form1.userid.value.length>10){

alert("用戶名必須在5到10位之間!");

document.form1.userid.focus();

return false;

}

var m1=/[\u4e00-\u9fa5]/g;

if(!m1.test(document.form1.name.value)){

alert(姓名必須是中文!);

form1.name.value.focus();

return false;

}

}

在HTML中調用

<script type="text/javascript" src="libs/js/my_check.js"></script>

<form id="form1" name="form1" method="post" onsubmit="return check();">

--------------------------------------JS驗證,一個對話框創建一個函數,-----------------------------------------------------------------------------

my_check.js---------------

function register_userid_check(){

var text1="document.form1.userid.value";

if(document.form1.userid.value==""){

document.getElementById('userid_c').innerHTML="用戶名不能爲空";

return false;

}

/*}else{

document.getElementById('userid_c').innerHTML="<img width=20 height=15 src='images/11.jpg'>";

}*/

else if(document.form1.userid.value.length>10 || document.form1.userid.value.length<6){

document.getElementById('userid_c').innerHTML="<font color='red'>用戶名長度必須在6-10位之間</font>";

return false;

}

for(var i=0;i<document.form1.userid.value.length;i++)

{

var text=document.form1.userid.value.charAt(i);

if(!(text<=9&&text>=0)&&!(text>='a'&&text<='z')&&!(text>='A'&&text<='Z')&&text!="_")

{

document.getElementById('userid_c').innerHTML="<font color='red'>用戶名只能是數字、字母、下劃線組成!</font>";

break;

}

}

if(i>=document.form1.userid.value.length)

{

document.getElementById('userid_c').innerHTML="<font color='red'>√</font>";

return true;

}


}

function register_password_check(){

if(document.form1.password.value==""){

document.getElementById('password_c').innerHTML="密碼不能爲空";

return false;

}else if(document.form1.password.value.length<6){

document.getElementById('password_c').innerHTML="密碼不能少於六位";

return false;

}else{

document.getElementById('password_c').innerHTML="<img width=20 height=15 src='images/11.jpg'>";

return true;

}

}

function checkall() //檢查所有

{

if(register_userid_check()&&register_password_check())

{

return true;

}

return false;

}

register.html---------

<html>

<head>

<title>註冊界面難證</title>

<script type="text/javascript" src="libs/js/my_check.js"></script>

</head>

<body>

<form name="form1" method="post" action="">

<div>

<input type="text" name="userid" onblur="register_userid_check()" /><span id="userid_c"></span>

</div>

<div>

<input type="password" name="password" onblur="register_password_check()" /><span id="password_c"></span>

</div>

<input type="submit" name="Submit" value="註冊" OnClick="return checkall();" />

</form>

</body>

</html>


8,ajax無刷新驗證

-----------------------------------ajax無刷新驗證-----------------------------------------------------------------

由頁面調用JS中的函數funphp100(),再由該函數傳參給for.php(xmlHttp.open("GET","for.php?id="+f,true);)然後由byphp()函數

接收for.php傳回來的值,顯示到頁面的相應位置。

------------ajax.js文件---------------

var xmlHttp;

function S_xmlhttprequest(){

if(window.ActiveXObject){

xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');

}else if(window.XMLHttprequest){

xmlHttp=new XMLHttpRequest();

}

}


function funphp100(xx){

var f=document.myform.xx.value;

S_xmlhttprequest();

xmlHttp.open("GET","for.php?id="+f,true);

xmlHttp.onreadystatechange=byphp;

xmlHttp.send(null);

}


function byphp(){

if(xmlHttp.readyState==1){

document.getElementById('php100').innerHTML="<img src=006.jpg>";

}

if(xmlHttp.readyState==4){

var byphp100=xmlHttp.responseText;

document.getElementById('php100').innerHTML=byphp100;

}

}

-----------------------for.php文件---------

<?php

header('Content-Type:text/html; charset=gb2312');

if($id=$_GET['id']){

sleep(1);

$conn=mysql_connect('localhost','root','')or die ('數據庫連接錯誤');

mysql_select_db('test',$conn);

$sql="select * from users where name='$id'";

$result=mysql_query($sql);

if(is_array(mysql_fetch_row($result))){

echo '用戶名已存在';

}else{

echo "可以使用";

}

}

?>


---------------------------login.php-----------

<script type="text/javascript" src="ajax.js"></script>

<form action="" name="myform" method="post" enctype="text/plain">

<input type="text" name="user" value="" onblur="funphp100()"/>

<div id="php100"></div>

</form>

-------------------------------------------------------------

9,複選框,單選框,下拉菜單獲值。

-------------------------單選框--------------------------------------------

<form>

男:

<input type="radio" checked name="Sex" value="male">

<br>

女:

<input type="radio" name="Sex" value="female">

</form>

----------------------------------複選框---------------------------------------------------

c語言<input type="checkbox" name="kecheng[]" id="" value="c" >//此處name定義爲數組。

java語言<input type="checkbox" name="kecheng[]" id="" value="java">

php語言<input type="checkbox" name="kecheng[]" id="" value="php">

c#語言<input type="checkbox" name="kecheng[]" id="" value="c#">


php獲得的值是數組,$kecheng=implode("|",$_POST['kecheng']); //用合併函數合併爲字符串存入數據庫


修改的時候用下面這段代碼:

$array_kecheng=explode('|',$array[kecheng]);//將字符串切割爲數組

for($i=0;$i<4;$i++){//循環判斷數組裏的值

switch ($array_kecheng[$i]){

case "c": $c_check="checked"; break; //如果數組中的值等於C就將複選框選上

case "java": $java_check="checked"; break;

case "php": $php_check="checked"; break;

case "c#": $cx_check="checked"; break;

}

}


修改顯示頁面

c語言<input type="checkbox" name="kecheng[]" id="" value="c" ^$c_check^>//如果$c_check的值爲checked則複選框被選擇

java語言<input type="checkbox" name="kecheng[]" id="" value="java" ^$java_check^>

php語言<input type="checkbox" name="kecheng[]" id="" value="php" ^$php_check^>

c#語言<input type="checkbox" name="kecheng[]" id="" value="c#" ^$cx_check^>


------------------------------------下拉菜單-------------------------------------------------------------------------------

<form>

<select name="cars">

<option value="volvo">Volvo

<option value="saab">Saab

<option value="fiat" selected>Fiat selected表示被選擇

<option value="audi">Audi

</select>

</form>



------------------------------------------------簡單圖片驗證碼,他建圖像,在圖像上寫字符串。--------------------------------------------------------------

<?php

session_start(); //開啓SESSION

header("content-type:image/jpeg"); //向瀏覽器發佈頭信息,爲圖片。header()第二個參數是圖片保存的路徑。可將圖片保存到文件中,此處是顯示在瀏覽器。

$handel=imagecreatefromjpeg("1.jpg");// 以1。JPG爲原形創建一張圖片。

$textcolor=imagecolorallocate($handel,255,255,255);//調色。白色

$valicode=rand(1000,9999);//四位隨機數做驗證碼。

$_SESSION[yzm]=$valicode;//傳值給SESSION。

imagestring($handel,5,100,100,$valicode,$textcolor);//在圖片上寫字符串,第一個是圖片句柄,第二個參數是字體大小,第三,四個參數是書寫起始座標,第五個是要寫的字符串,第六個是字體色。

imagejpeg($handel);//輸出JPEG格式的圖像。

imagedestory($handel);//釋放圖片資源。

?>


----------------------------------------------------頁面跳轉-----------------------------------------------------------------------------

<span id="tiao">3</span><a href="countDown">< /a>秒後自動返回…

<!--腳本開始-->

<script language="javascript" type="">

function countDown(secs){

tiao.innerText=secs;

if(--secs>0){

setTimeout("countDown("+secs+")",1000);

}else{

location.href="xx.html"//top.location.href=""//跳轉到上級頁面。

}


}

countDown(3);

</script>

-------------------------頁面跳轉---------------------------------------------------------------------------------

header("location:xx.html");//頭信息跳轉

javascript:history.back(1);//後退一步跳轉

<META content=2;url=msg.php?id=xx http-equiv=refresh>//meta跳轉,content=2指暫停2秒再跳轉

<script> top.location='yy.html' </script>//返回上一級頁面再跳轉

<script> setTimeout(top.location='xx.php',50000);</script>//用於在指定的毫秒數後調用函數或計算表達式。

<script>location.href='register.php';</script>//跳轉

<script>history.go(-1);</script>//返回跳轉

<body onLoad="top.location.href='xxx.php';">//body中加載跳轉,top指先回到上一級頁面再跳轉。




























發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章