1

I have a sql table images which has columns id,imagePath,caption``count.

To view those images what i do is, I ask for the page number and query the iamge

$limit=$page-1;
$sel=$mysqli->prepare("SELECT id,imagePath,caption,count from images".
                        " order by id desc limit ".$limit.",1");

This shows images correctly.

But i have another function which shows 4 random thumbnail of that images out of the total images from the database.With those images i also give a href tag so that when a user clicks on that link he is taken to the page of that image.

Now the problem is with those links. Suppose i have images like these in the database

id=1
id=2
id=3
id=4
id=5

To find the page number of the specific id what i do is

$newid=$idOfTheCurrentImage-($idOfTheLastImage-1);

Using this logic i get

id=1  page=5
id=2  page=4
id=3  page=3
id=4  page=2
id=5  page=1

This gives correct results if all the ids are in order. But if anyone of the id is deleted this logic fails.

Is there any other solution?

2
  • 1
    Why don't you just pass id instead of page (or both)? That would be easier and faster:SELECT id,imagePath,caption,count from images where id = $id Commented Jul 7, 2011 at 5:32
  • That would show the images in ascending order i want to show the recently added image first and so on. Commented Jul 7, 2011 at 6:38

2 Answers 2

1

You could do the following:

  • Adding another field, a 'sequential number' , so you have pairs ($id, $seq_number), and do your calculations as you did with $seq_number. Don't forget to update the sequential number field it on delete.

  • Get all the id's on an array, and calculate over there. In this option, the $id value is not used in the calculations, you use only the array indexes.

Hope this helps. Cheers

Sign up to request clarification or add additional context in comments.

1 Comment

Thnaks will try it out and,soon let you know :)
1

I would suggest using something other than a running counter as the page-denominator. As you say, it is subject to change, or fail if a number is missing in between.

Your images have a caption. You could create a "slug" from that and use that as your page-denominator.

No more quirky maths or murky logic when creating links, no more disappearing pages.

1 Comment

Hey nice idea,Will surely use this in my future projects Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.