doutun9179 2017-03-28 09:52
浏览 4
已采纳

php pdo递减高于删除值的值

Hello I am a little stuck on this problem can anyone lend a hand. I have a table which has a column with positions with which they are sorted and what I am trying to do is that when the delete button is clicked for one row the row's which have a position value higher than the one that got deleted to get decremented. As in this

 //   1
 //   2 //now we delete this 
 //   3
 //   4
 //   and this get's updated with this
 //    1
 //    2
 //    3

<?php 
require_once('connect.php');
$name = get_option('selectedSlide');
$position = get_option('selectedSlideId'); 
    try{
        global $db;
        if(!get_option('selectedSlide')){
        $error.="No slide selected.";
        echo $error; die;
        }
        else{
        $inserts = $db->prepare("DELETE FROM slider_images WHERE slider_image_name = :names");
        $inserts->bindParam(':names', $name);
        $inserts->execute();
        $incrementAllBehindDeleted = $db->prepare("SELECT slider_image_position FROM slider_images WHERE slider_image_position>:position");
        $incrementAllBehindDeleted->bindParam(':position', $position);
        $incrementAllBehindDeleted->execute();
        die;
        }
    }
    catch(Exception $e){
        echo $e->getMessage();
        exit;
    }
?>
  • 写回答

1条回答 默认 最新

  • dongli8979 2017-03-28 10:10
    关注

    Generally, I agree with Niet the Dark Absol's comment that you should not need to worry about a gap, as long as the order of the numbers is fine.

    However, if you insist on it, then I would just simple do an update without a select to decrease the position of the subsequent images:

    UPDATE slider_images SET slider_image_position=slider_image_position-1 WHERE slider_image_position>:position
    

    However, if you have multiple image sliders, then you have to include the image slider's identifier in the update's where clause, which images are being reordered.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起