Ciprian Kis Ciprian Kis - 1 year ago 43
MySQL Question

Send array to mysql row and break it up into variables again

I have a table for clients that stores some basic info but in one row it also stores an array that looks something like this i.e.
"1-1, 2-1, 3-1, 6-1, 7-1, 16-1, 17-1, 18-1"

the commas separate the "options", the number before the dash is the "option" ID, and the number after after the dash is the "option's" "quantity". each client might have different "options" with different "quantities"

I have this so far but I have no idea where to go from here.

$out = "";
$id_client ="";
$id_client = $_SESSION['client_cod_card'];
$sql="SELECT * FROM customer WHERE client_cod_card='$id_client'";
$result = mysql_query ($sql);
while ($row = mysql_fetch_array($result)){

$in = $row['options'];
$out = str_replace(',', '<br />', $in);

echo $out;} ?>

and it gives me this









is there any way to echo that string into something like:

$option_id - $option_quantity

$option_id - $option_quantity

$option_id - $option_quantity

$option_id - $option_quantity

so I can use those numbers and get each option's info from another table and adjust the quantity.

I hope I managed to explain this somewhat understandable and I hope someone can give me a hand as this is the most frustrating thing I've gotten myself into.
Thank you all in advance!

Answer Source

You can use explode() to split up the value into its parts. First split the whole list on the commas, then split each option on the hyphens.

$option_array = explode(',', $row['options'];
foreach ($option_array as $opt) {
    list($option_id, $option_quantity) = explode('-', $opt);
    // Use $option_id and $option_quantity here