Jump to content

Previous/Next MySQL Query


suttercain

Recommended Posts

Hi everyone,

 

I use this script:

 

<?php
$sql = mysql_query("SELECT * FROM comics WHERE comic_id ='".mysql_real_escape_string($_GET['id'])."'") or die(mysql_error());
	 $row = mysql_fetch_assoc($sql);

	 $sql1 = "SELECT comic_id FROM comics WHERE title='" . $row['title'] . "' AND issue_number > '" . $row['issue_number'] . "' ORDER BY issue_number + 0 LIMIT 1";
    if ($result1 = mysql_query($sql1)) {
      if (mysql_num_rows($result1)) {
        $row1 = mysql_fetch_assoc($result1);
      }
    }

$sql2 = "SELECT comic_id FROM comics WHERE title='" . $row['title'] . "' AND issue_number < '" . $row['issue_number'] . "' ORDER BY issue_number + 0 DESC LIMIT 1";
    if ($result2 = mysql_query($sql2)) {
      if (mysql_num_rows($result2)) {
        $row2 = mysql_fetch_assoc($result2);
      }
    }

if (isset($row2['comic_id']) || isset($row1['comic_id'])) {
		  echo "<div class='comicImgLeftPrevNext'>";
		  $echoDiv = "</div>";
		    }
if (isset($row2['comic_id'])) {
		  echo "</a><a href='http://www.supermandatabase.com/comics/" . $row2['comic_id'] . "/'/>PREV</a>";
		  	}
		  if (isset($row2['comic_id']) && isset($row1['comic_id'])) {
		  echo " | ";
		  	}
		  if (isset($row1['comic_id'])) {
		  echo "</a><a href='http://www.supermandatabase.com/comics/" . $row1['comic_id'] . "/'/>NEXT</a><br>";
		  	}
?>

 

This is used to go to the next comic book in a series... example Supergirl 21... hit next, goto Supergirl 22.

 

The Problem is there may be 3 supergirl 22 because there were different volumes. Is it possible to increment with the next based not only on the issue number but also the date? I have the dates in a 9/1/1938 format which may complicate things... but this would solve my issue since supergirl 21 in january 1995 will obviously be followed by supergirl 22 in February 1995 and not supergirl 22 in December 1978.

 

Thanks in advance for the help.

 

SC

 

Link to comment
Share on other sites

thought I had it... I tried

 

<?php
$sql = mysql_query("SELECT * FROM comics WHERE comic_id ='".mysql_real_escape_string($_GET['id'])."'") or die(mysql_error());
	 $row = mysql_fetch_assoc($sql);

	 $sql1 = "SELECT comic_id FROM comics WHERE title='" . $row['title'] . "' AND issue_number > '" . $row['issue_number'] . "' AND cover_date > '" . $row['cover_date'] . "'  ORDER BY issue_number + 0 LIMIT 1";
    if ($result1 = mysql_query($sql1)) {
      if (mysql_num_rows($result1)) {
        $row1 = mysql_fetch_assoc($result1);
      }
    }

$sql2 = "SELECT comic_id FROM comics WHERE title='" . $row['title'] . "' AND issue_number < '" . $row['issue_number'] . "'  AND cover_date < '" . $row['cover_date'] . "' ORDER BY issue_number + 0 DESC LIMIT 1";
    if ($result2 = mysql_query($sql2)) {
      if (mysql_num_rows($result2)) {
        $row2 = mysql_fetch_assoc($result2);
      }
    }
?>

 

but it didn't work...

Link to comment
Share on other sites

The way you are trying to do it is a little messy.

I advice getting a class for it, (you can google some good classes) and keep it cleaner.

This will allow you to modify it, and will probably fix the errors you are having naturally.

Pagination can be a pain without having controlled modules to help figure out what's causing the

problems and a class can help you do that.

Link to comment
Share on other sites

I think you're right my problem is the date format of 1/1/2000 which is a basic us calendar date as opposed to the mysql format of 2000-01-01. I don't even know how to convert all 5000 plus records to the mysql format. Do you know if there is a simple way of doing the conversion?

 

Thanks again.

Link to comment
Share on other sites

I am dying here...

 

I am trying to manipulate the date per the STR_TODATE mysql function as such:

 

<?php
$sql1 = "SELECT comic_id, STR_TO_DATE(cover_date, '%m/%d/%Y') AS cover_date FROM comics WHERE title='" . $row['title'] . "' AND issue_number > '" . $row['issue_number'] . "' ORDER BY cover_date, issue_number + 0 LIMIT 1";
?>

 

I was able to fix the date order using the function in the following statement:

 

$sql = mysql_query("SELECT STR_TO_DATE(cover_date, '%m/%d/%Y') AS cover_date FROM comics ORDER BY cover_date LIMIT 0, 245") or die(mysql_error());
while ($row = mysql_fetch_array($sql)) {
echo "1. ".$row['cover_date'];

 

But when I tried to apply that function to the bigger query (the first code box above) it won't increment the issue number by one and then the next date... I am going bonkers.

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.