simple discussion forum part 2 - tutorial

Status
Not open for further replies.

Baljeet

Member
Jan 31, 2011
76
0
hi every body in this post i will illustrate how to build a simple discussion forum and making a page for each registered user(illustrated in the part 3 -soon-) having a welcome message and the photo he uploaded while the registration.

first i will say the sequence of the user access:
1.when the user enter the site he will be able to view the sections of the forum (previously added by the database admin ) in a table containing the section name and the section description .
2.the user will choose one of the sections to view the topics in it
3.then he will choose one of the topics ,the posts reated to it will appear.
4.he will have a button add new post.
5.when he click add new post he will be asked to login
note : if he was already logged in he will redirected to the add post page.
6.in the app post page : he will be asked to choose the topic to write about,then he will write the post title ,the post content and click post
7.when he clicks post it will be added to the database ,then the user will be redirected to the view post page having hi post added.
note: on the index page there will be a button saying "register anew user"
with a page for registration . i will illustrate it soon in this thread.

first the index page :
PHP:
<?php
  //  printing th registrer button
  echo  "<a href='register.php'><input type=button value='register new user'><a>";
  // connecting to the DB
  $conn=mysql_connect("localhost","root",null,null);
  mysql_selectdb("forum",$conn);
  $sql="select * from dbo_dep";
  $result=mysql_query($sql,$conn) or die(mysql_error());
  // this is the part of printing the table containing the sections of the forum
  echo "<table border=\"1\">
  <tr>
  <th>section name</th>
  <th>description  </th>
  </tr>";
  //puting the content of the table of  the db in the table with a link
  //to the vuew topiv page having a query string to indicate the selected section
  while ($row =mysql_fetch_array($result)) {
                  $desc = "$row[depdescription]" ; 
                  echo "<tr>
       <td> <a href='viewtopic.php?topic=\"$desc\"'> $row[depdescription] </a> </td>
                   <td> $row[depname]</td>
                   </tr>";
                  
  };
secondly the viewtopic.php page:
PHP:
  <?php
  //conecting to the mysql and choosing the DB
  $conn=mysql_connect("localhost","root",null,null);
  mysql_selectdb("forum",$conn);
  //this select statement joins between two tables(sections and the posts)using the where clause and
  //the where clause receives  topic by the ". $_REQUEST['topic'] expresson
  $sql="SELECT d.depdescription ,t.topicname ,t.topicid ,t.topicdescription ,u.user_name  FROM forum.topic t , forum.dbo_users u , forum.dbo_dep d where  d.depid = t.depid and  depdescription =". $_REQUEST['topic']." and t.userid = u.user_id ;  ";
   
  $result=mysql_query($sql,$conn) or die(mysql_error());
  echo $result;
  //for making sure that the query was accomplished
  //the same part as in the index page
  echo "<table border='1'>
  <tr>
  <th>topic name</th>
  <th>description  </th>
  </tr>";
  while ($row =mysql_fetch_array($result)) {
      $desc = $row['topicid'];
                  echo "<tr>
                  
                   <td> <a rel='nofollow' href='viewpost.php?post=$desc'> $row[topicname] </a> </td> ;
                   <td> $row[topicdescription]</td>
                   </tr>";
                  
  };
  ?>
then the viewpost.php page:
PHP:
<?php
  $conn=mysql_connect("localhost","root",null,null);
  $desc=$_REQUEST['post'];
  mysql_selectdb('forum',$conn);
  $sql= "select t.topicname, t.topicid ,p.post_title,p.post_content , p.postid , u.user_name ,u.user_id from forum.topic t , forum.dbo_users u , forum.posts p
  where t.topicID = p.topic_id and t.topicid = " .$desc . " and  p.user_id = u.user_id  order by postid desc;";
   
  $result=mysql_query($sql,$conn) or die(mysql_error());
  echo $result;
  echo "<table border=\"1\">
  <tr>
  <th>section name</th>
  <th>description  </th>
  </tr>";
  while ($row =mysql_fetch_array($result)) {
                  
                  echo "<tr>
       <td> <a href='#'> $row[post_title] </a> </td>
                   <td> $row[post_content]</td>
                   <td> $row[user_name]</td>
                   </tr>";
                  
  };
  ?>
the login button redirects the user to the login page:
__
Code:
<!--html part of the view post page  for puting the 'add new post' button-->
 
  <html >
  <head>
   
   
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Untitled Document</title>
  </head>
   
  <body>
  <a href="login.php"><input name="" type="button" value="add new post" />
  </a>
  </body>
  </html>
__ the login page will check if the user is already logged in the user is logged it will automatically redirect him to the 'add post' page
the php part will be like this:
PHP:
<?php 
  session_start();
  //in another page (loginphp.php) will verify the user and make him session with his username 
  //this page will if the session is not null and if so it will redirect him  to the add post //page(post.php)
  if ($_SESSION['username']==!null)
  {header( 'Location: post.php' );}
