dongxing2030 2011-07-07 04:58
浏览 7
已采纳

PHP逻辑问题

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条回答 默认 最新

  • duanliao5995 2011-07-07 05:07
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题