add 2 columns to the table to store rank eg book_rank & book_past_rank
for mysql you could
SELECT book_code, book_sales FROM books ORDER BY book_sales DESC;
which would return a list of book_codes and their associated sales ordered by sales.
if you created the columns in the mysql table(or what database you use)
you could populate the new columns like this
<?
$sql = "SELECT book_code FROM books ORDER BY book_sales DESC";
$results = mysql_query($sql, your connection stuff);
$counter = 1;
while ($row = mysql_fetch_assoc($result))
{
$book_code = $row['book_code'];
$sql = "UPDATE books SET book_rank = \"$counter\", book_past_rank = \"$counter\" WHERE book_code = \"$book_code\"";
$counter++;
}
?>
that would populate the book_rank and book_past_rank columns initially
then to update book ranks
<?
$sql = "SELECT book_code FROM books ORDER BY book_sales DESC";
$results = mysql_query($sql, your connection stuff);
$counter = 1;
while ($row = mysql_fetch_assoc($result))
{
$book_code = $row['book_code'];
$sql = "UPDATE books SET book_past_rank = book_rank, book_rank=\"$counter\" WHERE book_code =\"$book_code\"";
}
?>
get the idea?