dqluw20882 2010-01-30 02:07
浏览 36
已采纳

在我的网页上显示操纵数据库中条目的方式的最佳方法是什么?

I have a client I am building a small CMS for. One of the functions is that he would like to be able to pick the order in which images display on a page.

In my mySql Database I have a table called image_info with the fields:
index (auto incremented)
img_url
img_link

I was thinking of adding an additional field called img_order which would be an int that I would refer to to sort the data returned to my php script.

I'm not sure if that is the best way. I feel like I will have to be re numbering every entry in the database just to move one image up or down the list.

Is there a standard procedure for this type of function?

Thanks, -J

  • 写回答

2条回答 默认 最新

  • dpgu5074 2010-01-30 02:14
    关注

    I think that adding an img_order column is the easiest way, and it won't be hard be to renumber the fields. For instance, if you're adding a picture at img_order position 15, just insert it with that value, then run the following query:

    update image_info set img_order = img_order + 1 where img_order >= 15;
    

    If you're removing an existing image, you'd have to decrease the img_order values for all images after that one:

    update image_info set img_order = img_order - 1 where img_order >= 15;
    

    And if you're moving an image from one position to another, you'd have to increment the img_order for all images between the original and new position:

    update image_info set img_order = img_order + 1 where 
    img_order between (least($old_img_order, $new_img_order), 
    greatest($old_img_order, $new_img_order));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程