Web Hosting Deals Holiday Logo Design Webcam Chat Website Header Templates Register domain Search Engine Optimisation Web Hosting
Go Back   Talk Mania Forum > Tutorials > PHP / Perl / Java / JavaScript / CGI Tutorials

PHP / Perl / Java / JavaScript / CGI Tutorials PHP / Perl / Java / JavaScript / CGI Tutorials Please do not use this Forum to advertise your site or to link to tutorials.

 Image
Buy Sell Downloads

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-09-2007, 07:47 PM
Junior Member
 
Join Date: Feb 2007
Posts: 8
JinxHacker is on a distinguished road
Creating a simple PHP forum tutorial

if you've ever wondered how to build a simple forum in PHP/Mysql, this simple tutorial will give you the basics of how to post and reply to threads
and display messages.

First we need to create the SQL tables which will store the data for our threads and posts for this forum:

Code:

Code:
CREATE TABLE forumtutorial_posts (

  postid bigint(20) NOT NULL auto_increment,

  author varchar(255) NOT NULL default '',

  title varchar(255) NOT NULL default '',

  post mediumtext NOT NULL,

  showtime varchar(255) NOT NULL default '',

  realtime bigint(20) NOT NULL default '0',

  lastposter varchar(255) NOT NULL default '',

  numreplies bigint(20) NOT NULL default '0',

  parentid bigint(20) NOT NULL default '0',

  lastrepliedto bigint(20) NOT NULL default '0',

  PRIMARY KEY  (postid)

)
This is pretty basic. It creates the fields for the id of the post(this is how each post is identified), the author, the title, the time of post, the number of replies, if it was a reply, the thread it is a reply of, the last person to reply, and when the thread was last replied to.

Next we have the basic connect.php file to connect to our database:

Code:


Code:
<?php



$db = mysql_connect("localhost", "username", "password") or die("Could not connect.");

if(!$db) 

	die("no db");

if(!mysql_select_db("database_name",$db))

 	die("No database selected.");

if(!get_magic_quotes_gpc())

{

  $_GET = array_map('mysql_real_escape_string', $_GET); 

  $_POST = array_map('mysql_real_escape_string', $_POST); 

  $_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);

}

else

{  

   $_GET = array_map('stripslashes', $_GET); 

   $_POST = array_map('stripslashes', $_POST); 

   $_COOKIE = array_map('stripslashes', $_COOKIE);

   $_GET = array_map('mysql_real_escape_string', $_GET); 

   $_POST = array_map('mysql_real_escape_string', $_POST); 

   $_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);

}



?>
Simple, a simple connector function where you have to input your username, password, and database name to connect. Not that the function below starting at
if(!get_magic_quotes_gpc()) is not actually part of the connector function, it is simply to parse out mySQL injections and may not be needed. If your php to mysql extenson classes are not updated, mysql_escape_string may not work and you will have to take the parser lines out.

Next we have a stylesheet to give our forum colors and look:

Code:


Code:
<!---



body {

a:link, a:visited, a:active { text-decoration: none}

font-family:Verdana, Sans-serif;

color; #000000;

font-size: 12px

}

input,textarea, select,{

	color : #000000;

	font: normal 12px;

	border-collapse: collapse; border: 1px solid #000000;

}

