Reputation: 833
I have the following controller
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>";
}
}
and 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();
}
In my database i am using ,the ids
are not sequential since i have deleted some of the records.How would i use $row = $query->next_row()
to go to the next row?.
Upvotes: 1
Views: 5188
Reputation: 833
This is probably the Worst™ solution that works.Its kinda late and i needed this to work at all costs.I am about to to bed,so am a bit happy.
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>";
}
}
I am trying to figure out a way to do this in codeigniter database class instead of pdo.
Upvotes: 2