It took me a few nights to write a message board for a beginner to learn about PHP.
P.s. My space does not support PHP and does not support T_T demonstration.
Database structure: (Database Name: lyb)
Table 1: admin
Field: id (int11) name (varchvr) password (varchvr)
Table 2: lo
Field: id (int11) username (varchvr) sex (varchvr) qq (varchvr) email (varchvr) info (text) ip (varchvr) submit_time (datetime)
1 conn. php (connect to the database file)
<? Php
Mysql_connect ("localhost", "root", ""); // connect to the database
Mysql_select_db ("lyb"); // select a database
?>
2 header. php (Public header file)
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> Silver Message Board Version 1.0 </title>
</Head>
<Link href = "css.css" rel = "stylesheet" type = "text/css"/>
<Body>
<? Php session_start ();?>
<Div id = "head">
<Div id = "head_l">
<Ul>
<Li> <a href = "index. php"> Leave a message </a> </li>
<Li> <a href = "post. php"> occasionally published </a> </li>
<? Php
// Session_start (); // DOFY indicates that no output content exists before this sentence. correct this.
If ($ _ SESSION ["key"] = 1) {// if the obtained SESSION is 1, the management item is displayed.
?>
<Li> <a href = "adminexit. php"> exit management </a> </li>
<?
}
Else
{
?>
<Li> <a href = "admin. php"> occasionally managed </a> </li>
<?}?>
</Ul>
</Div>
<Div id = "head_r"> Silver Message Board Version 1.0 </div>
</Div>
3 footer. php (Public bottom file)
Copy codeThe Code is as follows:
<? Php
$ CounterFile = "conter. xml ";
Function displayCounter ($ counterFile ){
$ Fp = fopen ($ counterFile, "rw ");
$ Num = fgets ($ fp, 5 );
$ Num + = 1;
Print "<div id = \" footer \ "> Silver Message Board Version 1.0". "$ num". "bored silver </div> ";
Exec ("rm-rf $ counterFile ");
Exec ("echo $ num> $ counterFile ");
}
If (! File_exists ($ counterFile )){
Exec ("echo 0> $ counterFile ");
}
DisplayCounter ($ counterFile );
?>
</Body>
</Html>
4 index. php (home page)
Copy codeThe Code is as follows:
<? Php
Require_once ("conn. php ");
Require_once ("header. php ");
Session_start ();
// Start with paging code
$ Pagesize = 10; // you can specify the number of entries displayed on each page.
$ Rs = mysql_query ("select count (*) from lo"); // retrieves the total number of records and calculates the total number of pages.
$ Myrow = mysql_fetch_array ($ rs );
$ Numrows = $ myrow [0]; // calculates the total number of records.
$ Pages = intval ($ numrows/$ pagesize );
If ($ numrows % $ pagesize) $ pages ++; // you can specify the number of pages.
If (isset ($ _ GET ['page'])
{
$ Page = intval ($ _ GET ['page']);
}
Else
{
$ Page = 1; // set it to the first page
}
$ Offset = $ pagesize * ($ page-1); // calculates the record offset.
// The paging code ends.
$ SQL = "select id, username, sex, qq, email, info, ip, DATE_FORMAT (submit_time, '% Y % m % d % t ') from lo order by id desc limit $ offset, $ pagesize "; // The DATE-FORMAT is used.
$ Result = mysql_query ($ SQL );
$ Num = mysql_num_rows ($ result );
If ($ num> 0 ){
While ($ row = mysql_fetch_array ($ result ))
{
// Echo print_r ($ row );
If ($ row [2] = "male") // change the gender to the name you want ^_^
{
$ Sex = "Shuai pan ";
}
Else
{
$ Sex = "beauty ";
}
?>
<Div id = "show">
<P class = "num"> NO. [<? = $ Row [0]?>] Messages <p>
<P class = "unline"> contact: <span class = "blue"> <? = $ Row [1]?> </Span> Gender: <? = $ Sex?> Message time: <? = $ Row [7]?> <? If ($ row [3]) {?> <A href =" http://wpa.qq.com/msgrd?V=1&Uin= <? = $ Row [3]?> & Site = http://www.loai.cn&Menu=yes "> "/> </A> <? }?> <? If ($ row [4]) {?> <A href = "mailto: <? = $ Row [4]?> "> "/> </A> <? }?> <? If ($ _ SESSION ["key"] = 1) {?> IP: <? = $ Row [6]?> <A href = "update. php? Wuleying & id = <? = $ Row [0]?> "> Change </a> <a href =" delete. php? Wuleying & id = <? = $ Row [0]?> "> Delete </a> <?}?> </A> </p>
<P class = "blue"> message content: </p>
<Div id = "show_info"> <? = Nl2br (htmlspecialchars ($ row [5])?> </Div>
</Div>
<? Php
}
}
Else
{
Echo "<div id = \" show \ "> no data... </div> ";
}
?>
<Div id = "show_page">
<P>
<? Php
$ First = 1;
$ Prev = $ page-1;
$ Next = $ page + 1;
$ Last = $ pages;
If ($ page = 1 & $ pages> 1)
{
Echo "homepage | ";
Echo "Previous Page | ";
Echo "<a href = \" index. php? Page = ". $ next." \ "> next page </a> | ";
Echo "<a href = \" index. php? Page = ". $ last." \ "> last page </a> | ";
}
Elseif ($ page >=1 & $ page! = $ Pages & $ num> 0)
{
Echo "<a href = \" index. php? Page = ". $ first." \ "> homepage </a> | ";
Echo "<a href = \" index. php? Page = ". $ prev." \ "> previous page </a> | ";
Echo "<a href = \" index. php? Page = ". $ next." \ "> next page </a> | ";
Echo "<a href = \" index. php? Page = ". $ last." \ "> last page </a> | ";
}
Elseif ($ page ==$ pages & $ page! = 1)
{
Echo "<a href = \" index. php? Page = ". $ first." \ "> homepage </a> | ";
Echo "<a href = \" index. php? Page = ". $ prev." \ "> previous page </a> | ";
Echo "next page | ";
Echo "last page | ";
}
Elseif ($ page = $ pages)
{
Echo "homepage | ";
Echo "Previous Page | ";
Echo "next page | ";
Echo "last page | ";
}
Else
{
Echo "homepage | ";
Echo "Previous Page | ";
Echo "next page | ";
Echo "last page | ";
}
?>
Total <span> <? = $ Pages?> </Span> page | current <span> <? = $ Page?> </Span> page | Total <span> <? = $ Numrows?> </Span> & nbsp messages </p>
</Div>
<? Php
Mysql_close ();
?>
<? Php
Require_once ("footer. php ");
?>