PHP Common Code Encyclopedia (Novice entry required) _php Foundation

Source: Internet
Author: User
1, the connection MySQL database code
<?php
$connec =mysql_connect ("localhost", "root", "root") or Die ("Cannot connect to the database server:". mysql_error ());
mysql_select_db ("Liuyanben", $connec) or Die ("Cannot select Database:". mysql_error ());
mysql_query ("Set names ' GBK '");
?>

2, read the database, and realize the circular output
<?php
$sql = "SELECT * from Liuyan ORDER by ly_id Desc";
$conn =mysql_query ($sql, $connec);
while ($rs =mysql_fetch_array ($conn)) {
?>
The contents of the loop ...
<?php
}
?>

3, how to implement pagination, including two functions, two calls
1) Two functions
?
Paging function
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";
}
Show page pagination
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 "altogether". ($pages + 1). " Page: ";
if ($page >0) echo "<a href= $PHP _self?page=0$querystring> home </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> last </a>";
}
Show pagination with categories
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 "altogether". ($pages + 1). " Page: ";
if ($page >0) echo "<a href= $PHP _self?fenleiid= $fenlei &page=0$queryString> home </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> end </a& amp; gt; ";
}
?>
2) Two calls
First one
<?php
$sql = "SELECT * from Liuyan ORDER by ly_id Desc";
Genpage ($sql); Just the normal code plus this line will be OK.
$conn =mysql_query ($sql, $connec);
while ($rs =mysql_fetch_array ($conn)) {
?>
A second
<?php
}
?>
<?php
ShowPage (); Show Page
?>
<?php
Mysql_close ();
?>

4, server-side contains
<?php require_once (' conn.php ');?>

5, how to write a record to the database, and then prompt and jump page
<?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 (' Add success! '); location.href= ' index.php ';</script> ');
?>

6, pop-up dialog box, and the occurrence of page jump
<?php
Echo (' <script type= ' text/javascript ' > Alert (' Add success! '); location.href= ' index.php ';</script> ');
?>

7. Information viewing page (conditional read database)
1 conditionally Read the database
<?php
$sql = "SELECT * from Liuyan where Ly_id=$_get[id]";
$conn =mysql_query ($sql, $connec);
$rs =mysql_fetch_array ($conn);
?>
2) to output a field
<?= $rs [ly_title]?>
3) Close the database
<?php
Mysql_close ();
?>

8. Update the operation of a record in the database and make a prompt jump
<?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 (' Update succeeded! '); location.href= '. /index.php ';</script> ');
?>

9, how to delete a record in the database
<?php
$sql = "Delete from Liuyan where Ly_id=$_get[id]";
mysql_query ($sql, $connec);
Echo (' <script type= ' text/javascript ' > alert (' Delete succeeded! '); location.href= '. /index.php ';</script> ');
?>

10, how to conduct member login verification
<?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 (' Login succeeded! "); location.href= ' admin.php ';</script>");}
Else
{
Echo ("<script type= ' Text/javascript ' > Alert (') you entered the username or password incorrectly, please re-enter!" "); location.href= ' login.php ';</script& gt;");
}
Mysql_close ();
?>

11, how to test the session (background check the production of the page)
<?php
Session_Start ();
if (!isset ($_session["admin")) {
Header ("location:login.php");
Exit
}
?>

12, verify the user name and password is filled (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 ("Please enter your username!");
return false;
}
if (document.frmmain.password.value.length<4)
{
Document.frmmain.password.focus ();
Document.frmmain.password.select;
Window.alert ("Please enter your password!");
return false;
}
return true;
}
-->
</SCRIPT>

13, the way to invoke the editor in PHP
1 Place the Editor folder in the Background Management folder.
2) Use the following statement to introduce the operation.
<input name= "Content" type= "hidden" value= ' >
<iframe id= "EWebEditor1" src= "Ewebeditorphp38/ewebeditor.htm?id=content&style=coolblue" frameborder= "0" Scrolling= "No" width= "height=" ></IFRAME>
Note: The name of the EWebEditorPHP38 Editor folder.
Content in Id=content is the name of the hidden field above

14, the cycle of output (to achieve the breakdown)
1 insert a row of tables first
<?php
$i = 1;
?>
<table>
<tr>
<?php
while ($rs =mysql_fetch_array ($conn)) {
?>
<td>
Other tables and outputs that are recycled
</td>
<?php
if ($i% 2==0) {
echo "</tr><tr>";
}
$i + +;
}
?>
</tr>
</table>

15. Bind the data to the Drop-down list box (and select it by default when you modify it)
<select name= "Fenleiid" >
<?php
$sql = "SELECT * from Fenleibiao";
$conn =mysql_query ($sql, $connec);
while ($rs 1=mysql_fetch_array ($conn)) {
?>
<option value= "<?= $rs 1[" fenleiid "]?>"
?
if ($rs ["Fenleiid"]== $rs 1["Fenleiid"]) {
echo "Selected";
}
?>>
<?= $rs 1["Flname"]?>
</option>
<?php>
}
?>
</select>

