Jump to content

[SOLVED] Need a smart query. Next 12 results


Skipjackrick

Recommended Posts

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?

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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? :)
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.