1

我有以下控制器

controller

function bret($id){
$this->load->model('school_model');
$data = $this->school_model->get_city_and_population($id);
foreach ($data as $row)
{
echo "<b>Name Of The City</b>...........". $row['Name'];
echo "<br/>";
echo "<b>Total Population</b>...........".$row['Population'];
//echo "<a href='".$data->next_row()."'>next</a>";
}
}

model

function get_city_and_population($id){
$this->db->select('Name,Population');
$query = $this->db->get_where('city', array('ID'=>$id));
return $query->result_array();
}

在我正在使用的数据库中,ids由于我删除了一些记录,所以它们不是连续的。我将如何使用$row = $query->next_row()转到下一行?

4

1 回答 1

2

这可能是最糟糕的™ 解决方案。它有点晚了,我需要不惜一切代价工作。我要睡觉了,所以有点高兴。

Model

function get_next($id){
$db = new PDO('mysql:host=localhost;dbname=world', 'root', '');
$stmt = $db->query("SELECT * FROM city WHERE id > $id ORDER BY id LIMIT 1;");
$id = $stmt->fetchColumn(0);
if ($id !== false) {
    return $id;
}
}
function get_previous($id){
$db = new PDO('mysql:host=localhost;dbname=world', 'root', '');
$stmt = $db->query("SELECT * FROM city WHERE id < $id ORDER BY id DESC LIMIT 1;");
$id = $stmt->fetchColumn(0);
if ($id !== false) {
    return $id;
}
}

Controller

function bret($id){
$this->load->model('school_model');
$data = $this->school_model->get_city_and_population($id);
$next = $this->school_model->get_next($id);
$previous = $this->school_model->get_previous($id);
foreach ($data as $row)
{
echo "<b>Name Of The City</b>...........". $row['Name'];
echo "<br/>";
echo "<b>Total Population</b>...........".$row['Population'];
echo "<br/>";
echo "<a href='".$next."'>next</a>";
echo "<br/>";
echo "<a href='".$previous."'>previous</a>";
}
}

我试图找出一种在codeigniter数据库类而不是pdo中执行此操作的方法。

于 2013-07-06T19:04:17.747 回答