doubledee Posted June 24, 2012 Share Posted June 24, 2012 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?? (BTW, I am not using OOP or any MVC stuff, so please keep response at my level.) Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/264698-efficiently-coding-edit-comment-link/ Share on other sites More sharing options...
Drummin Posted June 24, 2012 Share Posted June 24, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/264698-efficiently-coding-edit-comment-link/#findComment-1356653 Share on other sites More sharing options...
doubledee Posted June 24, 2012 Author Share Posted June 24, 2012 I'm thinking the answer is to create a Function to house all of the logic, and then just use the results to determine what to output in my HTML section. Debbie Quote Link to comment https://forums.phpfreaks.com/topic/264698-efficiently-coding-edit-comment-link/#findComment-1356661 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.