我想在ajax中实现类似foreach的功能,但我知道如果我使用它就不会工作。我的这个功能的目标是在不加载整个页面的情况下发表评论后显示该图片的所有评论。截至目前,这是我的部分代码。
这只是一个临时视图因为我没有在这里放置使用ajax cuz显示数据的代码我不知道该怎么做。
<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>
这是我的部分Javascript代码:我想在这里提取列表中的详细信息。我是这种语言的新手,我不知道如何实现$.each()
。我想从这里获取每个返回的字段,以便在视图中显示这些数据。
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
}
});
这是我的控制器:
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;
}
我的模特:
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();
}
答案 0 :(得分:0)
$每个循环用作
$.each( result, function( key, value ) {
alert( key + ": " + value );
});
//here result is json encoded object
答案 1 :(得分:0)
使用echo在控制器中发送json数据
echo $result; not `return $result;`
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));
}
echo $result;
}
并且在视图中你可以将其作为
success: function(result){
$.each( result, function( key, value ) {
});
}