0

I am new in codeigniter, i have been trying to display data from database as ul list in form of buttons. It was easily done using php but i want to display the same using ajax to reduce the loading time. i tried some methods but cannot able to display the data. I really need help on this, help will be appreciated. Thanks in advance.

Controller:

function user()
    {
        $this->data['list'] = $this->mobiles_model->get_status();
        $this->_render_page('user', $this->data);               
    }

model:

function get_status()
     {      
        $sql = "select * from(SELECT * FROM my_user_view  ) as t group by imei ORDER BY dattim ASC ";               
        $query = $this->db->query($sql, array($uid,$value));
        $result = $query->result();
        return $result;
     }

view:

<ul class="sidebar-menu" id="nav-accordion">
        <?php
        for ($i = 0; $i < count($deptlist); ++$i) 
        { 
            $time = $deptlist[$i]->dattim;
            $sdate=date("d-m-Y H:i:s",strtotime($deptlist[$i]->dattim));
            $dateFromDatabase = strtotime($time);
            $dateFiveMinuteAgo = strtotime("-5 minutes");
            if ($dateFromDatabase >= $dateFiveMinuteAgo) 
            {  
            ?>
        <li>
            <button  value="<?php echo $deptlist[$i]->imei ?>" class="btn-success"><?php echo $deptlist[$i]->user;?>    
        </button>       
        </li>
    <?php }
    else
    {
    ?>
    <li>
    <button  value="<?php echo $deptlist[$i]->imei ?>"class="btn-danger"><?php echo $deptlist[$i]->user; ?>
        </button>
    </li>
    <?php }
        }?>
</ul>

The data displayed using php but i want to display the same using ajax. thanks again.

Rahul
  • 45
  • 2
  • 12

1 Answers1

1

working example

to output the view part

public function index(){ 
    if($this->session->userdata('is_logged_in')){
        $this->load->view('../template/header');
        $this->load->view('manufacturer');
        $this->load->view('../template/footer');
    } else {
        redirect('main/restricted');
    }
}

my controller name is manufacturer / method is manufacturer_list

public function manufacturer_list()
{

    $result = array('data' => array());

    $data = $this->manufacturer_model->fetchManufacturerData();
    foreach ($data as $key => $value) {

        //i assigned $buttons variable to hold my edit and delete btn to pass in my array.
        $buttons  = '
        <button class="btn btn-primary" onclick="editData('.$value->id.')" data-toggle="modal" data-target="#myModal">Edit</button>
        <button class="btn btn-danger" onclick="deleteData('.$value->id.')" data-toggle="modal" data-target="#deleteModal">Delete</button>
        ';

        $result['data'][$key] = array(
            $value->id,
            $value->brand,
            $buttons
        );
    }
    echo json_encode($result);      
}

my ajax

showRecords();
function showRecords(){
    $.ajax({
        url: 'manufacturer/manufacturer_list', //controller/method
        type: 'POST',
        dataType: 'json',
        success: function(data){
            var html = '';
            for(i=0; i<data.length; i++){
                html += '<tr align="center">'+
                            '<td>'+data[i].id+'</td>'+
                            '<td>'+data[i].brand+'</td>'+
                            '<td>'+'<button class="btn btn-primary edit-data" data="'+data[i].id+'">Edit</button>'+'&nbsp;'+
                            '<button class="btn btn-danger delete-data" data="'+data[i].id+'">Delete</button>'+'</td>'+
                        '</tr>';
            }
            $("#showdata").html(html); //pass the data to your tbody
        },
        error: function(){
            alert('Could not load the data');
        }
    });
}

html view part is like this (manufacturer.php from index method in controller)

<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>Manufacturer</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody id="showdata">

    </tbody>
</table>

hope you get the idea