0

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();
    }
Jen143Me
  • 273
  • 7
  • 24
  • I assume you are getting json data. And your only problem is to loop through the data. Please check this link [foreach](http://stackoverflow.com/questions/6208052/jquery-each-for-objects) – irfan rasool May 13 '15 at 07:25

2 Answers2

0

$each loop is used as

$.each( result, function( key, value ) {
  alert( key + ": " + value );
});
//here result is json encoded object
user3419778
  • 856
  • 3
  • 8
  • 11
  • How am I going to display the fields from the database into my view? – Jen143Me May 13 '15 at 07:31
  • Take the value of it and add it with the jquery code $('#YOUR_ID').text(VALUE) in your view; YOUR_ID is the id of your div, span or p of your view. – user3419778 May 13 '15 at 07:53
0

use echo for sending json data in controller it would be

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;
    }

And In view you can get it as

success: function(result){
      $.each( result, function( key, value ) {

    });    
    }
Saty
  • 22,443
  • 7
  • 33
  • 51
  • Is the event I used is correct? Cuz it gets the value every time I input something in the input area. I want to used like onclick function if is a button that it only gets the value after clicking. – Jen143Me May 13 '15 at 07:38