2010年6月12日 星期六

留言板

<SCRIPT LANGUAGE="JAVASCRIPT">

function check_data()

{

if (document.myForm.author.value.length == 0)

{

alert("作者欄位不可以空白哦!");

return false;

}

if (document.myForm.subject.value.length == 0)

{

alert("主題欄位不可以空白哦!");

return false;

}

if (document.myForm.content.value.length == 0)

{

alert("內容欄位不可以空白哦!");

return false;

}

myForm.submit();

}

</SCRIPT>

</HEAD>

<BODY>

<table width="600" border="0" align="center" cellpadding="0" cellspacing="0" >

<tr>

<td width="200" rowspan="2" valign="top"><div id="forecast"><br><embed wmode="transparent" allowScriptAccess="always" style="WIDTH: 180px; HEIGHT: 220px" src="http://weather.hinet.net/flash/city/taipei2.swf"/></embed></div>

<br>累積瀏覽人數:<br> <div class="counter"><?php

$now_counter = counter();

for($i=0;$i<(strlen($now_counter));$i++)

echo("<img src=c".substr($now_counter,$i,1).".png align=absmiddle>");

?>

</div>

</td>

<td width="400"><img src="TITLE.png" width="400" height="90"></td>

</tr>

<tr>

<td><?php

//指定每頁顯示幾筆記錄

$records_per_page = 5;



//取得要顯示第幾頁的記錄

if (isset($_GET["page"]))

$page = $_GET["page"];

else

$page = 1;



//建立資料連接

$link = mysql_connect("localhost", "root", "*****");

if (!$link) die("建立資料連接失敗");



//開啟資料表

$db_selected = mysql_select_db("guestbook", $link);

if (!$db_selected) die("開啟資料庫失敗");



//執行 SQL 命令

$sql = "SELECT * FROM message ORDER BY date DESC";

$result = mysql_query($sql, $link);

if (!$result) die("執行 SQL 命令失敗");



//取得記錄數

$total_records = mysql_num_rows($result);



//計算總頁數

$total_pages = ceil($total_records / $records_per_page);



//計算本頁第一筆記錄的序號

$started_record = $records_per_page * ($page - 1);



//將記錄指標移至本頁第一筆記錄的序號

mysql_data_seek($result, $started_record);

//使用 $bg 陣列來儲存表格背景色彩

$bg[0] = "#84D7FF";

$bg[1] = "#D9F2FF";

$bg[2] = "#84D7FF";

$bg[3] = "#D9F2FF";

$bg[4] = "#84D7FF";

echo "<TABLE WIDTH='400' ALIGN='CENTER' CELLSPACING='3'>";



//顯示記錄

$j = 1;

while ($row = mysql_fetch_assoc($result) and $j <= $records_per_page)

{

echo "<TR BGCOLOR='" . $bg[$j - 1] . "'>";

echo "<TD WIDTH='120' ALIGN='center'>

<IMG SRC='" . mt_rand(0, 8) . ".jpg'></TD>";

echo "<TD>作者:" . $row["author"] . "<BR>";

echo "主題:" . $row["subject"] . "<BR>";

echo "時間:" . $row["date"] . "<HR>";

echo $row["content"] . "</TD></TR>";

$j++;

}

echo "</TABLE>" ;



//產生導覽列

echo "<P ALIGN='center'>";



if ($page > 1)

echo "<A HREF='index.php?page=". ($page - 1) . "'>上一頁</A> ";



for ($i = 1; $i <= $total_pages; $i++)

{

if ($i == $page)

echo "$i ";

else

echo "<A HREF='index.php?page=$i'>$i</A> ";

}



if ($page < $total_pages)

echo "<A HREF='index.php?page=". ($page + 1) . "'>下一頁</A> ";

echo "</P>";



//釋放記憶體空間

mysql_free_result($result);

mysql_close($link);

?>

<div class="guestform"><FORM NAME="myForm" METHOD="post" ACTION="post.php">

<TABLE BORDER="0" WIDTH="400" ALIGN="center" CELLSPACING="0">

<TR HEIGHT="30" BGCOLOR="#0084CA" ALIGN="center" VALIGN="middle">

<TD COLSPAN="2" class="ppp">

<FONT COLOR="#105484">請在此輸入新的留言</FONT></TD>

</TR>

<TR HEIGHT="30" BGCOLOR="#D9F2FF" ALIGN="center" VALIGN="middle">

<TD WIDTH="30%">作者</TD>

<TD WIDTH="70%"><INPUT NAME="author" TYPE="text" SIZE="40"></TD>

</TR>

<TR HEIGHT="30" BGCOLOR="#84D7FF" ALIGN="center" VALIGN="middle">

<TD WIDTH="30%">主題</TD>

<TD WIDTH="70%"><INPUT NAME="subject" TYPE="text" SIZE="40"></TD>

</TR>

<TR HEIGHT="150" BGCOLOR="#D9F2FF" ALIGN="center" VALIGN="middle">

<TD WIDTH="30%">內容</TD>

<TD WIDTH="70%"><TEXTAREA NAME="content" COLS="34" ROWS="6"></TEXTAREA></TD>

</TR>

<TR>

<TD COLSPAN="2" HEIGHT="40" ALIGN="center">

<INPUT TYPE="button" VALUE="張貼留言" onClick="check_data()"> 

<INPUT TYPE="reset" VALUE="重新輸入"></TD>

</TR>

</TABLE>

</FORM></div></td>

</tr>

</table>

<P ALIGN="center">&nbsp;</P>



<?php

function counter() {

$file="cdetail.txt";

if(@file_exists($file)) {

chmod($file,0222); //設定檔案可寫入

if(@$open_file=fopen($file,"r")) {

$text=intval(fread($open_file,filesize($file)));

++$text;

fclose($open_file);

$open_file = fopen($file, "w");

fwrite($open_file,$text);

fclose($open_file);

chmod($file,0444); //設定檔案唯讀

return "$text";

}

else

return "開啟計數檔失敗!";

}

else { //第一次瀏覽

$open_file=fopen($file,"w");

if(@fwrite($open_file,"1",1)==false)

return "寫入檔案失敗!";

else

return "1";

}

}

?>

沒有留言:

張貼留言

google ad