the login form will oh like this:
Code:
<!--the login form -->
  <form action="loginphp.php" method="post" target='post.php'>
  <label> :</label>
  <br />
  <label></label>
  <label></label>
  <table width="365" border="0">
    <tr>
      <td width="103">username</td>
      <td width="103"><input name="userlogin" type="text" /></td>
      <td width="145">&nbsp;</td>
    </tr>
    <tr>
      <td>password:</td>
      <td><label>
        <input name="passlog" type="text" />
      </label></td>
      <td><input name="" type="submit" value="login" /></td>
    </tr>
  </table>
  </form>
  </body>
  </html>
the add post page will then open if user is verified :
will look like this:
PHP:
<?php
  //session start
   
  session_start();
  //this block checks if the user is verified in the loginphp.php if not it will redirect him to 
  // the login page
  if ($_SESSION['username']==null)
  {
                  
                  header( 'Location: login.php' );
  }
  else {echo "welcome : ".$_SESSION['username'];
  //connectig to the mysql
  $conn=mysql_connect("localhost","root",null,null);
  mysql_selectdb("forum",$conn);
  //the select statement will retreive th topics for the user to choose one
  $sql="SELECT d.depdescription ,t.topicname ,t.topicid ,t.topicdescription ,u.user_name  FROM forum.topic t , forum.dbo_users u , forum.dbo_dep d where  d.depid = t.depid  and t.userid = u.user_id ;  ";
   
  $result=mysql_query($sql,$conn) or die(mysql_error());
   
  }
  //note; the post will be added by a page called post action.php
  echo "<form action='postaction.php' method='post'>
    <p><br>" ;
    
    while ($row =mysql_fetch_array($result)) {
    $topicid= $row['topicid'];
    $topicname=$row['topicname'];
  $_SESSION['x']=$topicname;
  //this block will print radiolist containing the topics 
    echo "<input type='radio' name= \"main\" id=\"$topicid\"  value=\" $topicid\"  />".$topicname. "<br>";
     }
   
   echo  "title". " <input name='posttitle' type='text' size='50' />
    </p>
    <p>
      write here your content:<br>
      &nbsp;&nbsp;<textarea  name='area' cols='100' rows='10'></textarea>
      <input name='' type='submit' value=post />
    </p>
  </form>";
   
  ?>
the logout button in the same page:
Code:
<!--logout button takes action from logout.php -->
  <form action="logout.php" method="post">
  <input name="logout" type="submit" value="logout" />
  <p>
    <label></label>
    <br />
  </p>
  </form>
thh postaction.php will look like this:
PHP:
<?php session_start();$conn=mysql_connect("localhost","root",null,null);
  mysql_selectdb("forum",$conn);
   
  $topicid =$_POST['main'];
  $content = "'".$_POST['area']."'";
  $userid = $_SESSION['id'];
  $title ="'".$_POST['posttitle']."'";
  $id =$_POST['main']."'" ;
  $idi =$_POST['main'];
  //the insert statement will be like this
  $sql="insert into forum.posts (topic_id ,post_content ,user_id,post_title) values 
  ($topicid, $content,$userid,$title);";
   
  echo "<a href='viewpost.php?post=$id > <input type='button' value='view' /> </a>" ; 
  $result =mysql_query($sql,$conn);
  echo $result;
  //redirecting  to  the viewpost.php  page to view the post he added
  header ("location: viewpost.php?post=".$idi ."&type=no '");
  ?>
the logout page unregister and redirect the user to the index.php :
PHP:
<?php 
  session_start();
  session_unregister('username');
  session_unregister('password');
  session_destroy();
  header( 'Location: index.php' );
  ?>
the loginphp.php page (sorry for bad page naming):
PHP:
<?php
  session_start();
  $conn=mysql_connect("localhost","root",null,null);
  mysql_selectdb("forum",$conn);
  //the select statement will select username password from the db and the where clause 
  //will  make condition that the username= the posted username and the password = the posted password :
  $sql = "select * from dbo_users where user_name ='$_POST[userlogin]' AND password ='$_POST[passlog]'"; 
      
   
  $result=mysql_query($sql,$conn) or die(mysql_error());
  echo $result;
   
  echo mysql_num_rows($result);
  //in condition of having one row or more (user is varified)
  if (mysql_num_rows($result) >= 1)
  {
   
  $row =mysql_fetch_array($result);
   
  $username =$row['user_name'];
                  $pass =$row['password'];
                  $_POST['userlogin']=$username;
  $_POST['passlog']=$pass;             
  $_SESSION['username']=$_POST['userlogin'];
  $_SESSION['password']=$_POST['passlog'];
  $_SESSION['id']=$row['user_id'];
  header( 'Location: post.php' );
   
                  }
                  
                  else   echo $_POST['userlogin'];
                  //if user not verified
                  header( 'Location: login.php' );
                    
                   
   
  ?>

All Credits goes to one who really made this...
 
Status
Not open for further replies.

Users who are viewing this thread

☀️  Switch to Light Theme

Latest posts

Top