How to pass a variable from Controller to View with Ajax in Codeigniter?

I have a problem transferring data from Controller to View.

I use Ajax for this, you can see my Ajax code here:

$(document).ready(function(){
    $('li.thang').click(function(){
        var id_thang = $(this).attr('value');
        $.ajax({
            url: baseUrl+'/Home/getImage',
            dataType: 'json',
            type: 'POST',
            data: {id_thang: id_thang},
        }).done(function(result) {
            console.log(result.get_list_image_thang);
        })          
    });
});

I get id_thangwhen clicked in HTML tags li > thang.

In Controller / Home.phpI will get the array data on this id_thang.

function getImage(){
    $id_thang = $this->input->post('id_thang');
    $input = array();
    $input['order'] = array('id','ASC');
    $get_image_thang  = $this->Mmenushoatnao->get_info($id_thang);
    ob_start();
}

All data is stored in an array $get_image_thang.

Finally, I don’t know how to pass this array to the View to show all the data that I have selected.

In the /index.php view, I am trying to loop foreachthrough all the data in this array and show in the tag <html>. Like this:

<?php foreach($get_image_thang AS $item) ?>
    <div class="col-md-4 col-sm-4 col-xs-4">
        <?php echo $item->id; ?>
    </div>
<?php endforeach ?>

Note: View / index.phpthere is a demo code.

The problem is that I do not know how to send $get_image_thangto this view.

Update 1:

console.log(result); .done(function(result) :

: row += result[i].id; id, name, image_list undefined.

2:

, id. core/MY_MODEL.php:

function get_info($id, $field = '')
{
    if (!$id)
    {
        return FALSE;
    }

    $where = array();
    $where[$this->key] = $id;

    return $this->get_info_rule($where, $field);
}

function get_info_rule($where = array(), $field= '')
{
    if($field)
    {
        $this->db->select($field);
    }
    $this->db->where($where);
    $query = $this->db->get($this->table);
    if ($query->num_rows())
    {
        return $query->row();
    }

    return FALSE;
}

get_info. :

Mmenushoatnao - .

3:

Controller click.

. Ajax.

:

function getImage(){
    $id_thang = $this->input->post('id_thang');
    $input = array();
    $input['order'] = array('id','ASC');
    $get_image_thang  = $this->Mmenushoatnao->get_info($id_thang);
    ob_start();
    ?>
    <?php foreach($get_image_thang as $item): ?>
    <?php $image_list = json_decode($item->image_list); ?>
    <?php foreach($image_list as $image): ?>
    <div class="col-md-4 col-sm-4 col-xs-4">
        <img src="<?php echo upload_url() ?>/img/hoatnao/hinhanh/<?php echo $image ?>" alt="Image" class="img-responsive">
    </div>
    <?php endforeach; ?>
    <?php endforeach; ?>

    <?php
    $return  = ob_get_clean();
    $data['result']=$return;
    echo json_encode($data);
}

, .

, Ajax.

+4
3

function getImage(){
    $id_thang = $this->input->post('id_thang');
    $input = array();
    $input['order'] = array('id','ASC');
    $get_image_thang  = $this->Mmenushoatnao->get_info($id_thang);
    echo json_encode($get_image_thang);
}

ajax ( get_info())

//....
.done(function(result) {
   var row = '';
    for (var i = 0; i < Object.keys(result).length; i++) {
         row +=  result[i].id;
    }
   $('#res').html(row);
 })

, ID ,

<div id="res" class="col-md-4 col-sm-4 col-xs-4">

</div>
+3

, , JSON, codeigniter guide , JSON , , , JSON fore, , JSON .

function getImage(){
    $id_thang = $this->input->post('id_thang');
    $id_thang = json_decode($id_thang); // decode JSON to work with it.
    $input = array();// im guessing this is the array you want to send back as JSON
    $input['order'] = array('id','ASC');
    $get_image_thang  = $this->Mmenushoatnao->get_info($id_thang);
    ob_start();// I have no clue you want to achieve with this function? more info on this would be good.

    $this->output
      ->set_content_type('application/json')
      ->set_output(json_encode(array(YOUR ARRAY HERE)));

}

. , , .

, , , $.ajax;

    $(document).ready(function(){
        $('li.thang').click(function(){
            var id_thang = $(this).val();
            $.ajax({
                url: baseUrl+'/Home/getImage',
                dataType: 'json',
                type: 'POST',
                // this is where you should stringify your JSON
                data: {id_thang: JSON.stringify(id_thang)},
            }).done(function(result) {
                console.log(result.get_list_image_thang);
            })          
        });
    });

, , , .

+1

If you want to receive json data from a remote device, you can output your result to

echo json_encode($arr);

It will integrate with the result variable from the ajax method, and then treat it as an array.

0
source

Source: https://habr.com/ru/post/1657565/


All Articles