Codeigniter:当值为’0’时停止while函数。

huangapple go评论58阅读模式
英文:

Codeigniter : stop the while function when value '0'

问题

如何在我已经将数量更新为'0'后停止“while”循环?

$check = $this->db->query("SELECT * 
    FROM tmp_headik2 
    WHERE no_bukti = '$no_bukti' AND qty > '0'");

while($check->num_rows() > 0)
{
    $this->db->query("UPDATE tmp_headik2 
        SET qty = '0'
        WHERE no_bukti = '$no_bukti'");
}

return $check->result_array();

"while"函数仍然循环。

英文:

How the "while" can stopped when i'm already update qty to '0' ?

$check = $this->db->query("SELECT * 
    FROM tmp_headik2 
    WHERE no_bukti = '$no_bukti' AND qty > '0'");

while($check->num_rows() > 0)
{
    $this->db->query("UPDATE tmp_headik2 
        SET qty = '0'
        WHERE no_bukti = '$no_bukti'");
}

return $check->result_array();

The while function still loop

答案1

得分: 1

欢迎来到Stack Overflow Codeigniter:当值为’0’时停止while函数。

我对$check->num_rows()正在做什么有一点疑问,但我认为在你的情况下,可能不真正需要使用while

可能你可以用一个if来替换它吗?因为我缺少一些关于你的代码正在做什么的上下文,所以这可能不正确。请告诉我。

基本上,while会执行内部的代码,直到条件$check->num_rows() > 0false。这意味着你的$this->db->query(...)调用会一遍又一遍地执行,直到条件不再满足。

我还觉得有点奇怪的是你的$this->db->query(...)调用总是相同的,所以我会假设在第一次执行后再次调用它不会做任何不同的事情。

你可以再检查一下这个对你是否有效?

if ($check->num_rows() > 0)
{
    $this->db->query("UPDATE tmp_headik2 
                        SET 
                            qty = '0'
                        WHERE no_bukti = '$no_bukti' 
                    ");
}

return $check->result_array();
英文:

welcome to stackoverflow Codeigniter:当值为’0’时停止while函数。

I am missing a bit of information on what $check->num_rows() is doing, but I believe that you don't really need a while in your case.

Probably you can replace it with an if? Since I am missing a bit of context of what your code is doing this might not be correct. Let me know.

Basically the while will be executing the code inside until the condition $check->num_rows() > 0 is false. This means that your $this->db->query(...) call will be done again and again until then.

I find it also a bit strange that your $this->db->query(...) call is the same always, so I would assume that after the first time it is executed calling it a second time would do anything else.

Can you double check if this works for you?

            if( $check->num_rows() > 0 )
            {
                $this->db->query("UPDATE tmp_headik2 
                                        SET 
                                            qty = '0'
                                        WHERE no_bukti = '$no_bukti' 
                                    ");
            }

            return $check->result_array();`

huangapple
  • 本文由 发表于 2023年6月22日 10:45:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528300.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定