.maintable {border: 0px ; width: 100%; padding: 0px; background-color: #FFFFFF} /*main table for forum*/

.regrow {font-family: Verdana,Sans-serif; color: #000000; font-weight: bold; background-color: #FFFFFF;font-size: 12px;} /*registration row, mainly here for symetry*/

.headline {font-family: Verdana,Sans-serif;font-weight: bold;color: #FFFFFF;background-color: #003366;font-size: 11px;} /*headline row, the first row that says forum name, topics, posts and such*/

.forumrow {font-family: Verdana,Sans-serif; color: #000000;background-color: #F2F2F2;font-size: 12px;} /*color of the forum rows*/

.mainrow a:link, a:visited,  a:active { text-decoration: none;}

.mainrow {font-family: Verdana,Sans-serif; color: #000000;background-color: #F2F2F2;font-size: 12px; a:link, a:visited, a:active { text-decoration: none}} /*color of the forum rows*/

.maintables{background-color: #FFFFFF; width: 95%; padding: 0px; border: 1px solid; cellspacing: no} /*main table for forum*/

--->
Simple basic stylesheet to define some table headings and colors we will use, you can change this as you please.

Now the actual message board files. We only need 4 files, index.php(the one to display all the threads), post.php(The file to post and start threads),
message.php(The file that displays the messages), and reply.php(The file to reply to posts).

First index.php:

Code:

Code:
<?php 

include "connect.php"; //mysql db connection here

print "<link rel='stylesheet' href='style.css' type='text/css'>";

print "<A href='post.php'>New Topic</a><br>";

print "<table class='maintable'>";

print "<tr class='headline'><td width=50%>Topic</td><td width=20%>Topic Starter</td><td>Replies</td><td>Last replied time</td></tr>";

$getthreads="SELECT * from forumtutorial_posts where parentid='0' order by lastrepliedto DESC";

$getthreads2=mysql_query($getthreads) or die("Could not get threads");

while($getthreads3=mysql_fetch_array($getthreads2))

{

  $getthreads3[title]=strip_tags($getthreads3[title]);

  $getthreads3[author]=strip_tags($getthreads3[author]);

  print "<tr class='mainrow'><td><A href='message.php?id=$getthreads3[postid]'>$getthreads3[title]</a></td><td>$getthreads3[author]</td><td>$getthreads3[numreplies]</td><td>$getthreads3[showtime]<br>Last post by <b>$getthreads3[lastposter]</b></td></tr>";

}

print "</table>";



?>
This is straightforward, it simply selects all threads with a parentid of 0, meaning that all threads that are not replies and it selects them by the last time they were replied to, beginning with the latest first. Then it goes through a loop to display all of them along with the number of replies they have, and the thread starter. It links the thread subject with a message identifier to message.php so people can view the thread by clicking on the link.
The strip_tags function parses out SQL injections that user might have put in.

Now the message.php file:

Code:

Code:
<?php 

include "connect.php"; //mysql db connection here

$id=$_GET['id'];

print "<link rel='stylesheet' href='style.css' type='text/css'>";

print "<A href='index.php'>Back to main forum</a>-<A href='post.php'>New Topic</a>-<A href='reply.php?id=$id'>Reply<br>";

print "<table class='maintable'>";

print "<tr class='headline'><td width=20%>Author</td><td width=80%>Post</td></tr>";

$gettopic="SELECT * from forumtutorial_posts where postid='$id'";

$gettopic2=mysql_query($gettopic) or die("Could not get topic");

$gettopic3=mysql_fetch_array($gettopic2);

print "<tr class='mainrow'><td valign='top'>$gettopic3[author]</td><td vakign='top'>Last replied to at $gettopic3[showtime]<br><hr>";

$message=strip_tags($gettopic3['post']);

$message=nl2br($message);

print "$message<hr><br>";

print "</td></tr>";

$getreplies="Select * from forumtutorial_posts where parentid='$id' order by postid desc"; //getting replies

$getreplies2=mysql_query($getreplies) or die("Could not get replies");

while($getreplies3=mysql_fetch_array($getreplies2))

{

   print "<tr class='mainrow'><td valign='top'>$getreplies3[author]</td><td vakign='top'>Last replied to at $getreplies3[showtime]<br><hr>";

   $message=strip_tags($getreplies3['post']);

   $message=nl2br($message);

   print "$message<hr><br>";

   print "</td></tr>";

}

print "</table>";



?>
This file gets some data passed to it in the form $id in the URL.
What this code does is it first selects the topic post in $gettopic, printing it and then selects all the posts in $getreplies and prints them. Strip_tags is there to parse out HTML injectons and nl2br is there to make sure line breaks work right.

Now the post.php file

Code:

Code:
<?php

include "connect.php"; //connection string

print "<link rel='stylesheet' href='style.css' type='text/css'>";

print "<table class='maintables'>";

print "<tr class='headline'><td>Post a message</td></tr>";

print "<tr class='maintables'><td>";

if(isset($_POST['submit']))

{

   $name=$_POST['name'];

   $yourpost=$_POST['yourpost'];

   $subject=$_POST['subject'];

   if(strlen($name)<1)

   {

      print "You did not type in a name."; //no name entered

   }

   else if(strlen($yourpost)<1)

   {

      print "You did not type in a post."; //no post entered

   }

   else if(strlen($subject)<1)

   {

      print "You did not enter a subject."; //no subject entered

   }

   else

   {

      $thedate=date("U"); //get unix timestamp

      $displaytime=date("F j, Y, g:i a");

      //we now strip HTML injections

      $subject=strip_tags($subject);

      $name=strip_tags($name);

      $yourpost=strip_tags($yourpost); 

      $insertpost="INSERT INTO forumtutorial_posts(author,title,post,showtime,realtime,lastposter) values('$name','$subject','$yourpost','$displaytime','$thedate','$name')";

      mysql_query($insertpost) or die("Could not insert post"); //insert post

      print "Message posted, go back to <A href='index.php'>Forum</a>.";

   }



}

else

{

   print "<form action='post.php' method='post'>";

   print "Your name:<br>";

   print "<input type='text' name='name' size='20'><br>";

   print "Subject:<br>";

   print "<input type='text' name='subject' size='20'><br>";

   print "Your message:<br>";

   print "<textarea name='yourpost' rows='5' cols='40'></textarea><br>";

   print "<input type='submit' name='submit' value='submit'></form>";



}

print "</td></tr></table>";

?>
Tune into part 2

Reply With Quote
  #2 (permalink)  
Old 04-09-2007, 08:05 PM
Member
 
Join Date: Feb 2007
Posts: 57
matthewgrainger is on a distinguished road
PHP Code:
CREATE TABLE forumtutorial_posts (

  
postid bigint(20NOT NULL auto_increment,

  
author varchar(255NOT NULL default '',

  
title varchar(255NOT NULL default '',

  
post mediumtext NOT NULL,

  
showtime varchar(255NOT NULL default '',

  
realtime bigint(20NOT NULL default '0',

  
lastposter varchar(255NOT NULL default '',

  
numreplies bigint(20NOT NULL default '0',

  
parentid bigint(20NOT NULL default '0',

  
lastrepliedto bigint(20NOT NULL default '0',

  
PRIMARY KEY  (postid)



... Why not make this in a php file, inlude the config file and make an installer?
__________________
StudentMeh.co.uk - Next Generation Student Networking!
Reply With Quote
  #3 (permalink)  
Old 04-09-2007, 11:47 PM
Junior Member
 
Join Date: Feb 2007
Posts: 8
JinxHacker is on a distinguished road
Could

I could but I didnt have time to do it at the moment.
Reply With Quote
  #4 (permalink)  
Old 04-10-2007, 07:02 AM
Base's Avatar
Senior Member
 
Join Date: Mar 2007
Location: North Yorkshire, UK
Posts: 121
Base is on a distinguished road
Send a message via MSN to Base Send a message via Skype™ to Base
Now i could be seriously stepping over the line.

But is the reason you don't explain the code, because you don't understand it?

You used some complicated MySQL functions here, when you didn't need to.

You say: "Simple basic stylesheet to define some table headings and colors we will use, you can change this as you please."

What happenes if people don't know how to change it?

What are they going to do?

If your writing a tutorial. Think of your audince.

I personally know that most of the people who are active members of talk-mania are not professional coders, or are not of a high level of knowledge of the language i am giving the tutorial on.

So i try and explain my code in a way that both professionals, and newbies alike can understand.

However, you seem more interested in getting your code on the page, than you do on helping members understand what the hell they are actually doing!

Base
__________________
http://www.pointserv.co.uk/gfx/deal_..._pointserv.png
PointServ.co.uk - One of the UK's cheapest webhosting companies.

Last edited by Base; 04-10-2007 at 07:13 AM.
Reply With Quote
  #5 (permalink)  
Old 04-11-2007, 12:06 AM
Junior Member
 
Join Date: Feb 2007
Posts: 8
JinxHacker is on a distinguished road
Yeah

Yeah. I think I know php, & MySQL by now.

The resons why I havent explained it it because I just dont have the time.
__________________
www.dontplaywithyourself.com
Reply With Quote
  #6 (permalink)  
Old 04-11-2007, 10:40 PM
Member
 
Join Date: Feb 2007
Posts: 57
matthewgrainger is on a distinguished road
Quote:
Originally Posted by JinxHacker View Post
Yeah. I think I know php, & MySQL by now.

The resons why I havent explained it it because I just dont have the time.
If you dont have the time then you mite as well not post it, if you dont explain it people are not going to learn and will just copy and pase...
__________________
StudentMeh.co.uk - Next Generation Student Networking!
Reply With Quote
  #7 (permalink)  
Old 04-11-2007, 11:00 PM
Base's Avatar
Senior Member
 
Join Date: Mar 2007
Location: North Yorkshire, UK
Posts: 121
Base is on a distinguished road
Send a message via MSN to Base Send a message via Skype™ to Base
See i don't think your doing to well in this community so far..

We will have to see tho!

Base
__________________
http://www.pointserv.co.uk/gfx/deal_..._pointserv.png
PointServ.co.uk - One of the UK's cheapest webhosting companies.
Reply With Quote
  #8 (permalink)  
Old 04-14-2007, 04:31 PM
Junior Member
 
Join Date: Apr 2007
Posts: 3
Darkless is on a distinguished road
Nice THX much
Reply With Quote
  #9 (permalink)  
Old 04-17-2007, 11:37 AM
Junior Member
 
Join Date: Apr 2007
Posts: 28
djven_om is on a distinguished road
very instructive
Reply With Quote
  #10 (permalink)  
Old 04-25-2007, 08:20 PM
Junior Member
 
Join Date: Apr 2007
Posts: 3
surgeboard is on a distinguished road
Ive seen something similar to this before, but either way, nice tut.

Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT +2. The time now is 10:26 PM.


Fake ID

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC8
Forums Copyright © Talk-Mania.com