Comments in PHP. i have tried to have a quick glance on how and what i have to do to create comments on my website. Seems a bit complicated, but not impossible. But i have problems even with installing PHP on teh website, let alone creating comments.

Q.: what's the first thing i have to start with to get going with the whole process (well, besided downloading PHP and MySql and making sure my server supports them)?

    Err, I presume you mean dynamically posting content, when you say "comments"?

    If you dont know how to code with PHP, that probably is a reasonable goal.

    But you'll need to look around for tutorials to do even simpler things, and read the PHP Manual at php.net

      Yeah, tutorials are the way to go!... But just to give a basic overview:

      1. Create html form [http://www.w3schools.com/html/tryit.asp?filename=tryhtml_form_submit].

      2. Create a php page that has the same name as the file listed in the action part of the form you just created.

      3. If your form used 'post' as its method then $comment = $POST['comment'] or if you used 'get' then $comment = $GET['comment']... Where comment is the name of the text field that the user entered the comment into.

      4. Make a connection to the database, assuming you've already set it up.

      5. Insert the variable comment into the database, along with any other information such as a timestamp.

      6. To view the comment... Make a connection to the database, execute a query you want... Iterate through the resultset and echo the comment field from each row to the screen.

        Ok, tnks

        So basically besides some (many 🙂 )tutorials on PHP i should have a look at databases tutorials, like on MySqL as well.

          Yup; you need to store the comments somewhere!

          Another way would be to append each comment to a text file on the server, then print out the contents of the file with the correct formatting around it... Might be a little easier to set up, though the database solution is much neater.

            If i do it in teh easier way, will the people posting comments be able to see them afterwards (together with teh rest of the comments)?

            (although i am thinking of making it the hard but neater way)

              Yeah, it would work.

              Basically you'd just have a text file of comment on the server but NOT in the public_html folder - that way the server can still read the contents but no one can view the page of comments by itself.

              To add to the page just append the comment to the end of the file, separating files by tabs (\t) or something, and putting each new entry on a separate line.

              To extract the data just assign the contents of the text file to a variable and use explode (keyword) to get the person's name / email address and the comment for each new line.

              Obviously if you want to let people edit their comments (i.e. if they make a mistake), etc. then doing it this way would be recommended even less!

                Go the DB way!. It makes life alot easier in the long run and its good practice for those future projects.

                Mark.

                  Write a Reply...