Welcome Guest, Not a member yet? Register   Sign In
'Greater than' operator is working but not 'lesser than' in Model class
#2

(This post was last modified: 07-11-2018, 02:46 AM by Pertti.)

There's a simple explanation to this, and very simple fix.

If you don't order your query result, it will be ordered the same way it went in.

Lets assume you have items 1 2 3 and 4, the first matching item ID > 2 would be ID = 3, but the first matching result for ID < 4 would be ID = 1, because that's the first SQL server can find.

What you want to do is reverse results when you go backwards (4 3 2 1).

PHP Code:
public function get_previous_info() {
    
$a $this->input->post('infoID');        
    
$this->db->where('infoID <'$a);
    
$this->db->order_by('infoID''DESC'); // sort in reverse order
    
$query $this->db->get('info');                 
       return 
$query->row_array();


Because you only want the first matching item, I would also add limit to both methods. Right now the query will return all matching results from DB, and if you get to ID 1000, that's just unnecessary data your DB server has to return.

PHP Code:
public function get_previous_info() {
    
$a $this->input->post('infoID');        
    
$this->db->where('infoID <'$a);
    
$this->db->order_by('infoID''DESC');
    
$this->db->limit(1); // only return first matching result
    
$query $this->db->get('info');                 
       return 
$query->row_array();

Reply


Messages In This Thread
RE: 'Greater than' operator is working but not 'lesser than' in Model class - by Pertti - 07-11-2018, 02:45 AM



Theme © iAndrew 2016 - Forum software by © MyBB