php - 用爆炸分离字符串的各个部分
问题描述
如果使用选中该复选框,我正在尝试从 Wordpress 数据库中获取并显示一些数据
echo "<input type='checkbox' name='productinfo[]' value='" .$item->get_name() .$item->get_quantity() . $item->get_total() ."'>";
并使用以下命令打印出来: $test = $_POST['productinfo'];
for($i=0; $i < sizeof($test); $i++) {
list($name, $quantity, $total) = explode(",", $test[$i]);
echo "Name- ".$name;
echo "<br>";
echo "Quantity ".$quantity;
echo "<br>";
echo "Total ".$total;
echo "<br>";
echo "<br/>";
}
但问题是数量和总计在我无法打印单独的字符串中粘在一起,有没有办法将它们彼此分开,它目前打印出来是这样的,最后 1 是数量,279 是全部的。
解决方案
您可以尝试用不常见的字符分隔字符串,例如,如果产品名称中不包含“_”(或“|”例如)
echo "<input type='checkbox' name='productinfo[]'
value='" .$item->get_name() . "_" . $item->get_quantity() . "_" . $item->get_total() ."'>";
这样,您可以使用explode("_", $checkbox_value)
(PHP) 或checkbox_value.split("_")
(JS)检索不同的值
推荐阅读
- bugzilla - 将 bugzilla 从一台机器迁移到另一台机器后,用户无法登录
- java - 如何区分 kotlin 的类继承(在 java 中扩展)和接口实现(在 中实现)这里 kotlin 使用 (:) 两者?
- visual-studio - 如何将 Sublime Text 语法高亮文件转换为 Visual Studio 的 textmate 语法?
- java - Spring 依赖注入在 ConstraintValidator 中不起作用
- c - 循环可以在每次运行时创建一个新的、不同的数组吗?
- php - 尝试使用 Ajax 数据库调用填充动态表单字段
- xslt - 在包含反斜杠的“测试”字符串中使用 XSLT 变量
- .net - 通过 Unix 时间戳对 System.DateTime 实例的往返转换延迟 1 小时
- arrays - 查找其值接近另一个向量的元素的向量位置
- php - Laravel groupBy 和原始查询