เขาทำกันอย่างไง ที่ผมอ่านบทเรียน php ในเว็บนี้เวลาเราจะทำเว็บบอร์ดขึ้นมา เราก็จะต้องเขียน php ออกมาหลายๆไฟร์
ที่ผมอ่านบทเรียน php ในเว็บนี้เวลาเราจะทำเว็บบอร์ดขึ้นมา เราก็จะต้องเขียน php ออกมาหลายๆไฟร์ เช่น webboard.php addtopic.php showtopic.php deletetopic.php เป็นต้น แต่ผมที่ผมเห็นเว็บอื่นเขาเขียนทำไมมีแค่ไฟร์เดียวเองหล่ะ ก็จะมีแค่ webboard.php แค่ไฟร์เดียว สามารถที่จะลบกระทู้ แก้ไขกระทู้ เพิ่มกระทู้ สามารถทำงานอยู่ในไฟร์แค่ตัวเดียว เขาเขียนกันอย่างไงเหรอ ผมคิดว่าการเขียนแบบนี้มันจะสะดวกดีนะ ใครรู้บ้างบอกหน่อย หรือช่วยสอนผมหน่อยก็ได้ ผมมีตัวอย่างมาให้ดูด้วยนะแต่ผมไม่ค่อยเข้าใจ
<?php
include ("config.php");
if( ($act<3) || ($act==6) || ($act==9) )
{
include("function.php");
}
switch($act) :
case "1" :
$nofollow ="1";
themehead(_LANG_51);
bbmenu();
echo "<br>";
writemsg($act,$topic,$rep,$REMOTE_ADDR);
break;
case "2" :
$nofollow ="1";
themehead(_LANG_51);
bbmenu();
echo "<br>";
writemsg($act,$topic,$rep,$REMOTE_ADDR);
break;
case "3" :
if(!eregi($_SERVER["HTTP_HOST"],$HTTP_REFERER)){ exit; }
if( (trim($b1))&&(trim($b2))&&(trim($b3)) )
{
if($b5)
{
$sql = mysql_db_query($dbname,"select password from ".$fix."user where userid='1' and password='$b5' ");
$adminpost = (mysql_num_rows($sql)==1) ? "1" : "";
}
$b1=addslashes(htmlspecialchars($b1));
$b2=addslashes(htmlspecialchars($b2));
$b3=addslashes(htmlspecialchars($b3));
mysql_db_query($dbname,"insert into ".$fix."bb values('','$b1','$b2','$b3','$createon','$REMOTE_ADDR','$adminpost')");
echo "<script language=javascript>location='bb.php?topic=".mysql_insert_id()."';</script>";
}
break;
case "4" :
if(!eregi($_SERVER["HTTP_HOST"],$HTTP_REFERER)){ exit; }
$sql = mysql_db_query($dbname,"select password from ".$fix."user where userid='1' and password='$b5' ");
if(mysql_num_rows($sql)==1)
{
$b1=addslashes(htmlspecialchars($b1));
$b2=addslashes(htmlspecialchars($b2));
$b3=addslashes(htmlspecialchars($b3));
mysql_db_query($dbname,"update ".$fix."bb set title='$b1', description='$b2', pname='$b3' where id='$topicid' ");
}
echo "<script language=javascript>location='bb.php?topic=$topicid';</script>";
break;
case "5" :
if(!eregi($_SERVER["HTTP_HOST"],$HTTP_REFERER)){ exit; }
include "lang.php";
$sql = mysql_db_query($dbname,"select password from ".$fix."user where userid='1' and password='$b5' ");
if(mysql_num_rows($sql)==1)
{
@mysql_db_query($dbname,"delete from ".$fix."bb where id='$topic' ");
@mysql_db_query($dbname,"delete from ".$fix."bb2 where topicid='$topic' ");
echo "<script language=javascript>alert('"._LANG_49."'); location='bb.php';</script>";
}else
echo "<script language=javascript>alert('"._LANG_50."'); history.back();</script>";
break;
case "6" :
if(!eregi($_SERVER["HTTP_HOST"],$HTTP_REFERER)){ exit; }
if( (trim($b2))&&(trim($b3)) )
{
if($b5)
{
$sql = mysql_db_query($dbname,"select password from ".$fix."user where userid='1' and password='$b5' ");
$adminpost = (mysql_num_rows($sql)==1) ? "1" : "";
}
if( ($bbsys!=2) && (!$adminpost) )
{
echo "<script language=javascript>alert('"._LANG_47_3."'); location='bb.php?topic=$topicid';</script>"; exit;
}
$b2=addslashes(htmlspecialchars($b2));
$b3=addslashes(htmlspecialchars($b3));
mysql_db_query($dbname,"insert into ".$fix."bb2 values('','$topicid','$b2','$b3','$createon','$REMOTE_ADDR','$adminpost')");
}
echo "<script language=javascript>location='bb.php?topic=$topicid';</script>";
break;
case "7" :
if(!eregi($_SERVER["HTTP_HOST"],$HTTP_REFERER)){ exit; }
$sql = mysql_db_query($dbname,"select password from ".$fix."user where userid='1' and password='$b5' ");
if(mysql_num_rows($sql)==1)
{
$b2=addslashes(htmlspecialchars($b2));
$b3=addslashes(htmlspecialchars($b3));
mysql_db_query($dbname,"update ".$fix."bb2 set description='$b2', pname='$b3' where id='$repid' ");
}
echo "<script language=javascript>location='bb.php?topic=$topicid';</script>";
break;
case "8" :
if(!eregi($_SERVER["HTTP_HOST"],$HTTP_REFERER)){ exit; }
include "lang.php";
$sql = mysql_db_query($dbname,"select password from ".$fix."user where userid='1' and password='$b5' ");
if(mysql_num_rows($sql)==1)
{
mysql_db_query($dbname,"delete from ".$fix."bb2 where id='$repid' ");
echo "<script language=javascript>alert('"._LANG_49."'); location='bb.php?topic=$topic';</script>";
}else
echo "<script language=javascript>alert('"._LANG_50."'); history.back();</script>";
break;
case "9" :
$nofollow ="1";
themehead(_LANG_51);
bbmenu();
echo "<br><center><script>Hbox('300','$folder','$syscolor3')</script><center><font color=red>";
if($repid) echo _LANG_48_1; else echo _LANG_48;
echo "</font><table><form method=post action=bb.php><tr><td align=center>
<input type=hidden name=act value=\"$act2\">
<input type=hidden name=topic value=\"$topicid\">
<input type=hidden name=repid value=\"$repid\">
Password: <input type=password name=b5 size=8> <input type=submit value=\" OK \"></td></tr></form></table>";
echo "</center><script>Fbox('$folder')</script></center>";
break;
default:
if($topic)
{
$sql = mysql_db_query($dbname,"select * from ".$fix."bb where id='$topic' ");
$row = mysql_num_rows($sql);
if($row) $arr = mysql_fetch_array($sql);
}
if (empty($page)){$page=1;}
if($arr[1])
themehead($arr[1]);
else{
$nofollow ="1";
themehead(_LANG_51." Page$page");
}
bbmenu();
$adminpost = ($arr[6]==1) ? "<u title=\""._LANG_52_6."\"><font color=green>".stripslashes($arr[3])."</font></u><font color=red>*</font>" : "<u title=\"ip:$arr[5]\">".stripslashes($arr[3])."</u>";
$arr[2] = eregi_replace("((http|ftp|https)://[^[:space:]<>]{3,})","<a href=\"\\1\" target=_blank>\\1</a>",$arr[2]);
if($row)
{
echo "<br><center>
<script>Hbox('95%','$folder','$syscolor3')</script>
<table width=\"100%\" cellspacing=1 cellpadding=0 bgcolor=\"$syscolor2\"><tr><td>
<table width=\"100%\" cellspacing=1 cellpadding=2 bgcolor=white>
<tr>
<td background=\"$folder/bgbb2.gif\" align=center height=38><font color=\"$color1\"><b>".stripslashes($arr[1])."</b></font></td>
</tr>
<tr>
<td valign=top bgcolor=white colspan=2><table width=\"100%\" cellpadding=10><tr><td>".nl2br(stripslashes($arr[2]))."</td></tr></table><br><div align=right><a href=\"bb.php?topic=$topic&act=2\" rel=\"nofollow\"><img src=\"$folder/edit.gif\" alt=\""._LANG_40_1."\" border=0></a> <a href=\"bb.php?topicid=$topic&act=9&act2=5\" rel=\"nofollow\"><img src=\"$folder/delete.gif\" alt=\""._LANG_41."\" border=0></a></div></td>
</tr>
<tr bgcolor=\"$syscolor1\">
<td><table width=\"100%\" cellpadding=0 cellspacing=0><tr><td class=small><font color=\"#999999\">"._LANG_52_2." ".sprintf("%05d","$arr[0]")."@".datetimebb($arr[4])."</font></td>
<td align=right class=small><font color=\"#999999\">"._LANG_39." $adminpost</font></td></tr></table></td>
</tr></table></td></tr></table><br>";
$sql = mysql_db_query($dbname,"select * from ".$fix."bb2 where topicid='$arr[0]' order by id asc");
$i=1;
while($bb2arr = mysql_fetch_array($sql))
{
$adminpost2 = ($bb2arr[6]==1) ? "<u title=\""._LANG_52_6."\"><font color=green>".stripslashes($bb2arr[3])."</font></u><font color=red>*</font>" : "<u title=\"ip:$bb2arr[5]\">".stripslashes($bb2arr[3])."</u>";
$bb2arr[2] = eregi_replace("((http|ftp|https)://[^[:space:]<>]{3,})","<a href=\"\\1\" target=_blank>\\1</a>",$bb2arr[2]);
echo "\n
<table width=\"100%\" cellspacing=1 cellpadding=0 bgcolor=\"$syscolor2\"><tr><td>
<table width=\"100%\" cellspacing=1 cellpadding=2 bgcolor=white>
<tr><td valign=top bgcolor=white><table cellpadding=10><tr><td>".nl2br(stripslashes($bb2arr[2]))."</td></tr></table>
<br><div align=right><a href=\"bb.php?topic=$topic&act=2&rep=$bb2arr[0]\" rel=\"nofollow\"><img src=\"$folder/edit.gif\" alt=\""._LANG_40_1."\" border=0></a> <a href=\"bb.php?topicid=$topic&act=9&act2=8&repid=$bb2arr[0]\" rel=nofollow><img src=\"$folder/delete.gif\" alt=\""._LANG_41."\" border=0></a></div></td></tr>
<tr bgcolor=\"$syscolor1\"><td><table width=\"100%\" cellspacing=0 cellpadding=0>
<tr><td class=small><font color=\"#999999\">"._LANG_52_1." $i@".datetimebb($bb2arr[4])."</font></td><td align=right class=small><font color=\"#999999\">"._LANG_42." $adminpost2</font></td></tr></table></td></tr></table></td></tr></table><br>\n"; $i++;
}
mysql_free_result($sql);
echo "<script>Fbox('$folder')</script><br>";
writemsg("",$arr[0],"",$REMOTE_ADDR);
echo "</center>";
}else{
$list_page = $Sbb;
$sql = mysql_db_query($dbname,"select * from ".$fix."bb");
$NRow = mysql_num_rows($sql);
if($NRow)
{
echo "<table width=\"100%\" cellspacing=1 cellpadding=0 bgcolor=\"$syscolor2\"><tr><td><table width=\"100%\" cellspacing=1 cellpadding=4 bgcolor=white><tr><td background=\"$folder/bgbb.gif\" colspan=2>"._LANG_44."</td><td background=\"$folder/bgbb.gif\" width=25 align=center>"._LANG_43."</td><td background=\"$folder/bgbb.gif\" width=65>"._LANG_52_3."</td></tr>";
$rt = $NRow%$list_page;
if($rt!=0) $totalpage = floor($NRow/$list_page)+1;
else $totalpage = floor($NRow/$list_page);
$goto = ($page-1)*$list_page;
$sql = mysql_db_query($dbname,"select * from ".$fix."bb order by id desc limit $goto,$list_page");
while($array=mysql_fetch_array($sql))
{
echo "<tr bgcolor=\"$syscolor1\">
<td width=25 class=small><font color=\"$color2\">".sprintf("%05d","$array[0]")."</font></td>
<td class=catbox><a href=\"bb.php?topic=$array[0]\"><font face=\"MS Sans Serif\">".stripslashes($array[1])."</font></a> : <font face=\"MS Sans Serif\" color=\"$syscolor\">$array[3]</font></td>
<td align=center class=small>".checkreply($array[0])."</td>
<td>".datetimebb($array[4])."</td>
</tr>";
}
mysql_free_result($sql);
echo "</table></td></tr></table><div align=right class=catbox>Page: ";
if(($page-1)!=0)
{
if(($page-1)==1) echo "<a href=\"bb.php\">«</a> ";
else echo "<a href=\"bb.php?page=".($page-1)."\">«</a> ";
}
for($i=1; $i<$page; $i++){ $pages = ($i!=1) ? "?page=$i" : ""; if( ($page-$i)<3) echo "<a href=\"bb.php".$pages."\">$i</a> "; }
echo "<font color=red>$page</font> ";
for($i=($page+1); $i<=$totalpage; $i++){ if( ($i-$page)<3) echo "<a href=\"bb.php?page=$i\">$i</a> "; }
if( ($page+1)<=$totalpage ) echo "<a href=\"bb.php?page=".($page+1)."\">»</a> ";
echo "</div>";
}else
echo "<br><center><font color=red>"._LANG_52."</font></center>";
}
endswitch;
?>
<?php
themefoot();
mysql_close($connection);
function checkreply($id)
{ global $fix,$dbname;
$sql = mysql_db_query($dbname,"select count(topicid) from ".$fix."bb2 where topicid='$id' ");
$row = "0";
$row = @mysql_result($sql,0);
return $row;
}
function bbmenu()
{ global $fix,$dbname,$topic,$act,$color1,$page;
echo "<table width=\"100%\" cellspacing=0 cellpadding=3><tr><td align=right class=linktitle>";
if( ($page>1) || ($act) || ($topic) )
echo "<a href=bb.php title=\""._LANG_37."\">"._LANG_37_1."</a> : ";
echo "<a href=bb.php?act=1>"._LANG_38."</font></a> : <a href=\"#\" onclick=\"searchbb('"._LANG_37_3."')\">"._LANG_37_2."</a>";
if( ($topic) && (!$act) )
{
$sql = mysql_db_query($dbname,"select id from ".$fix."bb where id<'$topic' limit 1");
$PP = @mysql_result($sql,0);
if($PP) echo " <a href=\"bb.php?topic=$PP\" title=\""._LANG_52_4."\"><font style=\"font-weight: bold; text-decoration: none; color: red\">«</font></a>";
$sql = mysql_db_query($dbname,"select id from ".$fix."bb where id>'$topic' limit 1");
$NP = @mysql_result($sql,0);
if($NP) echo " <a href=\"bb.php?topic=$NP\" title=\""._LANG_52_5."\"><font style=\"font-weight: bold; text-decoration: none; color: red\">»</font></a>";
}
echo "</td></tr></table>";
}
function writemsg($act,$topic,$rep,$IP)
{ global $syscolor1,$syscolor2,$folder,$fix,$dbname,$syscolor3;
if($act==1){ $titletext = _LANG_38; $actvalue = 3; }
elseif($act==2)
{
if($rep){ $titletext = _LANG_40_2.$rep; $actvalue = 7;
$sqlq = "select * from ".$fix."bb2 where id='$rep' and topicid='$topic' ";}
elseif($topic){ $titletext = _LANG_40_1; $actvalue = 4;
$sqlq = "select * from ".$fix."bb where id='$topic' ";}
$sql = @mysql_db_query($dbname,$sqlq);
$arr = @mysql_fetch_array($sql);
}
else{ $titletext = _LANG_40; $actvalue = 6; }
$ipaddr = ($arr[5]) ? "<font size=1 color=gray>ip:$arr[5]</font>" : "<font size=1 color=gray>ip:$IP</font>";
$MSG = ($arr[2]) ? stripslashes($arr[2]) : "";
$MSGby = ($arr[3]) ? stripslashes($arr[3]) : "";
echo "<center><script>Hbox('95%','$folder','$syscolor3')</script><center><b>$titletext</b></center><table width=\"100%\" cellspacing=1 cellpadding=0 bgcolor=\"$syscolor2\"><tr><td><table width=\"100%\" cellspacing=1 cellpadding=3 bgcolor=white><form name=bbpost action=bb.php method=post onsubmit=\"return checkbb('$act','$rep','"._LANG_52_8."')\">";
if( ($act) && (!$rep) )
echo "<tr bgcolor=\"$syscolor1\"><td align=right>"._LANG_44.":</td><td><input type=text name=b1 size=40 maxlength=100 value=\"".stripslashes($arr[1])."\"> *</td></tr>";
echo "
<tr bgcolor=\"$syscolor1\">
<td align=right>"._LANG_45."</td><td><textarea name=b2 rows=6 cols=50>$MSG</textarea> *</td>
</tr>
<tr bgcolor=\"$syscolor1\">
<td align=right>"._LANG_46."</td>
<td><input type=text name=b3 size=20 maxlength=30 value=\"$MSGby\">* $ipaddr</td>
</tr>
<tr bgcolor=\"$syscolor1\">
<td align=right>"._LANG_47."</td>
<td><input type=password size=8 name=b5> <u title=\""._LANG_47_2."\"><font color=red size=1>"._LANG_47_1."</font></u></td>
</tr>
<input type=hidden name=repid value=\"$rep\">
<input type=hidden name=topicid value=\"$topic\">
<input type=hidden name=act value=\"$actvalue\">
<tr bgcolor=\"$syscolor1\">
<td></td><td><input type=submit value=\" Send \"></td>
</tr>
</form></table></td></tr></table><script>Fbox('$folder')</script></center>";
}
?>
Tag : - - - -
Date :
3 ธ.ค. 2550 13:54:33
By :
surachai
View :
2919
Reply :
3
อันนี้ผมเดาเอานะ คือ อาศัยหลักการของการเขียนฟังก์ชั่น กับกรณี switch case ซึ่งกำหนด hidden form ขึ้นมา 1 9
1 ตัว คือ $act นั่นเอง แล้วก็อาศัย เงื่อนไข ไปตามฟังก์ชั่น ต่าง ๆ พูดง่าย ๆ ก็คือ รวมเอาทุกไฟล์ มาอยู่ในไฟลN
เดียว และให้แต่ละไฟล์ เท่ากับ 1 ฟังก์ขั่น
Date :
3 ธ.ค. 2550 14:17:59
By :
addy
เขาเรียนว่าการเขียนโปรแกรมแบบ oop จ๊ะ
Date :
3 ธ.ค. 2550 21:47:00
By :
พล
Date :
2011-08-25 09:49:07
By :
kolo
Load balance : Server 04