I wanted to implement like foreach function inside ajax but I know it will not work if I used it. My goal for this function is to display all the comments of that picture after giving a comment without loading the entire page. As of now, this is my partial code.
This is just a temporary view cuz I haven't put here the code for displaying the data using the ajax cuz I don't know also how to do it.
<ul class="list">
<?php
foreach($comment as $row2) {
if($row['uploadID']==$row2['uploadID']) {
?>
<li>
<div class="col-md-2 preview"><a href="#"><img src="<?php echo base_url();?>uploads/Profile/<?php echo $row2['profilePic'];?>" height='50' alt=""></a></div>
<div class="col-md-9 data">
<div class="title"><span class='text-info'><?php echo $row2['firstName']." ".$row2['lastName']?></span> : <?php echo $row2['comment']?><p class='pull-right'><a title='Remove this comment' href='<?php echo base_url();?>index.php/photoCheese/deleteComment/<?php echo $row2['commentID'];?>'><i class='fa fa-times'></i></a></p></div>
<div class="title"><img src='<?php echo base_url();?>images/blog-icon1.png' title='date'> <span><?php echo(date('M d, Y H:i a',strtotime($row2['date_comment'])));?></span></div>
<span class="comment-arrow"> </span>
</div>
<div class="clear"></div>
</li>
<?php
}
}
?>
</ul>
This is my partial Javascript code: I want here to extract the details on the list. Im new to this language I don't know how to implement $.each()
. I want to get from here every field being returned in order to display those data in the view.
function myComment()
{
var comment = document.getElementById('commentID').value;
var upload = document.getElementById('uploadID').value;
jQuery.ajax({
type:"GET",
url: "<?php echo base_url();?>index.php/photoCheese/userComment/",
dataType:'json',
data: {uploadID : upload, comment_user:comment},
error: function(result){
alert("No data!");
},
success: function(result){
//getting the data per field here
}
});
This is my Controller:
public function userComment()
{
$userID = $this->session->userdata('userID');
$uploadID = $this->input->get('uploadID');
$comment = $this->input->get('comment_user');
$data = array('userID'=>$userID,
'uploadID'=>$uploadID,
'comment'=>$comment);
$confirm = $this->photoCheese_model->insertComment($data);
if($confirm){
$result = $this->photoCheese_model->getCommentInd($uploadID);
$this->output->set_content_type('application/json');
$this->output->set_output(json_encode($result));
}
return $result;
}
My Model:
public function insertComment($data)
{
$this->db->insert('tbl_comment',$data);
return true;
}
public function getCommentInd(uploadID){
$this->db->select()->from('tbl_comment')->join('tbl_personalinfo','tbl_comment.userID=tbl_personalinfo.userID');
$this->db->where('uploadID',$uploadID);
$this->db->order_by('date_comment asc');
$this->db->limit(6);
$query = $this->db->get();
return $query->result_array();
}