当 str_split 个字符不符合要求时,如何取消设置行结果

How do i unset row result when str_split characters is not upto required

在名为 promo 的 mysql 数据库 table 中,我在 [=17= 列中有 msgpriceqty ] 列中的行作为例子

code | msg          |  price  | qty
180  | weallwinthem |  150    | 7
180  | yeswellhappy |  520    | 15
180  | wearerunning |  100    | 10

如果所需的 msg str_split 未达到 qty 的要求,我如何取消设置 qty 的行结果?

E:g2

if msg split 是 we, al, lw, in, th, em 并且 qty 是 7 unset qty to 6 示例 3

we, al, lw, in, th, em qty 6 我试过:

$sql = mysqli_query($conn, "SELECT * FROM promo WHERE code='180' LIMIT 1");
while($row = mysqli_fetch_array($sql)) {
    $msg = $row["msg"];
    $qty = $row["qty"];
    $disp = str_split($msg, 2);
    for($b = 0; $b < 2; $b++) {
        if(strlen($disp[$b]) != $qty) {
            unset($qty[$b]);
            $amt += ($row["price"] * $qty);
            $so = "uncompleted";

        } else {

            $so = "$disp[$b]";
            echo "','$so $amt";
        }
    }

感谢阅读和影响我的解决方案

if msg split is we, al, lw, in, th, em and qty is 7 unset qty to 6

据我从你的第二个例子中了解到:

我们可以根据msg.

写一个"calculate"对Qty的函数
  1. 拆分消息
  2. 统计拼接字数
  3. 这应该是新的数量。 (return吧)

function getDesiredQtyBySplit($msg){
   $split = str_split($msg, 2);
   $count = count($split);
   return $count;
}

对于每一行都会运行这个函数,如果行的Qty是desiredQty - 不需要采取任何行动,否则,我们应该更新行的Qty列。

$sql = mysqli_query($conn, "SELECT * FROM promo WHERE code='180' LIMIT 1");
while($row = mysqli_fetch_array($sql)) {
    $msg = $row["msg"];
    $qty = $row["qty"];
    $desiredQty = getDesiredQtyBySplit($msg);
    if($desiredQty != $qty){
       //Using mysqli, UPDATE qty so it would be $desiredQty
       //Make sure that your condition (WHERE) related to this specific row. 
      //Usually there's a "Primary Key" (for instance: id) which is has a unique value.
    }
}