phpstringimplodedelimited

Join string values with a comma


I'm trying to get and display some data from the Wordpress database if the checkbox is checked using

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

and printing it out using:

$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/>";
}

but the issue is that quantity and total are stuck together in a string at I cannot print the separate, is there a way to separate them from each other, it prints out like this currently, at the end 1 being the quantity and 279 being the total. it prints out like this


Solution

  • You could try separating the strings with an unusual character, for example the '_' if it isn't contained in the name of the product (or the "|" for example)

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

    This way you can retrieve the different values using explode("_", $checkbox_value)(PHP) or checkbox_value.split("_") (JS)