Jump to content
xisto Community
Sign in to follow this  
SofiaComp

How To Make Your Own Post Board using OO php and mysql relational database

Recommended Posts

Hi there
To make a post board first we have to design and implement the database in this case we use mysql.
1. I decided to have 1 database called post board so enter in your favourite mysql tool and create one database:

create database your_database;
then i'll use 2 mysql tables : One for topics or "headings" if you like and on for the actual posts.
create table headings(id int not null primary key auto_increment, heading varchar(100) not null, author varchar(30) not null, date  datetime not null);
The second table - posts will have relation key transfering data from table headings to table posts:
create table posts(id int not null primary key auto_increment, post varchar(2000) not null, author varch(30) not null, date datetime not null,headings int not null, foreign key(headings) references headings.id);[\code]Now we start with the php part.first we need to create a connection to mysql, OOP has a good painless way of doing this[code]//Your_filename, Save it as YourClassName.class.phpclass YourClassName{<?phpfunction connect(){  $user="yourMYSQLusername";   $password="yourMYSQLpassword";  $host="yourWebServiceHost"//Usually localhost  mysql_connect($host,$username,$password);  }}?>

//filename_utils<?php//Your_filename1 Save it as YourClass1Name_Main.class.phpclass YourClass1Name_Main  public $heading;  public $author;   public $post;   function New_Topic(){  $query="Insert into headings (heading,author,date) values('$this->heading','$this->author',now());";  mysql_query($query)or die(mysql_error());  $query="Insert into post(post,author,date,heading)values('$this->post','$this->author',now(),LAST_INSERT_ID());";  mysq_query($query)or die(mysql_error());  }  function show_headings(){	$query="Select * from headings;";	$result=mysql_query($query);	while($row=mysql_fetch_assoc($result)){echo "{$row->heading}\n Posted by {$row->author}\n On {$row->date}";?><a href="post_into_topic.php?id=<?php echo $row->id;?>">Join</a><br /><?php	}  }  function set_a_cookie(){   ob_start();	$id=$_GET['id'];	setrawcookie("id",$id,time()+3600);	ob_get_contents();	ob_clean();  }  function post_message(){   $query="Insert into posts(post,author,date,headings)values('$this->post','$this->author',now()),'$_COOKIE['id'];";   mysql_query($query)or die(mysql_error());  }  function show_messages(){  $query="Select * from headings,posts where headings.id='$_COOKIE['id']';";  $result=mysql_query($query);  while($row=mysql_fetch_assoc($result)){ echo "{$row->heading}\n Author {$row->author}\n Date {$row->date}<br />"; echo "Answer: {$row->post}\n Author{$row->author}\n Date{$row->date}<br />;  }}?>
Now we need two files
//CreateATopic.php<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">Subject <input type="text" name="heading" />Author <input type="text" name="author" />Post <textarea cols="100" rows="100"></textarea><input type="submit" /></form><?php?>//Post_into_topic.php<form action="<?php echo "{$_SERVER['PHP_SELF']}?id={$_COOKIE['id']}";?>" method="post">Author <input type="text" name="author" />Post <textarea cols="100" row="100"></textarea><input type="submit" /></form>
Now we have to make one more file Index.php who will be the main page
//index.php<?phpinclude_once 'YourClassName'; $db=new DB(); $db->connect(); include_once 'YourClassName_Main'; $ClassName=new YourClassName_Main;$ClassName->show_headings(); ?>
We have the main page from there people have a link allowing to post a topic since we don't have a topic we have to post the first
//[code]//CreateATopic.php<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">Subject <input type="text" name="heading" />Author <input type="text" name="author" />Post <textarea cols="100" rows="100" name="post"></textarea><input type="submit" /></form><?phpinclude_once 'YourClassName.class.php'; include_once 'YourClassName_Main.class.php'; $db=new YourClassName(); $db->connect(); $ClassName=new YourClassName(); $ClassName->heading=$_POST['heading']; $ClassName->author=$_POST['author']; $ClassName->post=$_POST['post']; $ClassName->New_Topic(); ?><br /><a href="index.php">Go to start</a><br />

//Post ‌into topic .php<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">Author <input type="text" name="author" />Post <textarea cols="100" rows="100" name="post"></textarea><input type="submit" /></form><?phpinclude_once 'YourClassName.class.php'; include_once 'YourClassName_Main.class.php'; $db=new YourClassName(); $db->connect(); $ClassName=new YourClassName(); $ClassName->author=$_POST['autor']; $ClassName->post=$_POST['post']; $ClassName->set_a_cookie();$ClassName->post_message(); $ClassName->show_messages(); ?><br /><a href="index.php">Go to start</a><br />

Well, That was it.
If i've maded some part unclear please post a message so i can fix it.
I hope you enjoy it.

Share this post


Link to post
Share on other sites

