• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved] How to get array json on to the view?

#1
I have this ajax script on success it gets a response which is array.

Code:
[{"aid":"17","pid":"0","uid":"4","posthash":"581ebfc0e8657","filename":"IMG_20161027_153658.jpg","filetype"
:"image\/jpeg","filesize":"1840","attachname":"112016\/IMG_20161027_153658.jpg","downloads":"0","dateuploaded"
:"1478410178","thumbnail":"112016\/IMG_20161027_153658.jpg"},{"aid":"18","pid":"0","uid":"4","posthash"
:"581ebfc0e8657","filename":"IMG_20161027_153658.jpg","filetype":"image\/jpeg","filesize":"1840","attachname"
:"112016\/IMG_20161027_153658.jpg","downloads":"0","dateuploaded":"1478410185","thumbnail":"112016\/IMG_20161027_153658
.jpg"}]


Code:
<script type="text/javascript">
$('#newattachment').on('click', function(e){
    var formData = new FormData();
    formData.append('attachment', $('input[type=file]')[0].files[0]);
    formData.append('posthash', $('#posthash').val());
    $.ajax({
        type: 'post',
        url: "<?php echo base_url('newthread/upload_attachment');?>",
        data: formData,
        cache: false,
        contentType: false,
        processData: false,
        success: function(response){
            if (response) {

                alert(response); // to #list_attachments

            } else {
            
            }
        }
    });

    e.preventDefault();
});
</script>


But I need to be able to display it on my view How can I


Code:
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12" id="list_attachments">
<ul class="list-inline">
<?php if ($attachments) {?>
    <?php foreach ($attachments as $attachment) {?>
        <li class="list-group-item">
        <span class="badge"><?php echo $attachment['filesize'];?> KB</span>
        <?php echo $attachment['filename'];?></li>
    <?php }?>
<?php }?>
</ul>
</div>
</div>


On success full uploads it gets the current files $attachments


PHP Code:
public function upload_attachment() {
    $config['upload_path'] = './uploads/' date('mY');
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = 3000;
    $config['max_width'] = 0;
    $config['max_height'] = 0;
    $config['max_height'] = 0;
    $config['overwrite'] = TRUE;

    $this->upload->initialize($config);

    if ($this->upload->do_upload('attachment')) {
            
        $upload_data 
$this->upload->data();
        $this->attachment_model->upload_attachment($upload_data'4'$this->input->post('posthash'));
        $attachments $this->attachment_model->get_attachments($this->input->post('posthash'), '4');
    }

    $this->output
    
->set_content_type('application/json')
    ->set_output(json_encode($attachments));


There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply

#2
I have a solution here that I found

Please let me know if any thing better using


Code:
response.forEach(function(element) {



Code:
<script type="text/javascript">
$('#newattachment').on('click', function(e){
    var formData = new FormData();
    formData.append('attachment', $('input[type=file]')[0].files[0]);
    formData.append('posthash', $('#posthash').val());
    $.ajax({
        type: 'post',
        url: "<?php echo base_url('newthread/upload_attachment');?>",
        data: formData,
        cache: false,
        contentType: false,
        processData: false,
        success: function(response){
            if (response) {
                html  = '';
                html  += '<ul class="list-unstyled">';
                    response.forEach(function(element) {

                        html  += '<li class="list-group-item">';
                        html  += '<span class="badge">' + element['filesize'] + 'KB </span>';
                        html  += element['filename'];
                        html  += '</li>';
                        
                    });
                html  += '</ul>';

                $('#list_attachments').html(html);
            } else {
            
            }
        }
    });

    e.preventDefault();
});
</script>
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply

#3
great dude, hope this thread can help another people with the same case
Caffeine IT Solutions | Bina Darma University
Reply

#4
You can also do this.

PHP Code:
echo json_encode($attachments); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.