Quick Search:
CODE
Oracle PL/SQL Code Library
JOBS
Find Or Post Oracle Jobs
FORUM
Oracle Discussion & Chat
 PHP Code: Forum skeleton code Jump to:  
Category: >> PHP Code >> Forum skeleton code Bookmark and Share

<< lastnext >>

Snippet Name: Forum skeleton code

Description: Very basic skeleton code for a forum.

Installation:
filename.php?type=setup

Comment: (none)

Language: PHP, MYSQL
Highlight Mode: PHP
Last Modified: March 01st, 2009

 
 
     $host="localhost"; // Host name
     $username="root"; // Mysql username
     $password="pass"; // Mysql password
     $db_name="test"; // Database name
     $tbl_name="forum_topics"; // Table name -- LEAVE THIS AS IT IS
 
     MYSQL_CONNECT($host, "$username", "$password")or DIE("cannot connect");
     MYSQL_SELECT_DB($db_name)or DIE("cannot select DB");
     IF (ISSET($_GET['type']) && $_GET['type'] == "setup") {
 
          $sql = "CREATE TABLE `forum_topics` (
          `id` int(4) NOT NULL auto_increment,
          `topic` varchar(255) NOT NULL default '',
          `detail` longtext NOT NULL,
          `name` varchar(65) NOT NULL default '',
          `email` varchar(65) NOT NULL default '',
          `datetime` varchar(25) NOT NULL default '',
          `view` int(4) NOT NULL default '0',
          `reply` int(4) NOT NULL default '0',
          PRIMARY KEY (`id`)
          ) TYPE=MyISAM AUTO_INCREMENT=1 ;";
          $sql2 = "CREATE TABLE `forum_replies` (
          `thread_id` int(4) NOT NULL default '0',
          `a_id` int(4) NOT NULL default '0',
          `a_name` varchar(65) NOT NULL default '',
          `a_email` varchar(65) NOT NULL default '',
          `a_answer` longtext NOT NULL,
          `a_datetime` varchar(25) NOT NULL default '',
          KEY `a_id` (`a_id`)
          ) TYPE=MyISAM;";
          MYSQL_QUERY($sql);
          MYSQL_QUERY($sql2);
     }ELSEIF (ISSET($_GET['type']) && $_GET['type'] == "create") {
          ECHO "<table width=\"400\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"1\" bgcolor=\"#CCCCCC\"><tr><form id=\"form1\" name=\"form1\" method=\"post\" action=\"".$PHP_SELF."?type=post\"><td><table width=\"100%\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" bgcolor=\"#FFFFFF\"><tr><td colspan=\"3\" bgcolor=\"#E6E6E6\"><strong>Create New Topic</strong> </td></tr><tr><td width=\"14%\"><strong>Topic</strong></td><td width=\"2%\">:</td><td width=\"84%\"><input name=\"topic\" type=\"text\" id=\"topic\" size=\"50\" /></td></tr><tr><td valign=\"top\"><strong>Detail</strong></td><td valign=\"top\">:</td><td><textarea name=\"detail\" cols=\"50\" rows=\"3\" id=\"detail\"></textarea></td></tr><tr><td><strong>Name</strong></td><td>:</td><td><input name=\"name\" type=\"text\" id=\"name\" size=\"50\" /></td></tr><tr><td><strong>Email</strong></td><td>:</td><td><input name=\"email\" type=\"text\" id=\"email\" size=\"50\" /></td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td><input type=\"submit\" name=\"Submit\" value=\"Submit\" /> <input type=\"reset\" name=\"Submit2\" value=\"Reset\" /></td></tr></table></td></form></tr></table>";
     }ELSEIF (ISSET($_GET['type']) && $_GET['type'] == "post") {
          $topic=$_POST['topic'];
          $detail=$_POST['detail'];
          $name=$_POST['name'];
          $email=$_POST['email'];
          $datetime=DATE("d/m/y h:i:s"); //create date time
          $sql="INSERT INTO $tbl_name(topic, detail, name, email, datetime)VALUES('$topic', '$detail', '$name', '$email', '$datetime')";
          $result=MYSQL_QUERY($sql);
          IF($result){
               ECHO "Successful<BR>";
               ECHO "<a href=\"".$PHP_SELF."\">View your topic</a>";
          }ELSE {
               ECHO "Error Posting.";
          }
     }ELSEIF (!ISSET($_GET['type'])) {
          $sql="SELECT * FROM $tbl_name ORDER BY id DESC";
          // OREDER BY id DESC is order result by descending
          $result=MYSQL_QUERY($sql);
          ECHO "<table width=\"90%\" border=\"0\" align=\"center\" cellpadding=\"3\" cellspacing=\"1\" bgcolor=\"#CCCCCC\"><tr><td width=\"6%\" align=\"center\" bgcolor=\"#E6E6E6\"><strong>#</strong></td><td width=\"53%\" align=\"center\" bgcolor=\"#E6E6E6\"><strong>Topic</strong></td><td width=\"15%\" align=\"center\" bgcolor=\"#E6E6E6\"><strong>Views</strong></td><td width=\"13%\" align=\"center\" bgcolor=\"#E6E6E6\"><strong>Replies</strong></td><td width=\"13%\" align=\"center\" bgcolor=\"#E6E6E6\"><strong>Date/Time</strong></td></tr>";
          WHILE($rows=MYSQL_FETCH_ARRAY($result)){ // Start looping table row 
               ECHO "<tr><td bgcolor=\"#FFFFFF\">".$rows['id']."</td><td bgcolor=\"#FFFFFF\"><a href=\"".$PHP_SELF."?type=view&id=".$rows['id']."\">".$rows['topic']."</a><BR></td><td align=\"center\" bgcolor=\"#FFFFFF\">".$rows['view']."</td><td align=\"center\" bgcolor=\"#FFFFFF\">".$rows['reply']."</td><td align=\"center\" bgcolor=\"#FFFFFF\">".$rows['datetime']."</td></tr>";
          }
          ECHO "<tr><td colspan=\"5\" align=\"right\" bgcolor=\"#E6E6E6\"><a href=\"".$PHP_SELF."?type=create\"><strong>Create New Topic</strong> </a></td></tr></table>";
     }ELSEIF (ISSET($_GET['type']) && $_GET['type'] == 'view') {
          // get value of id that sent from address bar
          $id=$_GET['id'];
          $sql="SELECT * FROM $tbl_name WHERE id='$id'";
          $result=MYSQL_QUERY($sql);
          $rows=MYSQL_FETCH_ARRAY($result);
 
          ECHO "<table width=\"400\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"1\" bgcolor=\"#CCCCCC\"><tr><td><table width=\"100%\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" bordercolor=\"1\" bgcolor=\"#FFFFFF\"><tr><td bgcolor=\"#F8F7F1\"><strong>".$rows['topic']."></strong></td></tr><tr><td bgcolor=\"#F8F7F1\">".$rows['detail']."</td></tr><tr><td bgcolor=\"#F8F7F1\"><strong>By :</strong> ".$rows['name']." <strong>Email : </strong>".$rows['email']."</td></tr><tr><td bgcolor=\"#F8F7F1\"><strong>Date/time : </strong>".$rows['datetime']."</td></tr></table></td></tr></table><BR>";
          $tbl_name2="forum_replies"; // Switch to replies table
          $sql2="SELECT * FROM $tbl_name2 WHERE thread_id='$id'";
          $result2=MYSQL_QUERY($sql2);
          WHILE($rows=MYSQL_FETCH_ARRAY($result2)){
               ECHO "<table width=\"400\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"1\" bgcolor=\"#CCCCCC\"><tr><td><table width=\"100%\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" bgcolor=\"#FFFFFF\"><tr><td bgcolor=\"#F8F7F1\"><strong>ID</strong></td><td bgcolor=\"#F8F7F1\">:</td><td bgcolor=\"#F8F7F1\">".$rows['a_id']."</td></tr><tr><td width=\"18%\" bgcolor=\"#F8F7F1\"><strong>Name</strong></td><td width=\"5%\" bgcolor=\"#F8F7F1\">:</td><td width=\"77%\" bgcolor=\"#F8F7F1\">".$rows['a_name']."</td></tr><tr><td bgcolor=\"#F8F7F1\"><strong>Email</strong></td><td bgcolor=\"#F8F7F1\">:</td><td bgcolor=\"#F8F7F1\">".$rows['a_email']."</td></tr><tr><td bgcolor=\"#F8F7F1\"><strong>Answer</strong></td><td bgcolor=\"#F8F7F1\">:</td><td bgcolor=\"#F8F7F1\">".$rows['a_answer']."</td></tr><tr><td bgcolor=\"#F8F7F1\"><strong>Date/Time</strong></td><td bgcolor=\"#F8F7F1\">:</td><td bgcolor=\"#F8F7F1\">".$rows['a_datetime']."</td></tr></table></td></tr></table><br>";
          }
          $sql3="SELECT view FROM $tbl_name WHERE id='$id'";
          $result3=MYSQL_QUERY($sql3);
          $rows=MYSQL_FETCH_ARRAY($result3);
          $view=$rows['view'];
          // if have no counter value set counter = 1
          IF(EMPTY($view)){
               $view=1;
               $sql4="INSERT INTO $tbl_name(view) VALUES('$view') WHERE id='$id'";
               $result4=MYSQL_QUERY($sql4);
          }
          // count more value
          $addview=$view+1;
          $sql5="update $tbl_name set view='$addview' WHERE id='$id'";
          $result5=MYSQL_QUERY($sql5);
 
          ECHO "<BR><table width=\"400\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"1\" bgcolor=\"#CCCCCC\"><tr><form name=\"form1\" method=\"post\" action=\"".$PHP_SELF."?type=reply\"><td><table width=\"100%\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" bgcolor=\"#FFFFFF\"><tr><td width=\"18%\"><strong>Name</strong></td><td width=\"3%\">:</td><td width=\"79%\"><input name=\"a_name\" type=\"text\" id=\"a_name\" size=\"45\"></td></tr><tr><td><strong>Email</strong></td><td>:</td><td><input name=\"a_email\" type=\"text\" id=\"a_email\" size=\"45\"></td></tr><tr><td valign=\"top\"><strong>Answer</strong></td><td valign=\"top\">:</td><td><textarea name=\"a_answer\" cols=\"45\" rows=\"3\" id=\"a_answer\"></textarea></td></tr><tr><td>&nbsp;</td><td><input name=\"id\" type=\"hidden\" value=\"".$id."\"></td><td><input type=\"submit\" name=\"Submit\" value=\"Submit\"> <input type=\"reset\" name=\"Submit2\" value=\"Reset\"></td></tr></table></td></form></tr></table>";
     }ELSEIF (ISSET($_GET['type']) && $_GET['type'] == "reply") {
          // Get value of id that sent from hidden field
          $id=$_POST['id'];
          // Find highest answer number.
          $sql="SELECT MAX(a_id) AS Maxa_id FROM $tbl_name WHERE thread_id='$id'";
          $result=MYSQL_QUERY($sql);
          $rows=MYSQL_FETCH_ARRAY($result);
          // add + 1 to highest answer number and keep it in variable name "$Max_id". if there no answer yet set it = 1
          IF ($rows) {
               $Max_id = $rows['Maxa_id']+1;
          }ELSE {
               $Max_id = 1;
          }
          // get values that sent from form
          $a_name=$_POST['a_name'];
          $a_email=$_POST['a_email'];
          $a_answer=$_POST['a_answer'];
          $datetime=DATE("d/m/y H:i:s"); // create date and time
          // Insert answer
          $sql2="INSERT INTO $tbl_name(thread_id, a_id, a_name, a_email, a_answer, a_datetime)VALUES('$id', '$Max_id', '$a_name', '$a_email', '$a_answer', '$datetime')";
          $result2=MYSQL_QUERY($sql2);
          IF($result2){
               ECHO "Successful<BR>";
               ECHO "<a href='".$PHP_SELF."?type=view&id=".$id."'>View your answer</a>";
               // If added new answer, add value +1 in reply column
               $tbl_name2="forum_question";
               $sql3="UPDATE $tbl_name2 SET reply='$Max_id' WHERE id='$id'";
               $result3=MYSQL_QUERY($sql3);
          }
          ELSE {
               ECHO "ERROR";
          }
     }
 


Free
Oracle Magazine
Subscriptions
and Oracle White Papers


SQL University.net courses meet the most demanding needs of the business world for advanced education in a cost-effective manner. SQL University.net courses are available immediately for IT professionals and can be taken without disruption of your workplace schedule or processes.

Compared to traditional travel-based training, SQL University.net saves time and valuable corporate resources, allowing companies to do more with less. That's our mission, and that's what we deliver.

Click here to find out more
 
Home      :      Code Library      :      Sponsors      :      Privacy      :      Terms of Use      :      Contact Us 309 users online    © 2009 psoug.org

PSOUG LOGIN
Username: 
Password: 
Forgot your password?