Hi thereTo make a post board first we have to design and implement the database in this case we use mysql.
1. I decided to have 1 database called post board so enter in your favourite mysql tool and create one database:

create database your_database;
then i'll use 2 mysql tables : One for topics or "headings" if you like and on for the actual posts.
create table headings(id int not null primary key auto_increment, heading varchar(100) not null, author varchar(30) not null, date  datetime not null);
The second table - posts will have relation key transfering data from table headings to table posts:
 create table posts(id int not null primary key auto_increment, post varchar(2000) not null, author varch(30) not null, date datetime not null,headings int not null, foreign key(headings) references headings.id); [\code] Now we start with the php part. first we need to create a connection to mysql, OOP has a good painless way of doing this [code] //Your_filename, Save it as YourClassName.class.php class YourClassName{ <?php function connect(){   $user="yourMYSQLusername";    $password="yourMYSQLpassword";   $host="yourWebServiceHost"//Usually localhost   mysql_connect($host,$username,$password);   } } ?>

//filename_utils <?php //Your_filename1 Save it as YourClass1Name_Main.class.php class YourClass1Name_Main   public $heading;   public $author;    public $post;    function New_Topic(){   $query="Insert into headings (heading,author,date) values('$this->heading','$this->author',now());";   mysql_query($query)or die(mysql_error());   $query="Insert into post(post,author,date,heading)values('$this->post','$this->author',now(),LAST_INSERT_ID());";   mysq_query($query)or die(mysql_error());   }   function show_headings(){	 $query="Select * from headings;";	 $result=mysql_query($query);	 while($row=mysql_fetch_assoc($result)){ echo "{$row->heading}\n Posted by {$row->author}\n On {$row->date}"; ?> <a href="post_into_topic.php?id=<?php echo $row->id;?>">Join</a><br /> <?php	 }   }   function set_a_cookie(){	ob_start();	 $id=$_GET['id'];	 setrawcookie("id",$id,time()+3600);	 ob_get_contents();	 ob_clean();   }   function post_message(){	$query="Insert into posts(post,author,date,headings)values('$this->post','$this->author',now()),'$_COOKIE['id'];";	mysql_query($query)or die(mysql_error());   }   function show_messages(){   $query="Select * from headings,posts where headings.id='$_COOKIE['id']';";   $result=mysql_query($query);   while($row=mysql_fetch_assoc($result)){  echo "{$row->heading}\n Author {$row->author}\n Date {$row->date}<br />";  echo "Answer: {$row->post}\n Author{$row->author}\n Date{$row->date}<br />;   } } ?>
Now we need two files
//CreateATopic.php <form action="<?=$_SERVER['PHP_SELF'];?>" method="post"> Subject <input type="text" name="heading" /> Author <input type="text" name="author" /> Post <textarea cols="100" rows="100"></textarea> <input type="submit" /> </form> <?php ?> //Post_into_topic.php <form action="<?php echo "{$_SERVER['PHP_SELF']}?id={$_COOKIE['id']}";?>" method="post"> Author <input type="text" name="author" /> Post <textarea cols="100" row="100"></textarea> <input type="submit" /> </form>
Now we have to make one more file Index.php who will be the main page
 //index.php <?php include_once 'YourClassName';  $db=new DB();  $db->connect();  include_once 'YourClassName_Main';  $ClassName=new YourClassName_Main; $ClassName->show_headings();  ?>
We have the main page from there people have a link allowing to post a topic since we don't have a topic we have to post the first
 // [code] //CreateATopic.php <form action="<?=$_SERVER['PHP_SELF'];?>" method="post"> Subject <input type="text" name="heading" /> Author <input type="text" name="author" /> Post <textarea cols="100" rows="100" name="post"></textarea> <input type="submit" /> </form> <?php include_once 'YourClassName.class.php';  include_once 'YourClassName_Main.class.php';  $db=new YourClassName();  $db->connect();  $ClassName=new YourClassName();  $ClassName->heading=$_POST['heading'];  $ClassName->author=$_POST['author'];  $ClassName->post=$_POST['post'];  $ClassName->New_Topic();  ?> <br /><a href="index.php">Go to start</a><br />

//Post ‌into topic .php <form action="<?=$_SERVER['PHP_SELF'];?>" method="post"> Author <input type="text" name="author" /> Post <textarea cols="100" rows="100" name="post"></textarea> <input type="submit" /> </form> <?php include_once 'YourClassName.class.php';  include_once 'YourClassName_Main.class.php';  $db=new YourClassName();  $db->connect();  $ClassName=new YourClassName();  $ClassName->author=$_POST['autor'];  $ClassName->post=$_POST['post'];  $ClassName->set_a_cookie(); $ClassName->post_message();  $ClassName->show_messages();  ?> <br /><a href="index.php">Go to start</a><br />

Well, That was it.
If i've maded some part unclear please post a message so i can fix it.
I hope you enjoy it.
thanks for this nice tutorial
it really help a lot

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.