I have two arrays that come out of two select multiples that I want to combine to store them in mysql
I am using a foreach to go through one of them but I would like to know if there is the option to go through both and store them, so far this is the code I use.
foreach($_POST["dato"] as $Dato) {
foreach($_POST["dato2"] as $Dato2) {
$INuevo = Insertar_Datos("Codigos" ,"`que`,`idcosa`,`1`,`2`" , "'test','$Id_Numero','".$Dato."','".$Dato2."' ");
}
}
The thing is that I want to make the loop that first reads a data from the first array and then goes through the second inserting data from the second with a value from the first (with this in principle I have no problem once I know what the variables that I have to pass to the wanted). For example, suppose the data is names and surnames:
name = 1
surname = 1 from first name 1
surname = 2 of first name 1
surname = 3 of first name 1
name = 2
surname = 1 of name 2
surname = 2 of name 2
surname = 3 of name 2
and so on until the end of days, being able to have only two surnames or only one.
Is it possible or do I have to do another foreach within the foreach? maybe a for within the first foreach is better?
inside the Insert_Data function I have this:
function Insertar_Datos() { global $Conectar;
$Parametros = func_get_args();
$InDatos = "INSERT INTO `".$Parametros[0]."` (".$Parametros[1].") VALUES (".$Parametros[2].");";
$RDatos = mysqli_query($Conectar, $InDatos);
if (!$RDatos) { http_response_code(500); print(mysqli_error($Conectar)); } else { http_response_code(200); echo "ok"; }
return $RDatos;
}
and the select multiples are something like this:
<select name="dato[]" multiple="multiple">
<option value="1">Numero 1</option>
<option value="2">Numero 2</option>
<option value="3">Numero 3</option>
</select>
<select name="dato2[]" multiple="multiple">
<option value="1">Numero 1</option>
<option value="2">Numero 2</option>
<option value="3">Numero 3</option>
</select>
The structure of the table would be something like this:
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(10) | NO | PRI | NULL | |
| que | varchar(50) | YES | | NULL | |
| idcosa | int(10) | YES | | NULL | |
| 1 | varchar(200)| YES | | NULL | |
| 2 | varchar(200)| YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
idthing I bring it from another table and it is a number to identify where it comes from, but that number is well kept.
The thing is that for some reason when data2 has more than one value it doesn't work and I don't know what's wrong with me :_(
To calculate the
producto cartesiano
of both arrays you could use the following function:show
Your code could result in the following:
From what I understand you have:
$_POST['datos']
and$_POST['datos2']
and you want to combine them to loop through them. Well, you could combine them witharray_merge()
: http://php.net/manual/es/function.array-merge.phpSee if it works for you that way.