I'm building a website (php and mysql) with different content types (pictures, posts, questions). Each require a comment section. I wouldn't want to repeat myself in each of this pages. To make it easier to maintain I don't know if I should make a comment function or a comment.php file that I'll include anywhere I want but the problem will be that the output will be highly variable. I'll greatly appreciate any help. I'm kind of new to this stuff
Asked
Active
Viewed 15 times
0
-
create a separate file for comment section and include where require. – Amit Rajput May 12 '17 at 04:43
-
Possible duplicate of [Difference between require, include and require\_once?](http://stackoverflow.com/questions/2418473/difference-between-require-include-and-require-once) – Andrea Carraro May 12 '17 at 09:47
-
I think I'll go with Amit Rajput's answer but how do i get it to load comments specific to a particular content? since I'll be including the comment file, – Frank May 12 '17 at 10:57
1 Answers
0
First of all i need to know how your db is structured, lets assume you have table call comments with id, comment_title,content, post_id(which post it's commented on)
Note: if you need to comment for different section which stored in different tables this won't work properly. Your id's has to be unique. For example if you used 1 as id in post table you can't use it in the question table as primary key.
Another way create different comment table for different section, which is a repetition, so figure out a way.
So as you say you can use a function to do that,
if you are using one table you don't need the $table
parameter, for multiple table you can use it
function storeComment($title,$content,$id,$table="defult_table_name"){
$stmt = $conn->prepare("INSERT INTO $table (title,content,id) VALUES (?, ?,?)");//No of question marks are equal to columns you have mentioed
$stmt->bind_param("ssi",$empName, $title,$id);
// i - integer
// d - double
// s - string
// b - BLOB
$stmt->execute();
$stmt->close();
}
Hope it helps :)

Kuru
- 738
- 5
- 18