16. Get the character length function
Strlen ($c) >12

17. Define a character intercept function
Usage: <?=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, judge whether is the number
!is_numeric (QQ)

19, PHP technology to get the current date
$ptime =date ("y-m-d");

20, the user registers the PHP verification program which uses
if ($admin = = "" or (strlen ($admin) >16) or (strlen ($admin) <2)) {
echo "<script language=javascript>alert (' Please enter username (not greater than 16 less than 2) ');";
echo "This.location.href= ' Vbscript:history.back () ';</script>";
}
if ($password = = "" or strlen ($password) >16 or strlen ($password) <6) {
echo "<script language=javascript>alert (' password length is 6-16 characters ');";
echo "This.location.href= ' Vbscript:history.back () ';</script>";
}
if ($password = = "") {
echo "<script language=javascript>alert (' Confirm password cannot be empty ');";
echo "This.location.href= ' Vbscript:history.back () ';</script>";
}else{
if ($password!= $password 1) {
echo "<script Language=javascript>alert (' Password and Confirm password inconsistent ');";
echo "This.location.href= ' Vbscript:history.back () ';</script>";
}
}
if ($WT = "") {
echo "<script language=javascript>alert (' Password problem cannot be empty ');";
echo "This.location.href= ' Vbscript:history.back () ';</script>";
}
if ($da = "") {
echo "<script language=javascript>alert (' question answer cannot be empty ');";
echo "This.location.href= ' Vbscript:history.back () ';</script>";

}
if ($qq!= "") {
if (!is_numeric ($QQ)) {
echo "<script Language=javascript>alert (' QQ number must be number ');";
echo "This.location.href= ' Vbscript:history.back () ';</script>";
}
}
if ($youbian = = "" or strlen ($youbian)!=6) {
echo "<script language=javascript>alert (' Please enter the ZIP code correctly ');";
echo "This.location.href= ' Vbscript:history.back () ';</script>";
}
if ($youbian!= "") {
if (!is_numeric ($youbian)) {
echo "<script language=javascript>alert (' Zip must be number ');";
echo "This.location.href= ' Vbscript:history.back () ';</script>";
}
}
if ($dizhi = "") {
echo "<script language=javascript>alert (' address cannot be empty ');";
echo "This.location.href= ' Vbscript:history.back () ';</script>";
}
if ($mail = = "") {
echo "<script Language=javascript>alert (' e-mail cannot be empty!") ');";
echo "This.location.href= ' Vbscript:history.back () ';</script>";
}
if ($textarea = = "") {
echo "<script language=javascript>alert (' personal description cannot be empty!") ');";
echo "This.location.href= ' Vbscript:history.back () ';</script>";
}
if ($textarea = = "" or strlen (TEXTAREA) >150) {
echo "<script language=javascript>alert (' personal description to 150 characters ');";
echo "This.location.href= ' Vbscript:history.back () ';</script>";
}

24, the output of the content to determine, so as to output other results
<?php
if ($rs ["active"]==1) {
echo "<font color= ' #ff0000 ' > Activation </font>";
}else{
echo "Disabled";
}
?>

25. Character Intercept function
<?=substr ("$rs [Zixun_biaoti]", 0,28)?>

26. Male and female issues or selection of a single selection
<input type= "Radio" name= "Hy_zhuangtai" value= "male" <?php if ($rs ["Hy_zhungtai"]=== "male") {echo "checked";}? >> Male
<input type= "Radio" name= "Hy_zhuangtai" value= "female" <?php if ($rs ["Hy_zhuangtai"]=== "female") {echo "checked";}? >> Women

