Jump to content

Anyone got a working php comment script or tutorial ?


nimzrocks

Recommended Posts

yeah my code would be some "forget everything you've done before" code.  You could implement the same things that they are doing.  Mines just a Get it working now script I guess.  Its all on one page if you just copy and pasted the code and created story.php it should work.  The only thing is you need to make sure your calling the correct table when showing the story.

Link to comment
Share on other sites

OK I saw that you added a time stamp to the table like I suggested, but we will also need a page column; so that we can pull comments for a specific page. This lesson will be to more parts. One more tonight and the final tomorrow. Then we can wrap up any lose ends.

After you have added that other column for your entries into your data base we will extract the comments on a per page level. As a quick side note to automate the process when we add comments to the database we will pull the page name with the Reserved variable $_SERVER['PHP_SELF'].

The automated page for pulling your comments.

<?php
$mysqli = new mysqli('dataBaseHost', 'userName', 'userPassword', 'userDataBase');
if ($mysqli->connect_error) { die('Connect Error ('.$mysqli->connect_errno .') '.$mysqli->connect_error); }
//Of course there are much better was to handle this error instead of dying but for now.
$sql='SELECT * FROM comment WHERE page='.$_SERVER['PHP_SELF'];
$result=$mysqli->query($sql);
while($row = $result->fetch_array(MYSQLI_ASSOC) {
  //At this point I am guessing that this will work as a while statement. But I am pretty sure.
  //Next we need your HTML here so that it will be styled the way you would like for your comments.
  //We should be able to spit our the parts of the returned query like so.
  echo $row['time'];
  echo $row['name'];
  echo $row['url'];
  echo $row['notes'];
/*Be sure to include your HTML that will separate each comment block you will most likely want the separation to be applied on the top.*/
}
$result->close();
$mysqli->close();
?>

Tomorrow I will combine all of the sections together appropriately and then we will add any missing pieces. Then you can have a look and tell me what else you would like.

Link to comment
Share on other sites

I added that page column as you requested.

 

CREATE TABLE  `mydatabase`.`comment` (

`commentid` INT NOT NULL AUTO_INCREMENT ,

`time` TIMESTAMP NOT NULL ,

`pagecolumn` INT NOT NULL,

`name` VARCHAR( 40 ) NOT NULL ,

`email` VARCHAR( 50 ) NOT NULL ,

`url` VARCHAR( 50 ) NOT NULL ,

`notes` VARCHAR( 500 ) NOT NULL ,

PRIMARY KEY (  `commentid` ) ,

UNIQUE (

`commentid`

)

) ENGINE = MYISAM

 

I'm bit confuse now but I made the file and named it $_SERVER['PHP_SELF'].php  that is what you asked me to do right ? Waiting for you next step WolfRage :)

Link to comment
Share on other sites

No make the name something like include_comments.php. Change your pagecolumn column to a varchar not an int. I need your html that will style the comments just like you want. So please mock up the html the way you would want it to look for a comment, you can even include a fake comment and I will modify it to properly loop with our SQL Query.

Link to comment
Share on other sites

Ok so the html structure should look something like this:

<div class="comment_container">
<p class="comment"></p>
<p class="comment_signature"></p>
</div>

*Note to self be sure to perform nl2br before inserting the comments into the DB.

You can of course apply your CSS rules using the classes.

I will have more latter today, should have time throughout the day and for the rest of the week during work. We will get it done.

 

Sorry for delays but as you know time is money. This weekend I launched one new website and I am nearing a deadline for another project that has much work still to be done. Any ways still trying to squeeze you in.

Link to comment
Share on other sites

Simply includen the script below for the page with comments. Not tested!

 
<?php
$mysqli = new mysqli('dataBaseHost', 'userName', 'userPassword', 'userDataBase');
if ($mysqli->connect_error) { die('Connect Error ('.$mysqli->connect_errno .') '.$mysqli->connect_error); }
//Of course there are much better was to handle this error instead of dying but for now.
$sql='SELECT * FROM comment WHERE page='.$_SERVER['PHP_SELF'];
$result=$mysqli->query($sql);
while($row = $result->fetch_array(MYSQLI_ASSOC)) {
?><div class="comment_container">
  <p class="comment"><?php echo $row['notes']; ?></p>
  <p class="comment_signature"><?php echo $row['name'].' | '.$row['time']; ?></p>
  </div><?php
/*Be sure to include your HTML that will separate each comment block you will most likely want the separation to be applied on the top.*/
}
$result->close();
$mysqli->close();
?>

 

In order to post comments you will need to use the comment form that you created earlier and submit to this script:

<?php
if($_POST['submit']==='Submit') {
  $name=htmlspecialchars(trim($_POST['visitor'])); // we use htmlspecialchars to prevent some one from injecting content.
  $email=htmlspecialchars(trim($_POST['visitormail'])); //We could also use the filter_var() here if you want more precision.
  $url=htmlspecialchars(trim($_POST['url']));
  $notes=nl2br(htmlspecialchars(trim($_POST['notes'])));
  if(empty($name)&&empty($email)&&empty($url)&&empty($notes)) {
    $mysqli = new mysqli('dataBaseHost', 'userName', 'userPassword', 'userDataBase');
    if ($mysqli->connect_error) { die('Connect Error ('.$mysqli->connect_errno .') '.$mysqli->connect_error); }
    //Of course there are much better was to handle this error instead of dying but for now.
    $sql='INSERT INTO table_name (name, email, url, notes, time, pagecolumn) VALUES ('.$mysqli->real_escape_string($visitor).', '.$mysqli->real_escape_string($visitoremail).', '.$mysqli->real_escape_string($url).', '.$mysqli->real_escape_string($notes).','.$mysqli->real_escape_string($_SERVER['REQUEST_TIME']).','.$mysqli->real_escape_string($_SERVER['PHP_SELF']).')';
    $mysqli->query($sql);
    $mysqli->close();
  }
  else { echo 'Nothing was Posted!'; }// we will change this to be more helpful later.
  header('Location: '.$_SERVER['HTTP_REFERER']); //HTTP_REFERER may not always be set by the users browser!
}
else { echo 'Nothing was Posted!'; }// we will change this to be more helpful later.
?>

Besure to take the time to customize these scripts further; at the very least you will need to change the database settings to correctly connect.

 

Truth of the matter is that you had everything that you needed right here to complete your comment script. However you unfortunately did not participate in the excercise. Thus you have learned little of PHP; so it is very unlikely that I will help you out agian. Particularly because you do not appreciate free work and expected me to build you something quickly even though you were not willing to contribute anything more than setting up the database.

Link to comment
Share on other sites

No its not like that WolfRage. I highly appreciate your help but Its been more than 3 weeks, I have stuck with this and I can not finish my project till I complete this that is why I said that. I'm really tired of doing this. Do not think this is the only place where I tried. I tried every where and tried to learn it and I'm glad I learn lil bit because of you. I always appreciate your help. I will put all together and I will test this right now.

 

Thank you very much my friend.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.