Tải bản đầy đủ - 0 (trang)
Chapter 19. Creating a Simple Discussion Forum

Chapter 19. Creating a Simple Discussion Forum

Tải bản đầy đủ - 0trang

DesigningtheDatabaseTables

Thinkofthebasiccomponentsofaforum:topicsandposts.A

forumifproperlyusedbyitspatronsshouldhaveseveraltopics,

andeachofthosetopicswillhaveoneormorepostsbyusers.

Knowingthat,youshouldrealizethatthepostsaretiedtothe

topicsthroughakeyfield.Thiskeyformstherelationship

betweenthetwotables.

Thinkabouttherequirementsforthetopicsthemselves.You

definitelyneedafieldforthetitle,andsubsequentlyyoumay

wantfieldstoholdthecreationtimeandtheidentificationofthe

userwhocreatedthetopic.Similarly,thinkoftherequirements

fortheposts:Youwantthetextofthepost,thetimeitwas

created,andthepersoncreatingit.Mostimportantly,youneed

thatkeytotietheposttothetopic.

Thefollowingtwotablecreationstatementscreatethesetables,

calledforum_topicsandforum_posts:



mysql>createtableforum_topics(

->topic_idintnotnullprimarykeyauto_increment,

->topic_titlevarchar(150),

->topic_create_timedatetime,

->topic_ownervarchar(150)

->);



QueryOK,0rowsaffected(0.03sec)

mysql>createtableforum_posts(

->post_idintnotnullprimarykeyauto_increment,

->topic_idintnotnull,

->post_texttext,

->post_create_timedatetime,

->post_ownervarchar(150)

->);

QueryOK,0rowsaffected(0.00sec)



Inthisforumexample,wewillidentifyusersbytheiremail

addressesandnotrequireanysortofloginsequence.



Youshouldnowhavetwoemptytables,waitingforsomeinput.

Inthenextsection,you'llcreatetheinputformsforaddinga

topicandapost.



CreatingtheInputFormsandScripts

Beforeyoucanaddanyposts,youmustaddatopictothe

forum.Itiscommonpracticeinforumcreationtoaddthetopic

andthefirstpostinthattopicatthesametime.Fromauser's

pointofview,itdoesn'tmakemuchsensetoaddatopicand

thengoback,selectthetopic,andaddareply.Youwantthe

processtobeassmoothaspossible.

Listing19.1showstheformforanewtopiccreation,which

includesaspaceforthefirstpostinthetopic.



Listing19.1FormforAddingaTopic



1:

2:

3:AddaTopic

4:

5:

6:

AddaTopic



7:

8:

YourE-MailAddress:




9:
10:

TopicTitle:




11:
12:

PostText:




13:


14:


15:

16:

17:

Seemssimpleenoughthethreefieldsshownintheform,which

youcanseeinFigure19.1,areallyouneedtocompleteboth

tables;yourscriptanddatabasecanfillintherest.SaveListing

19.1assomethinglikeaddtopic.htmlandputitinyourWeb

serverdocumentrootsothatyoucanfollowalong.



Figure19.1.Thetopiccreationform.



Tocreatetheentryintheforum_topicstable,youusethe

topic_titleandtopic_ownerfieldsfromtheinputform.The

topic_idandtopic_create_timefieldswillbefilledin

automatically.Similarly,intheforum_poststable,youusethe

post_textandtopic_ownerfieldsfromtheinputform,andthe

post_id,post_create_time,andthetopic_idfieldswillbe

filledinautomatically.Becauseyouneedavalueforthe

topic_idfieldtobeabletocompletetheentryinthe

forum_poststable,youknowthatquerymusthappenafterthe

querytoinserttherecordintheforum_topicstable.

Listing19.2createsthescripttoaddtheserecordstothetable.



Listing19.2ScriptforAddingaTopic



1:
2://checkforrequiredfieldsfromtheform

