Download ppt - PHP Webboard

Transcript
Page 1: PHP Webboard

PHPWebboard

Page 2: PHP Webboard

Tables and files• 2 Tables(Webboard and Reply)• Webboard.php for show all question• NewQuestion.php for creating new question• ViewWebboard.php for show question ,replies and create new

reply• And include.php for connect database

Page 3: PHP Webboard

CREATE TABLE Webboard

CREATE TABLE `webboard` (`QuestionID` int(5) unsigned zerofill NOT NULL auto_increment,`CreateDate` datetime NOT NULL,`Question` varchar(255) NOT NULL,`Details` text NOT NULL,`Name` varchar(50) NOT NULL,`View` int(5) NOT NULL,`Reply` int(5) NOT NULL,PRIMARY KEY (`QuestionID`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Page 4: PHP Webboard

CREATE TABLE Reply

CREATE TABLE `reply` (`ReplyID` int(5) unsigned zerofill NOT NULL auto_increment,`QuestionID` int(5) unsigned zerofill NOT NULL,`CreateDate` datetime NOT NULL,`Details` text NOT NULL,`Name` varchar(50) NOT NULL,PRIMARY KEY (`ReplyID`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Page 5: PHP Webboard

Webboard.php

<html><body><a href="NewQuestion.php">New Topic</a><?include ("include.php");$strSQL = "SELECT * FROM webboard ";$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");$Num_Rows = mysql_num_rows($objQuery);$Per_Page = 10; // Per Page$Page = $_GET["Page"];

Page 6: PHP Webboard

if(!$_GET["Page"]){ $Page=1; }$Prev_Page = $Page-1;$Next_Page = $Page+1;$Page_Start = (($Per_Page*$Page)-$Per_Page);if($Num_Rows<=$Per_Page){ $Num_Pages =1; }else if(($Num_Rows % $Per_Page)==0){ $Num_Pages =($Num_Rows/$Per_Page) ; }else{ $Num_Pages =($Num_Rows/$Per_Page)+1;

$Num_Pages = (int)$Num_Pages;}$strSQL .=" order by QuestionID DESC LIMIT $Page_Start , $Per_Page";

Page 7: PHP Webboard

$objQuery = mysql_query($strSQL);?><table width="909" border="1"> <tr> <th width="99"> <div align="center">QuestionID</div></th> <th width="458"> <div align="center">Question</div></th> <th width="90"> <div align="center">Name</div></th> <th width="130"> <div align="center">CreateDate</div></th> <th width="45"> <div align="center">View</div></th> <th width="47"> <div align="center">Reply</div></th> </tr><?while($objResult = mysql_fetch_array($objQuery)){?>

Page 8: PHP Webboard

<tr> <td><div align="center"><?=$objResult["QuestionID"];?></div></td> <td><a href= "ViewWebboard.php?QuestionID=<?=$objResult["QuestionID"];?>"> <?

=$objResult["Question"];?></a></td> <td><?=$objResult["Name"];?></td> <td><div align="center"><?=$objResult["CreateDate"];?></div></td> <td align="right"><?=$objResult["View"];?></td> <td align="right"><?=$objResult["Reply"];?></td> </tr><?}?></table><br>

Page 9: PHP Webboard

Total <?= $Num_Rows;?> Record : <?=$Num_Pages;?> Page :<?if($Prev_Page){ echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page'><< Back</a> ";}for($i=1; $i<=$Num_Pages; $i++){

if($i != $Page){ echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]";}else{ echo "<b> $i </b>";}

}

Page 10: PHP Webboard

if($Page!=$Num_Pages){ echo " <a href = '$_SERVER[SCRIPT_NAME]?

Page=$Next_Page'>Next>></a> ";}mysql_close();?></body></html>

Page 11: PHP Webboard

NewQuestion.php<?include ("include.php");if($_GET["Action"] == "Save"){ //*** Insert Question ***//

$strSQL = "INSERT INTO webboard ";$strSQL .="(CreateDate,Question,Details,Name) ";$strSQL .="VALUES ";$strSQL .="('".date("Y-m-d H:i:s")."','".$_POST["txtQuestion"]."','".$_POST["txtDetails"]."','".$_POST["txtName"]."') ";$objQuery = mysql_query($strSQL);header("location:Webboard.php");

}

Page 12: PHP Webboard

?><html><body><form action="NewQuestion.php?Action=Save" method="post“ name="frmMain"

id="frmMain"> <table width="621" border="1" cellpadding="1" cellspacing="1"> <tr> <td>Question</td> <td><input name="txtQuestion" type="text" id="txtQuestion" value="" size="70"></td> </tr><tr> <td width="78">Details</td> <td><textarea name="txtDetails" cols="50" rows="5" id="txtDetails"></textarea></td> </tr>

Page 13: PHP Webboard

<tr> <td width="78">Name</td> <td width="647"><input name="txtName" type="text" id="txtName"

value="" size="50"></td> </tr> </table> <input name="btnSave" type="submit" id="btnSave" value="Submit"></form></body></html><?mysql_close();?>

Page 14: PHP Webboard

ViewWebboard.php

<?include ("include.php");if($_GET["Action"] == "Save"){ //*** Insert Reply ***//

$strSQL = "INSERT INTO reply ";$strSQL .="(QuestionID,CreateDate,Details,Name) ";$strSQL .="VALUES ";$strSQL .="('".$_GET["QuestionID"]."','".date("Y-m-d H:i:s")."','".$_POST["txtDetails"]."','".$_POST["txtName"]."') ";$objQuery = mysql_query($strSQL);

Page 15: PHP Webboard

//*** Update Reply ***//$strSQL = "UPDATE webboard ";$strSQL .="SET Reply = Reply + 1 WHERE QuestionID = '".$_GET["QuestionID"]."' ";$objQuery = mysql_query($strSQL);

}?><html><body><?//*** Select Question ***//$strSQL = "SELECT * FROM webboard WHERE QuestionID = '".

$_GET["QuestionID"]."' ";$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");$objResult = mysql_fetch_array($objQuery);

Page 16: PHP Webboard

//*** Update View ***//$strSQL = "UPDATE webboard ";$strSQL .="SET View = View + 1 WHERE QuestionID = '".$_GET["QuestionID"]."' ";$objQuery = mysql_query($strSQL);?><table width="738" border="1" cellpadding="1" cellspacing="1"> <tr> <td

colspan="2"><center><h1><?=$objResult["Question"];?></h1></center></td> </tr> <tr> <td height="53" colspan="2"><?=nl2br($objResult["Details"]);?></td> </tr>

Page 17: PHP Webboard

<tr> <td width="397">Name : <?=$objResult["Name"];?> Create Date : <?

=$objResult["CreateDate"];?></td> <td width="253">View : <?=$objResult["View"];?> Reply : <?

=$objResult["Reply"];?></td> </tr></table><br><br><?$intRows = 0;$strSQL2 = "SELECT * FROM reply WHERE QuestionID = '".$_GET["QuestionID"]."'

";$objQuery2 = mysql_query($strSQL2) or die ("Error Query [".$strSQL."]");

Page 18: PHP Webboard

while($objResult2 = mysql_fetch_array($objQuery2)){ $intRows++;?> No : <?=$intRows;?><table width="738" border="1" cellpadding="1" cellspacing="1"> <tr> <td height="53" colspan="2"><?=nl2br($objResult2["Details"]);?></td> </tr> <tr> <td width="397">Name : <?=$objResult2["Name"];?> </td> <td width="253">Create Date : <?=$objResult2["CreateDate"];?></td> </tr></table><br>

Page 19: PHP Webboard

<?}?><br><a href="Webboard.php">Back to Webboard</a> <br><br><form action="ViewWebboard.php?QuestionID=<?=$_GET["QuestionID"];?

>&Action=Save" method="post" name="frmMain" id="frmMain"> <table width="738" border="1" cellpadding="1" cellspacing="1"> <tr> <td width="78">Details</td> <td><textarea name="txtDetails" cols="50" rows="5"

id="txtDetails"></textarea></td> </tr>

Page 20: PHP Webboard

<tr> <td width="78">Name</td> <td width="647"><input name="txtName" type="text" id="txtName"

value="" size="50"></td> </tr> </table> <input name="btnSave" type="submit" id="btnSave" value="Submit"></form></body></html><?mysql_close();?>

Page 21: PHP Webboard

Include.php

<?mysql_connect("localhost","root","1234");mysql_select_db("test");?>


Recommended