SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Following online tutorial-Need Help

    Hello everyone,

    I am very new to PHP and MySQL. I am currently following a tutorial and having couples of errors but I manage to solve most of them.
    But there is one last thing to be completed. The tutorial is on a 'Simple Forum Board'. Actually, I do not know how to describe this problem technically and precisely but I can explain by an example.

    Whenever a 'New Topic' is created, the author name, time and date of the post will be displayed. Let say the author posting time is March 8, 2012, 10.20am. When another person reply to the topic at let say March 9, 2012, 4.50pm, the author posting time will change to March 9, 2012, 4.50pm as well. Meaning to say, the author posting time will change and same as the posting time of the last poster. I have put my best effort and did not find any solution yet.

    Here's the code
    Index code
    PHP 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%>Discussion Topic</td><td width=20%>Author</td><td>Replies</td><td>Last replied time</td></tr>";

    $getthreads="SELECT * from forum where parentid='0' order by postid 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>";



    ?>  

    <a href="forum.php?"><img src="image/button_back1.gif" border="0" align="right"/></a>

    Post code
    PHP 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

       {
           
    date_default_timezone_set('Asia/Brunei');
           
    $thedate=date("U"); //get unix timestamp
        
          
    $displaytime=date("F j, Y, h:i a");

          
    //we now strip HTML injections

          
    $subject=strip_tags($subject);

          
    $name=strip_tags($name);

          
    $yourpost=strip_tags($yourpost); 

          
    $insertpost="INSERT INTO elec_forum(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='elec_forum.php'>Forum</a>.";

       }



    }

    else

    {

       print 
    "<form action='elec_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>";

    ?>
    Reply code
    PHP 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>Reply</td></tr>";

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

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

    {

       
    $name=$_POST['name'];

       
    $yourpost=$_POST['yourpost'];

       
    $subject=(isset($_POST['subject']))?$_POST['subject']:null;;

       
       
    $id=$_POST['id'];

       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

       {
          
    date_default_timezone_set('Asia/Brunei');
          
    $thedate=date("U"); //get unix timestamp

          
    $displaytime1=date("F j, Y, h:i a");

          
    //we now strip HTML injections

          
    $subject=strip_tags($subject);

          
    $name=strip_tags($name);

          
    $yourpost=strip_tags($yourpost); 

          
    $insertpost="INSERT INTO elec_forum(author,title,post,showtime,realtime,lastposter, parentid) values('$name','$subject','$yourpost','$displaytime1','$thedate','$name','$id')";

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

          
    $updatepost="Update elec_forum set numreplies=numreplies+'1', lastposter='$name', showtime='$displaytime1',lastrepliedto='$thedate' where postid='$id'";

          
    mysql_query($updatepost) or die("Could not update post");
          print 
    "Message updated, go back to <A href='elec_message.php?id=$id'>Message</a>.";

       }



    }

    else

    {

       
    $id=$_GET['id'];

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

       print 
    "<input type='hidden' name='id' value='$id'>";

       print 
    "Your name:<br>";

       print 
    "<input type='text' name='name' 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>";

    ?>
    Message code
    PHP Code:
    <?php 

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

    $id=(isset($_GET['id']))?$_GET['id']:null;;


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

    print 
    "<A href='elec_forum.php'>Back to main forum</a>-<A href='post.php'>New Topic</a>-<A href='elec_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 forum 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 valign='top'>Last replied to $gettopic3[showtime]<br><hr>";

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

    $message=nl2br($message);

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

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

    $getreplies="Select * from forum 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 valign='top'>Last replied to $getreplies3[showtime]<br><hr>";

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

       
    $message=nl2br($message);

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

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

    }

    print 
    "</table>";



    ?>
    Connect code
    PHP Code:
    <?php



    $db 
    mysql_connect("localhost""MentorMenteeData""mentormenteedata") or die("Could not connect.");

    if(!
    $db

        die(
    "no db");

    if(!
    mysql_select_db("mentormenteesystem",$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);

    }



    ?>
    Database Table:

    PHP Code:
    -- phpMyAdmin SQL Dump
    -- version 3.4.5
    -- [url]http://www.phpmyadmin.net[/url]
    --
    -- 
    Hostlocalhost
    -- Generation TimeMar 092012 at 10:05 AM
    -- Server version5.5.16
    -- PHP Version5.3.8


    SET SQL_MODE
    ="NO_AUTO_VALUE_ON_ZERO";
    SET time_zone "+00:00";

    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    /*!40101 SET NAMES utf8 */;


    --
    -- 
    Database: `mentormenteesystem`
    --

    -- --------------------------------------------------------

    --
    -- 
    Table structure for table `forum`
    --

    CREATE TABLE IF NOT EXISTS `elec_forum` (
      `
    postidbigint(20NOT NULL AUTO_INCREMENT,
      `
    authorvarchar(255NOT NULL DEFAULT '',
      `
    titlevarchar(255NOT NULL DEFAULT ''
      `
    postmediumtext NOT NULL,
      `
    showtimevarchar(255NOT NULL DEFAULT '',  
     `
    realtimebigint(20NOT NULL DEFAULT '0',  
     `
    lastpostervarchar(255NOT NULL DEFAULT '',  
     `
    numrepliesbigint(20NOT NULL DEFAULT '0',  
     `
    parentidbigint(20NOT NULL DEFAULT '0',  
     `
    lastrepliedtobigint(20NOT NULL DEFAULT '0'
    PRIMARY KEY (`postid`)

    ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=

    Please guide and advise. I really do need a help here. thanks in advance

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Code:
    $updatepost="Update elec_forum set numreplies=numreplies+'1', lastposter='$name', showtime='$displaytime1',lastrepliedto='$thedate' where postid='$id'";
    Here you change the showtime of the original post (the part in red) when a reply is posted. If you don't want that, don't update the value of showtime.

  3. #3
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Code:
     showtime='$displaytime1'
    Opps, sorry. It should be
    PHP Code:
    showtime='$displaytime' 

    Quote Originally Posted by guido2004 View Post
    If you don't want that, don't update the value of showtime.
    I do need to update the showtime, because I need the 'Date and Time' of the last poster (last person who reply to the thread) to be shown/updated in the index page (Page before entering the topic). But I just do not know why it updated the author 'date and time' as well.

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by SyntaxError View Post
    I do need to update the showtime, because I need the 'Date and Time' of the last poster (last person who reply to the thread) to be shown/updated in the index page (Page before entering the topic). But I just do not know why it updated the author 'date and time' as well.
    And what column in your table would be containing the authore date and time?

  5. #5
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    And what column in your table would be containing the authore date and time?
    I did not specified column for the author. In the above post, I have stated the database table I am using.
    Do I really need to have the separate column for the author and the other poster?

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by SyntaxError
    But I just do not know why it updated the author 'date and time' as well.
    Quote Originally Posted by SyntaxError View Post
    I did not specified column for the author.
    Ehm, if there is no column for the author date and time, then how can your script update that column?
    Quote Originally Posted by SyntaxError View Post
    Do I really need to have the separate column for the author and the other poster?
    Not for me

  7. #7
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Well, that's the problem, isn't it. If you use 1 column for 2 different things, and it can contain only 1. So I guess you'll have to add a 'last modified date' column.

  8. #8
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Location
    Kuching, Sarawak
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Well, that's the problem, isn't it. If you use 1 column for 2 different things, and it can contain only 1. So I guess you'll have to add a 'last modified date' column.
    Thanks guido, I will try it and get back here if I still cant work it well.

  9. #9
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Location
    Kuching, Sarawak
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Guido,

    I just realize I have the $getthreads[showtime] in the 'Index code'. That variable is to display the posting time for the author post and the last poster(if there is reply). I will added 'last modified date' column into the database table and then use the 'if else' statement to select the column.

    The 'if else' statement I plan to put in the 'Index Code' with condition of

    if (numreplies==0)
    showtime='$displaytime'
    else
    lastmodifieddate='$displaytime'

    I am not sure, whether this is possible or not. Please correct me if I am wrong conceptually and technically.Thank you


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •