PHP - guardas campos de matriz como registros independientes

 
Vista:

guardas campos de matriz como registros independientes

Publicado por alberto (3 intervenciones) el 23/06/2017 15:05:01
cordial saludo,

desde un servicio web services estoy recibiendo una cadena de valores donde cada campo esta separado por , y cada línea separada por ;
cuyo resultado es
16866051,P/C,99415,TZA123,23,MARMA,3,OK,2017-06-20 02:00:00;
16866051,P/C,99415,TZA123,21,LAGOS,3,OK,2017-06-20 04:31:00;
ahora para poder enviarlo a la tabla en MYSQL hago una captura en de estos datos con un textarea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
$valor = $_POST["valores"];
$valore = chop($valor);
$valores = nl2br($valore);
 
$array_datos = explode("<br />", $valores);
 
 
mysql_select_db($database_conexion, $conexion);
foreach ($array_datos as $valor) {
 
 
$tabla = 'Znombre_tabla';
$_GRABAR_SQL = "INSERT INTO $tabla (valores) VALUES ('".$valor."')";
mysql_query($_GRABAR_SQL);
 
}
?>
<html>
 
<head>
</head>
 
<body>
 
<table border="1" width="600" id="table1">
<tr>
<td>ID</td>
<td>VALOR</td>
</tr>
 
<?php
$_CONSULTA_SQL = "SELECT * FROM $tabla ORDER BY id ASC";
$_RESULT = mysql_query($_CONSULTA_SQL);
 
while ($registro = mysql_fetch_array($_RESULT)){
 
echo "
<tr> 
<td>".$registro['id']."</td>
<td>".$registro['valores']."</td>

</tr> 

";
$res = array_filter(explode(",", $registro['valores']));
//print_r($res);
}
echo "</table>";
?>
 
<form method="POST" action="prueba.php">
 
<!-- <p><textarea rows="20" name="valores" value="<? echo $result ?>" cols="40"></textarea></p> -->
<textarea rows="20" name="valores" value="" cols="40"><?php function multiexplode ($delimiters,$string) {
 
$ready = str_replace($delimiters, $delimiters[0], $string);
$launch = explode($delimiters[0], $ready);
return $launch;
}
 
$text = $result;
$exploded = multiexplode(array(",",";"),$text);
 
print_r($exploded);
 
//print_r(explode(',', $exploded, 2));
?></textarea>
<p><input type="reset" value="Restablecer" name="B2">&nbsp
<input type="submit" value="Enviar" name="B1"></p>
</form>

DONDE me devuelve un resultado como este
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Array
(
[0] => 16866051
[1] => P/C
[2] => 99415
[3] => TZA123
[4] => 23
[5] => MARMA
[6] => 3
[7] => OK
[8] => 2017-06-20 02:00:00
[9] => 16866051
[10] => P/C
[11] => 99415
[12] => TZA123
[13] => 21
[14] => LOS LAGOS
[15] => 3
[16] => OK
[17] => 2017-06-20 04:31:00
[18] =>
)
y así mismo hace la inserción en la tabla.

les solicito su ayuda en el sentido que necesito guardar los datos en la tabla para cada posición de la matriz en un campo en la tabla. es decir,
1
2
3
4
5
6
7
campo 1        campo2
[0] =>            16866051
[1] =>            P/C
[2] =>            99415
[3] =>            TZA123
[4] =>            23
[5] =>            MARMA

de antemano agradezco su ayuda
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder