隐藏输入中的php迭代循环不迭代
这个脚本中有一个令人烦恼的小错误。我正在检查我的购物车物品并将它们传递到隐藏的输入中。 cart_id ($obj->id) 在 value="" 中运行良好,但为每个值提供唯一名称="" (cart_id_1、cart_id_2 等)的迭代循环没有迭代。
<?php
$pass_cart_q = "SELECT c.id FROM carts AS c WHERE c.user_session_id='$sid'";
$result = $mysqli->query($pass_cart_q);
$i = 1;
while ($obj = $result->fetch_object()) {
echo "<input type=\"hidden\" name=\"cart_id_".$i."\" value=\" .$obj->id. \"><br>";
$i = $i++;
}
mysqli_close();?>
每个名称字段都以 cart_id_1 的形式出现
I've got one nagging little bug in this script. I'm going through my cart items and passing them into hidden inputs. The cart_id ($obj->id) is working fine into the value="" but my iteration loop that gives each value a unique name="" (cart_id_1, cart_id_2 etc) is NOT iterating.
<?php
$pass_cart_q = "SELECT c.id FROM carts AS c WHERE c.user_session_id='$sid'";
$result = $mysqli->query($pass_cart_q);
$i = 1;
while ($obj = $result->fetch_object()) {
echo "<input type=\"hidden\" name=\"cart_id_".$i."\" value=\" .$obj->id. \"><br>";
$i = $i++;
}
mysqli_close();?>
Each name field is coming through as cart_id_1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这就是问题所在:
That's the problem just do:
请将
$i = $i++;
替换为$i++
。Please replace
$i = $i++;
with just$i++
.$i = $i++
会导致它字面上是这样的:“使$i
等于$i
然后将其加一”,但是$i
仍将保持不变。要解决此问题,只需将$i = $i++;
替换为$i++
即可。手动输入
What
$i = $i++
will cause it literally this: "make$i
equal to$i
and then increase it by one", but the$i
will still remain the same. To solve this, simply replace$i = $i++;
with$i++
.Manual Entry
您正在将递增的值分配给
$i
变量。因此它无法迭代。相反,您应该删除该赋值变量$i
并且它应该仅为$i++
you are assigning the incremented value to
$i
variable. and hence it is not able to iterate. instead you should remove that assignment variable$i
and it should only be$i++