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?

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

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.

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? :)

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.