Friday, September 12, 2014

Commenting Function with PHP Codeigniter


 A user is able to comment on an image according to our project. So let's take a look at how this commenting function happens.



View

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Update and Delete with Slide Effect.....</title>
<link href="frame.css" rel="stylesheet" type="text/css"><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
  <script type="text/javascript">
 $( document ).ready(function () {

//class of comment button
//what to do when btn is clickd
$(".comment_button").click(function() 
{
var element = $(this);
   
   //assign value of comment box to boxval
   //#content is id of comment box
   var boxval = $("#content").val();
    var dataString =  "comment="+ boxval +"&img_id="+ <?php echo $_GET['img_id'] ?> + "&userName=<?php echo $_SESSION['email']; ?>"  +"";

if(boxval=='')
{
alert("Please Enter Some Text");
}
else
{
//#flash is id of div tag
$("#flash").show();
$("#flash").fadeIn(1000).html('<img src="ajax.gif" align="absmiddle">&nbsp;<span class="loading">Loading Update...</span>');

$.ajax({
//URL to send the request to
url: 'addComment',
type: 'POST',
//specifies data to be sent to the server
data: dataString,
 //A function to be run when the request succeeds 
success: function(html) {
document.getElementById('update').innerHTML = document.getElementById('update').innerHTML + html;
document.getElementById('content').value='';
$("#flash").hide();
},
//A function to be run when the request fails 
error: function(data) {
    alert("ERROR !");
}
});
}
return false;
});
});
</head>
<body>
<div align="center">
<table cellpadding="0" cellspacing="0" width="500px">
<tr>
<td>
<div id="test">
</div>
<div style="height:7px"></div>
<div id="flash" align="left"  ></div>
<ol  id="update" class="timeline">

<div id="old_updates">
</div>
<?php
//assign the current element's key to the $key variable on each iteration
//$comments arr created in viewImage.php
foreach ($comments as $key => $val) {
$user = $val['userName'];
$comment = $val['comment'];

echo "<li>";
echo "$user </br>$comment";
echo "</li>";
}
?>

</ol>
<div align="left">
<form  method="post" name="form" action="">
<?php if($_SESSION['teamRole']=='Scrum Master'){ 
echo '<table cellpadding="0" cellspacing="0" width="500px">
<tr><td align="left"><div align="left"><h3>What do you think?</h3></div></td></tr>
<tr>
<td style="padding:4px; padding-left:10px;" class="comment_box">
<textarea cols="30" rows="2" style="width:480px;font-size:14px; font-weight:bold" name="content" id="content" maxlength="145" ></textarea><br />
<button type="submit" id="update" name="submit" class="comment_button">Comment</button>
</td></tr></table>';
}?>
</form>
</div>
</div>
</td></tr></div>
</li></table></div>
</body></html>


Comments have been written in the code to explain each line of the code. Basically what happens is in the body part the textbox needed to add the comment is there. Above it is an ordered list to show all the previously written comments. Only the Scrum master can write comments for a particular image.The developers can only view these comments.  


Model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class model_viewImage extends CI_Model{
public function __construct(){
parent::__construct();
}

public function getComments($imgID){ 
                  $this->db->select("message, msg_id, userName");
                  $whereCondition =array('img_id' =>$imgID);
                  $this->db->where($whereCondition);
                  $this->db->from('comment');
                  $query = $this->db->get();
                  return $query->result(); //returns a boolean value
            }

            public function insertComment($userName, $img_id, $comment){ 
                  $data = array(
                  'message' => $comment ,
                  'img_id' => $img_id ,
                  'userName' => $userName
                  );
                  $this->db->insert('comment', $data); 
            }
}

?>

The function insertComment() inserts the comments to the database where as the function getComments() is used  to get the saved comments from the database.

No comments:

Post a Comment