'Greater than' operator is working but not 'lesser than' in Model class - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10) +--- Thread: 'Greater than' operator is working but not 'lesser than' in Model class (/showthread.php?tid=71141) |
'Greater than' operator is working but not 'lesser than' in Model class - Leolabs - 07-11-2018 This is my example table: Code: infoID userNAME This is my model: Code: public function get_next_info() { My issue is 'get_next_info' is working fine in my view,when a 'NEXT' button is pressed,the data is able to display from 1 to 2 to 3 and so on,But when a 'PREVIOUS' button is pressed,the data straight go back id no1,instead of 3 to 2 to 1 as intended. Anything wrong with my 'get_previous_info' query? RE: 'Greater than' operator is working but not 'lesser than' in Model class - Pertti - 07-11-2018 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() { 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() { RE: 'Greater than' operator is working but not 'lesser than' in Model class - Leolabs - 07-11-2018 (07-11-2018, 02:45 AM)Pertti Wrote: There's a simple explanation to this, and very simple fix. Hi Pertti,thanks for the help!Work like a charm now for both methods. RE: 'Greater than' operator is working but not 'lesser than' in Model class - Pertti - 07-11-2018 (07-11-2018, 03:56 AM)Leolabs Wrote: Hi Pertti,thanks for the help!Work like a charm now for both methods. Happy to be of assistance RE: 'Greater than' operator is working but not 'lesser than' in Model class - Gurutechnolabs - 10-18-2018 Please check In your database "infoID" datatype if data type is int than only work if datatype is not int or float than you must be need to convert first it to int for example "CAST(infoID) AS INT" |