dweb Posted November 5, 2012 Share Posted November 5, 2012 Hi everyone I have a QUERY to get a product based on a ID, and I am trying to make a next and previous button. I'm using the following query SELECT p.id, p.name, prev.id AS prevID, prev.name AS prevNAME, next.id AS nextID, next.name AS nextNAME FROM products p LEFT JOIN products prev ON prev.id < p.id LEFT JOIN products next ON next.id > p.id WHERE p.type = 1 AND p.del = 1 AND p.id = '".$currentID."' AND prev.type = 1 AND prev.del = 1 AND next.type = 1 AND next.del = 1 ORDER BY prev.id DESC,next.id ASC LIMIT 1 which gives me the current product, the next product name and ID and the previous product name and ID It works ok, the problem is when you reach the last row, or first row, it won't return anything, I think because of the part AND prev.type = 1 AND prev.del = 1 AND next.type = 1 AND next.del = 1 Is there a better way to do this and a way which will work Thanks Quote Link to comment Share on other sites More sharing options...
Barand Posted November 5, 2012 Share Posted November 5, 2012 SELECT p.id, p.name, prev.id AS prevID, prev.name AS prevNAME, next.id AS nextID, next.name AS nextNAME FROM products p LEFT JOIN products prev ON prev.id < p.id AND prev.type = 1 AND prev.del = 1 LEFT JOIN products next ON next.id > p.id AND next.type = 1 AND next.del = 1 WHERE p.type = 1 AND p.del = 1 AND p.id = 1 ORDER BY prev.id DESC,next.id ASC LIMIT 1 Quote Link to comment Share on other sites More sharing options...
dweb Posted November 6, 2012 Author Share Posted November 6, 2012 thanks very much Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.