首页 > 解决方案 > 用爆炸分离字符串的各个部分

问题描述

如果使用选中该复选框,我正在尝试从 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 是全部的。它像这样打印出来

标签: phpwordpress

解决方案


您可以尝试用不常见的字符分隔字符串,例如,如果产品名称中不包含“_”(或“|”例如)

echo "<input type='checkbox' name='productinfo[]' 
      value='" .$item->get_name() . "_" . $item->get_quantity() . "_" . $item->get_total() ."'>";

这样,您可以使用explode("_", $checkbox_value)(PHP) 或checkbox_value.split("_")(JS)检索不同的值


推荐阅读