$get_data = "SELECT * FROM stats WHERE x < max_x and y < max_y";
$get_data_query = mysqli_query($conn,$get_data);
while ($data= mysqli_fetch_assoc($get_data_query)) {
$update = "UPDATE stats SET
x = x + (max_x / 11.11111111111111),
y = y + (max_y / 11.11111111111111)
WHERE id = '".$data['id']."' ";
mysqli_query($conn,$update); }
在上面的数据中,我有一个代码来确定每个用户的max_x和max_y值
我正在尝试将以上代码用作“计划任务”
但是有一个问题。
让我们考虑案例1:
max_x = 600,max_y = 100
如果我运行此代码11次,它将看起来像这样
==> X/Y
1 ==> 54/9
2 ==> 108/18
3 ==> 162/27
4 ==> 216/36
5 ==> 270/45
6 ==> 324/54
7 ==> 378/63
8 ==> 432/72
9 ==> 486/81
10 ==> 540/90
11 ==> 594/99
情况2:max_x = 1200,max_y = 200
...11 ==> 1188/198
那么在这种情况1下,如何确保添加X = +6和Y = +1
在案例2中X = +12且Y = +2
与之类似,我如何确保添加“剩余”值而不超过max_value
答案 0 :(得分:1)
在php中执行计算部分,在该部分中更容易(更合适)地实现程序逻辑,然后将新值简单地传递给更新脚本。
$get_data = "SELECT * FROM stats WHERE x < max_x and y < max_y";
$get_data_query = mysqli_query($conn,$get_data);
while ($data= mysqli_fetch_assoc($get_data_query)) {
// added one-liners if you want a bit cleaner code
// $x_new = $x + ($max_x / 11.11111111111111) < $max_x ? x + (max_x / 11.11111111111111) : $max_x;
// $y_new = $y + ($max_y / 11.11111111111111) < $max_y ? y + (max_y / 11.11111111111111) : $max_x;
$x_new = $x + ($max_x / 11.11111111111111);
if ($x_new > $max_x) {
$x_new = $max_x;
}
$y_new = $y + ($max_y / 11.11111111111111);
if ($y_new > $max_y) {
$y_new = $max_y;
}
$update = "UPDATE stats SET
x = '".$x_new."',
y = '".$y_new."'
WHERE id = '".$data['id']."' ";
mysqli_query($conn,$update); }