I put you in background:
I have one base de datos
with a table called entradas
, each entry I make in the blog, its corresponding fields are created, including the id
(using autoincrement and starting at 1).
In the html
, I load an entry (Either the last entry or X entry, or the first, for example). Just below the input text, I have two : Siguiente entrada y entrada anterior
.
Now comes the important part of the matter. How do I determine what is the next and previous entry? Imagine that I have four entries (ID 1, 2, 3, 4) and I decide to delete the second one, so it remains: ID: 1, 3, 4.
How do I know now, that my next entry (Starting with the first ID:1) is the one with ID 3? I imagine that I would have to pass a parameter to that function ($id) and from that parameter, calculate the next record in the database table, but I don't know how.
I hope I have explained myself well. The thing is, I'm doing it using codeigniter, and I don't know how to apply it or do it.
I edit with the answer provided by Mauricio Villa . I've changed a few things, if anyone manages to improve their efficiency and code, welcome:
public function nextEntrada($id){
[mas lineas de códigos]
$condicion = false;
$cont = 0;
while($condicion == false || $cont < $this->mi_modelo->numeroDeEntradas()){
$entradaDatos = array ( "entrada" => $this->mi_modelo->obtenerEntrada($id));
$num = $this->mi_modelo->numDeEntradasAfectadas($id);
if($num > 0){
$condicion = true;
}
else{
$id--;
}
$cont++;
}
[mas lineas de codigo]
}
Edit for the last time, in the end I used Irving Zamora 's answer, which I found to be more efficient.
Controller:
public function nextEntrada($id){
[mas lineas de códigos]
$resultado = $this->mi_modelo->siguiente_entrada_model($id);
[mas lineas de codigo]
}
Model:
public function siguiente_entrada_model($id)
{
$this->db->select('*');
$this->db->from('TABLA');
$this->db->where("(id>'{$id}'");
$this->db->order_by('id', 'DESC');
$this->db->limit('1');
$query = $this->db->get();
return $q->result();
}
I imagine that
siguiente entrada
andentrada anterior
are links, if so you could send them the oneid
of the current input +1 and -1 forsiguiente entrada
andentrada anterior
respectively, which means that you would be sending your controller the oneid
to perform the query, if the result of the query is greater to zero you can return it to the view otherwise perform the query again adding one until you find a value forsiguiente entrada
and in the case ofanterior entrada
create another controller and do the same but instead of adding 1 toid
it subtract 1, it's already a matter of you do your logic in the controllers. An example:Controller:
Model:
I hope it works for you, anything just comment.
Searching one by one until the "next entry" is found is inefficient. Try performing the following query on your DB of course using your CodeIgniter notation.
What the query does is: Select the fields you want from the table you want, where the id is greater than the one you provide, and you get only 1 ("The next entry")
For "the previous entry" just change ">" to "<"
I hope it is helpful
Model: