Jump to content

Efficiently coding "Edit Comment" link


doubledee

Recommended Posts

I want to add an "Edit Post" link to under my Article Comments section, and could use some advice on how to go about coding it in an efficient manner?!

 

Logically what I need to do is this...

 

- Check current time

- Determine how much time has transpired since the Orig Comment was posted

- If the Comment was made < 5 minutes ago, AND the person who is logged in is the person trying to edit the Comment, then I want to display a simply "Edit Comment" hyperlink.

 

What I am trying to avoid is writing a whole bunch of PHP logic in my HTML section.

 

How should I go about separating the Logic from my HTML??  :shrug:

 

(BTW, I am not using OOP or any MVC stuff, so please keep response at my level.)

 

Thanks,

 

 

Debbie

 

 

Link to comment
Share on other sites

VERY ROUGH example Debbie based on no example to start with of what you're doing.

<?php	
//Get current time
$current_time=strtotime('now');

//Assuming member id is session
$memberID=$_SESSION['memberID'];

// Build query.  Change table/fields to what is needed.
$q1 = "SELECT a.article_title, a.comment, a.commentID, a.time, a.member_id, u.member_name
FROM article_comments AS a 
LEFT JOIN users AS u
ON a.member_id=u.member_id";

// Prepare statement.
$stmt1 = mysqli_prepare($dbc, $q1);

// Bind variables to query.
mysqli_stmt_bind_param($stmt1);

// Execute query.
mysqli_stmt_execute($stmt1);

// Store results.
mysqli_stmt_store_result($stmt1);

	// Check # of Records Returned.
	if (mysqli_stmt_num_rows($stmt1) > 0){
		// comments Found.

		// Bind result-set to variable.
		mysqli_stmt_bind_result($stmt1, $article_title, $comment, $commentID, $time, $member_id, $member_name);

		//Format output to your configuration for display. Store to variable.
		$comment_display="";

		//Loop through query results and format output
		while (mysqli_stmt_fetch($stmt1)){

			$display_time = date("m/d/Y h:i:s A",$time);

			$cutoff_time = $time+(60*5);

			$comment_display .="<tr>
			<td rowspan=\"2\" align=\"center\"><strong>$member_name</strong></td>
			<td>$article_title<br />$display_time";

			//check if member viewing is author
			$comment_display .=(isset($memberID) && $memberID==$member_id && $current_time<$cutoff_time ? "<style=\"float:right\"><a href=\"editcomment.php?$commentID\">Edit Comment</a></style>" : '' );
			$comment_display .="</td></tr>";

			$comment_display .="<tr>
			<td>$comment</td></tr>";
		}
	}

?>
<html>
<body>
<table>
<?php
if (isset($comment_display)){ echo "$comment_display";}
?>
</table>
</body>
</html>

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.