爲了能夠讓你像第二個數據,你需要給我們ASC和返回result_array()
http://www.codeigniter.com/user_guide/database/query_builder.html
的工作形象
如下面的模型圖所示:
文件名: Model_sample.php
<?php
class Model_sample extends CI_Model {
public function get_data() {
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'class');
$this->db->order_by('priority', 'ASC');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return false;
}
}
}
控制器
文件名:的welcome.php
<?php
class Welcome extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('model_sample');
}
public function index() {
$data['results'] = array();
$data['results'] = $this->model_sample->get_data();
$this->load->view('welcome_message', $data);
}
}
查看
文件名: welcome_message.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to CodeIgniter</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" integrity="sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous">
</head>
<body>
<div class="container">
<table class="table">
<thead>
<tr>
<td>Class ID</td>
<td>Name</td>
<td>Priority</td>
</tr>
</thead>
<tbody>
<?php if ($results) {?>
<?php foreach ($results as $result) {?>
<tr>
<td><?php echo $result['id'];?></td>
<td><?php echo $result['name'];?></td>
<td><?php echo $result['priority'];?></td>
</tr>
<?php }?>
<?php } else {?>
<tr>
<td>No Results</td>
</tr>
<?php }?>
</tbody>
</table>
</div>
</body>
</html>
如果順序,然後'Ashish'應該是第一位的。說啥? –
我想你想按優先順序排列「ASC」,不是嗎? –
但阿米特的位置是一樣的。 – user3653474