3:if((!$_POST[topic_owner])||(!$_POST[topic_title])

4:||(!$_POST[post_text])){

5:header("Location:addtopic.html");

6:exit;

7:}

8:

9://connecttoserverandselectdatabase

10:$conn=mysql_connect("localhost","joeuser","somepass")

11:ordie(mysql_error());

12:mysql_select_db("testDB",$conn)ordie(mysql_error());

13:

14://createandissuethefirstquery



15:$add_topic="insertintoforum_topicsvalues('','$_POST[

16:now(),'$_POST[topic_owner]')";

17:mysql_query($add_topic,$conn)ordie(mysql_error());

18:

19://gettheidofthelastquery



20:$topic_id=mysql_insert_id();

21:

22://createandissuethesecondquery



23:$add_post="insertintoforum_postsvalues('','$topic_id

24:'$_POST[post_text]',now(),'$_POST[topic_owner]')";

25:mysql_query($add_post,$conn)ordie(mysql_error());

26:

27://createnicemessageforuser



28:$msg="

The$topic_titletopichasbeen

29:?>

30:

31:

32:NewTopicAdded

33:

34:

35:

NewTopicAdded



36:

37:



38:

Lines37checkforthethreerequiredfieldsweneedto

completebothtables.Ifeitheroneofthesefieldsisnot

present,theuserisredirectedtotheoriginalform.

Lines1012formthedatabaseconnection,whichshouldbe

familiartoyoubynow.Lines1517createandinsertthefirst

query,whichaddsthetopictotheforum_topicstable.Note

thatthefirstfieldisleftblank,sotheautomatically

incrementingnumberisaddedbythesystem.Similarly,the

now()functionisusedtotimestamptherecordwiththe

currenttime.Theotherfieldsintherecordarecompletedusing

valuesfromtheform.

Line20showstheuseofaveryhandyfunction:

mysql_insert_id().ThisfunctionretrievestheprimarykeyID

ofthelastrecordinsertedintothedatabasebythisscript.In

thiscase,mysql_insert_id()getstheidvaluefromthe

forum_topicstable,whichwillbecometheentryforthe

topic_idfieldintheforum_poststable.

Lines2325createandinsertthesecondquery,againusinga

mixtureofinformationknownandsuppliedbythesystem.Line

28simplycreatesamessagefortheuser,andtherestofthe

scriptroundsoutthedisplay.

Savethislistingasdo_addtopic.phpthenameoftheactionin

thepreviousscriptandplaceitinthedocumentrootofyour

Webserver.Completetheformandthensubmitit,andyou

shouldseetheNewTopicAddedmessage.Figures19.2and

19.3showthesequenceofevents.



Figure19.2.Addingatopicandfirstpost.



Figure19.3.Successfuladditionofatopicand

firstpost.



Inthenextsection,you'llputtogethertwomorepiecesofthe



puzzle:displayingthetopicsandposts,andreplyingtoatopic.



DisplayingtheTopicList

Nowthatyouhaveatopicandatleastonepostinyour

database,youcandisplaythisinformationandletpeopleadd

newtopicsorreplytoexistingones.InListing19.3,wetakea

stepbackandcreateatopiclistingpage.Thispagewillshow

thebasicinformationofeachtopicandprovidetheuserwitha

linktoaddanewtopic;youalreadyhavetheformandscript

forthat.Thisscriptwouldactuallybeanentrypageforyour

forum.



Listing19.3TopicListingScript



1:
2://connecttoserverandselectdatabase

3:$conn=mysql_connect("localhost","joeuser","somepass")

4:ordie(mysql_error());

5:mysql_select_db("testDB",$conn)ordie(mysql_error());

6:

7://gatherthetopics

8:$get_topics="selecttopic_id,topic_title,



9:date_format(topic_create_time,'%b%e%Yat%r')asfmt_to



10:topic_ownerfromforum_topicsorderbytopic_create_timede



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 19. Creating a Simple Discussion Forum

Tải bản đầy đủ ngay(0 tr)

×