27. Single selection without a radio box
<?php if ($rs [' Hy_zhuangtai ']== ' lock ') {?>
<a href= "userzt.php?action=yes&id=<?php echo $rs [' hy_id '];? > > Lock </a>
<?php}
Else{?>
<a href= "userzt.php?id=<?php echo $rs [' hy_id '];? >&action=no "> Unlock </a>
<?php}?>
Its save page is
<?php require_once (' http://www.cnblogs.com/conn.php ');?>
<?php
$hy _id=$_get[' id '];
$action =$_get[' action '];
if ($action = = ' yes ') {
$sql = "Update Hybiao set hy_zhuangtai= ' lock ' where hy_id= ' $id '";
$query =mysql_query ($sql, $connec);
Echo ("<script type= ' text/javascript ' >location.href= ' usermanage.php ';</script> ')";
}
else{
$sql = "Update Hybiao set hy_zhuangtai= ' normal ' where hy_id= ' $id '";
$query =mysql_query ($sql, $connec);
Echo ("<script type= ' text/javascript ' >location.href= ' usermanage.php ';</script> ')";
}
Mysql_close ();
?>

28. If the text is too long, then the long section becomes an ellipsis display
<div style= "width:120px; height:50px; border:0px solid Blue;overflow:hidden; Text-overflow:ellipsis ">
<NOBR> is for example, there is a line of text, very long, a row in the table does not show .</nobr>
</DIV>

29.
Disable replication, mouse drag selection
<body Ondragstart=window.event.returnvalue=false Oncontextmenu=window.event.returnvalue=false onselectstart= Event.returnvalue=false>
30. Changes in large and small text
<script type= "Text/javascript" >
function Dozoom (size)
{document.getElementById (' Zoom '). style.fontsize=size+ ' px ';}
</script>
<span id= "Zoom" > need to specify the size of the text </span>
<a href= "Javascript:dozoom" > Big </a> <a href= "Javascript:dozoom ()" > </a> <a href= " Javascript:dozoom () > Small </a>

30. Add to Favorites and set as homepage
<a href=# onclick= "this.style.behavior= ' url (#default #homepage)"; This.sethomepage (' http://www.makewing.com/lanren/'); " > Homepage </a>
<a href= "Javascript:window.external.AddFavorite (' http://www.makewing.com/lanren/', ' Lazy gallery ')" > Favorites Site </a>

31. Record and display the last modification time of the Web page
<script language=javascript>
document.write ("Last update:" + document.lastmodified + "")
</script>

32. Holiday Countdown
<script language= "JavaScript" >
var timedate= new Date ("October 1,2002");
var times= "National Day";
var now = new Date ();
var date = Timedate.gettime ()-now.gettime ();
var time = Math.floor (Date/(1000 * 60 * 60 * 24));
if (Time >= 0)
document.write ("Now away from" +times+ "also:" +time + "Day")
</Script>

33. Open the window is maximized
<script language= "JavaScript" >
<!--Begin
Self.moveto (0,0)
Self.resizeto (Screen.availwidth,screen.availheight)
End-->
</script>

34. Add background music
<bgsound src= "Mid/windblue[1].mid" loop= "-1" > only applies to IE
<embed src= "Music.mid" autostart= "true" loop= "true" hidden= "true" > for Netscape, IE all apply

35. Rolling
<marquee direction=up height=146 Onmouseout=start () onmouseover=stop () scrollamount=2>
Scrolling information
</marquee>

36. Prevent the click of the empty link, the page is often reset to the first end of the page
Code "javascript:void (NULL)" replaces the original "#" tag

37. Can not point right button, do not use CTRL + A, can not copy!
<body oncontextmenu= "Window.event.returnvalue=false"
Onkeypress= "Window.event.returnvalue=false"
Onkeydown= "Window.event.returnvalue=false"
onkeyup= "Window.event.returnvalue=false"
Ondragstart= "Window.event.returnvalue=false"
onselectstart= "Event.returnvalue=false" >
</body>

37. Random Transform background image (a special effect that can refresh mood)
<script language= "JavaScript" >
Image = New Array (4); An array that defines image as the number of pictures
Image [0] = ' tu0.gif '//Background image path
Image [1] = ' tu1.gif '
Image [2] = ' tu2.gif '
Image [3] = ' tu3.gif '
Image [4] = ' tu4.gif '
Number = Math.floor (Math.random () * image.length);
document.write ("<body background=" +image[number]+ ">");
</Script>

38. Across the chain to inherit the mouse
Style= "Cursor:hand"

39. How to close the layer
<div id= "Layer1" ></div>
<a href= "#" onclick= "layer1.style.display= ' None '" > Shutdown layer </a>

40.<a href=javascript:close () >[closes the window]</a>

41. Sunken text background is gray
<div style= "Width:300px;padding:20px;overflow:hidden;word-wrap:break-word;word-break:break:all; font-size:12px; line-height:18px; Background-color: #eeeeee; " >
<font disabled>
What's up, I'm going to dent it? <br>
Don't you want to give it a try? <br>
<a href= "www.lenvo.cnhttp://www.lenvo.cn/" >www.lenvo.cn</a></font>
</div>

42. Make a link to the form
<table width= "100%" onclick= window.open (' http://www.makewing.com/', ' _blank ') ' style= ' Cursor:hand ' >
<tr>
&LT;TD height= "bgcolor=" F4f4f4 > </td>
</tr>
</table>

43. Back & Close Window
Back: Javascript:history.back (1)
Close: Javascript:window.close ();

44. If the text is too long, then the long section becomes an ellipsis display
<div style= "width:120px; height:50px; border:0px solid Blue;overflow:hidden; Text-overflow:ellipsis ">
<NOBR> is for example, there is a line of text, very long, a row in the table does not show .</nobr>
</DIV>

45. Prohibit copying, mouse drag selection
<body Ondragstart=window.event.returnvalue=false Oncontextmenu=window.event.returnvalue=false onselectstart= Event.returnvalue=false>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.