通过ajax在codeigniter中将数据库数据显示为列表

时间:2017-04-26 06:18:51

标签: javascript php ajax list codeigniter

我是codeigniter的新手,我一直试图将数据从数据库显示为按钮形式的ul列表。它很容易使用PHP完成,但我想使用ajax显示相同,以减少加载时间。我尝试了一些方法,但无法显示数据。我真的需要帮助,帮助将不胜感激。提前谢谢。

控制器:

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

模型:

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

视图:

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

使用php显示的数据,但我想使用ajax显示相同的数据。 再次感谢。

1 个答案:

答案 0 :(得分:1)

工作示例

输出视图部分

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

我的控制器名称是制造商/方法是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);      
}

我的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视图部分是这样的(控制器中索引方法的manufacturer.php)

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

    </tbody>
</table>

希望你明白这个想法