Skipjackrick Posted October 29, 2009 Share Posted October 29, 2009 I've got a challenge and I can't come up with a solution. I have a photo gallery where I query and display the last 12 images using the code below. (This is only a portion) <?php //Query the database for the image information $query_image = "SELECT * FROM image ORDER BY image_id DESC LIMIT 12"; $image_result = mysql_query($query_image) or die(mysql_error()); ?> Well, this only shows the last 12 images. I want to put a link that will re-load the page with the next 12 images. How would I run my query? Basically I need something like this.....LOL <?php //Query the database for the image information $query_image = "SELECT * FROM image ORDER BY image_id (subtract 12) LIMIT 12"; $image_result = mysql_query($query_image) or die(mysql_error()); ?> Or do you guys have a better way of doing this action? Quote Link to comment https://forums.phpfreaks.com/topic/179490-solved-need-a-smart-query-next-12-results/ Share on other sites More sharing options...
MatthewJ Posted October 29, 2009 Share Posted October 29, 2009 <?php //Query the database for the image information $query_image = "SELECT * FROM image ORDER BY image_id DESC LIMIT 12, 12"; $image_result = mysql_query($query_image) or die(mysql_error()); ?> When providing both numbers to limit, the first one tells the database which record to start with, the second tells it how many rows to return. Quote Link to comment https://forums.phpfreaks.com/topic/179490-solved-need-a-smart-query-next-12-results/#findComment-947031 Share on other sites More sharing options...
JonnoTheDev Posted October 29, 2009 Share Posted October 29, 2009 Search the forum for paginating results. Your pagination could consist of 2 links, 'prev 12', 'next 12' Quote Link to comment https://forums.phpfreaks.com/topic/179490-solved-need-a-smart-query-next-12-results/#findComment-947033 Share on other sites More sharing options...
Maq Posted October 29, 2009 Share Posted October 29, 2009 You can use the same concept as pagination. There is an excellent tutorial on phpfreaks that illustrates this: http://www.phpfreaks.com/tutorial/basic-pagination MatthewJ's code will grab 12 records, starting at the 12th, which will essentially grab 12-24 in a descending manner. This works once, but if you want something dynamic then I suggest you read the tutorial, specifically the MySQL of how CV keeps track of the pages and records. Quote Link to comment https://forums.phpfreaks.com/topic/179490-solved-need-a-smart-query-next-12-results/#findComment-947036 Share on other sites More sharing options...
nadeemshafi9 Posted October 29, 2009 Share Posted October 29, 2009 LIMIT 0, 12 change these to variables "Limit ".$start.", ".$limit.";"; im not sure if the , is required Quote Link to comment https://forums.phpfreaks.com/topic/179490-solved-need-a-smart-query-next-12-results/#findComment-947038 Share on other sites More sharing options...
MatthewJ Posted October 29, 2009 Share Posted October 29, 2009 Teach a man to fish I suppose... Quote Link to comment https://forums.phpfreaks.com/topic/179490-solved-need-a-smart-query-next-12-results/#findComment-947039 Share on other sites More sharing options...
Skipjackrick Posted October 29, 2009 Author Share Posted October 29, 2009 Ah, see....this is why I come here. Thanks fellas. I had never heard of pagination. Solved! Quote Link to comment https://forums.phpfreaks.com/topic/179490-solved-need-a-smart-query-next-12-results/#findComment-947041 Share on other sites More sharing options...
Skipjackrick Posted October 29, 2009 Author Share Posted October 29, 2009 Teach a man to fish I suppose... Agreed! Quote Link to comment https://forums.phpfreaks.com/topic/179490-solved-need-a-smart-query-next-12-results/#findComment-947043 Share on other sites More sharing options...
Skipjackrick Posted October 29, 2009 Author Share Posted October 29, 2009 This is my favorite part of the tutorial. Cause its my case exactly. Just had to share.......no more I promise. In fact, in my experience over the years of helping out on the forums, peoples' hardest problem about pagination is figuring out what it's called in the first place! But since we've got that part sorted out, the rest should be a piece of cake, right? Quote Link to comment https://forums.phpfreaks.com/topic/179490-solved-need-a-smart-query-next-12-results/#findComment-947048 Share on other sites More sharing options...
Maq Posted October 29, 2009 Share Posted October 29, 2009 This is my favorite part of the tutorial. Cause its my case exactly. Just had to share.......no more I promise. In fact, in my experience over the years of helping out on the forums, peoples' hardest problem about pagination is figuring out what it's called in the first place! But since we've got that part sorted out, the rest should be a piece of cake, right? That makes all the difference in the world. Finding a good book, finding a good tutorial, finding solid threads from forums etc... that are specific to what you are trying to learn or implement, is one of the toughest issues for people that are new to either the language or the concept in general. Quote Link to comment https://forums.phpfreaks.com/topic/179490-solved-need-a-smart-query-next-12-results/#findComment-947052 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.