gpt4 book ai didi

PHP Mysql更新表中Id的位置和父级

转载 作者:行者123 更新时间:2023-11-29 02:01:24 25 4
gpt4 key购买 nike

请给我一个包含三列的表格:

id   (this is the id of my table)
parent (this is the parent)
position (and this is the position)

我有这个数组 $_arr['menu'] :

array (size=3)
13 =>
array (size=1)
'parent' => string '0' (length=1)
14 =>
array (size=1)
'parent' => string '13' (length=2)
25 =>
array (size=1)
'parent' => string '0' (length=1)

我想用数组 $_arr['menu'] 中的那些值更新 may 表。

我想象做一些类似但在一个查询中的事情(可能使用 case in):

UPDATE table SET position = 1, parent = 0 WHERE id = 13;

UPDATE table SET position = 2, parent = 13 WHERE id = 14;

UPDATE table SET position = 3, parent = 0 WHERE id = 25;

请高手,如何从该数组中获取这些值以及如何进行更新!

非常感谢

最佳答案

我不确定我是否收到了您的问题,但我会写下我的代码片段。 (我不太记得php的语法,也没有测试过,但我认为它表达了我想说的)

try {
$db->beginTransaction();
$increment = 0;
foreach ($arr as $id => $innerArray) {
$db->query("UPDATE table SET position = ".(++$increment).", parent = ".$innerArray['parent']." WHERE id =".$id);
}
$db->commit();
} catch (Exception $e) {
$db->rollback();
}

关于PHP Mysql更新表中Id的位置和父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14014478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com