PHP - Automatizar igualación a cero

 
Vista:
Imágen de perfil de Lucas
Val: 87
Ha aumentado 1 puesto en PHP (en relación al último mes)
Gráfica de PHP

Automatizar igualación a cero

Publicado por Lucas (46 intervenciones) el 01/05/2020 22:08:38
Les comento, estoy pasando a una base de datos mysql un formulario con 20 valores numéricos, los cuales se transfieren entre 4 filas (4 por cada fila). Quiero que cada vez que se deje un valor vacío se complete automáticamente con un 0 a la hora de pasarse a la base de datos, y si es posible evitar copiar 20 veces un mismo código. Los elementos están llamados "elemento1", "elemento2", "elemento3", etc... hasta el "elemento20"

Se me había ocurrido usar un bucle como el siguiente pero no funcionó:
1
2
3
4
5
6
for($i=1;$i<21;$i++){
    $elementogeneral = "$" . "elemento" . $i;
    if(is_null($elementogeneral)){
        $elementogeneral=0;
    }
}

aunque era dudoso que funcione desde un primer momento jaja

A continuación tambien les adjunto el codigo completo por si puede llegar a servir:

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link href="formulario.css" rel="stylesheet">
    <meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no">
    <title>Formulario</title>
    <link href="https://fonts.googleapis.com/css?family=Baloo+Bhaina+2&display=swap" rel="stylesheet">
</head>
<body>
<div class="total">
    <div calss="titulo">
        <h1>Corte</h1>
    </div>
    <form name="form" method="post" action="formulario.php">
        <div class="comunes">
            <label for="fecha">Fecha</label>
                <input type="date" name="fecha" >
            <label for="lote">Lote</label>
                <input name="lote" type="text" autocomplete="off">
        </div>
        <div class="cuerpo">
            <div class="nombre">
            <label for="nombre">Nombre</label><input type="text" name="nombre"autocomplete="off">
            </div>
            <input type="number" name="elemento1">
            <input type="number" name="elemento2">
            <input type="number" name="elemento3">
            <input type="number" name="elemento4">
            <input type="number" name="elemento5">
            <div class="nombre">
            <label for="nombre">Nombre</label><input type="text" name="nombre2"autocomplete="off">
            </div>
            <input type="number" name="elemento6">
            <input type="number" name="elemento7">
            <input type="number" name="elemento8">
            <input type="number" name="elemento9">
            <input type="number" name="elemento10">
            <div class="nombre">
            <label for="nombre">Nombre</label><input type="text" name="nombre3"autocomplete="off">
            </div>
            <input type="number" name="elemento11">
            <input type="number" name="elemento12">
            <input type="number" name="elemento13">
            <input type="number" name="elemento14">
            <input type="number" name="elemento15">
            <div class="nombre">
            <label for="nombre">Nombre</label><input type="text" name="nombre4"autocomplete="off">
            </div>
            <input type="number" name="elemento16">
            <input type="number" name="elemento17">
            <input type="number" name="elemento18">
            <input type="number" name="elemento19">
            <input type="number" name="elemento20">
 
            <input type="submit" value="Listo" name="listo">
        </div>
    </div>
    </from>
</div>
    <!-- -------------------------------------------------------------------------------------------------------------------- -->
    <?php
if(isset($_POST["listo"])){
$nombre=$_POST["nombre"];
$fecha=$_POST["fecha"];
$elemento1=$_POST["elemento1"];
$elemento2=$_POST["elemento2"];
$elemento3=$_POST["elemento3"];
$elemento4=$_POST["elemento4"];
$elemento5=$_POST["elemento5"];
$lote=$_POST["lote"];
$suma=$elemento1+$elemento2+$elemento3+$elemento5+$elemento4;
 
$conexion= new mysqli ("localhost", "id12035304_aks4", "CS9&Zs^wP\cX%FE", "id12035304_planillas");
 
if($conexion->errno){
    echo "fallo en la conexión" . $conexion->connect_errno;
}
 
$conexion->set_charset("utf8");
 
$insertar="INSERT INTO DATOS(FECHA, LOTE, NOMBRE, ELEMENTO1, ELEMENTO2, ELEMENTO3, ELEMENTO4, ELEMENTO5, SUMA) VALUES('$fecha', '$lote', '$nombre', $elemento1, $elemento2, $elemento3,$elemento4,$elemento5, $elemento1+$elemento2+$elemento3+$elemento4+$elemento5)";
 
$conexion->query($insertar);
 
if($conexion->errno){
    echo  $conexion->connect_errno . "<br>";
}
 
    $nombre=$_POST["nombre2"];
    $fecha=$_POST["fecha"];
    $elemento1=$_POST["elemento6"];
    $elemento2=$_POST["elemento7"];
    $elemento3=$_POST["elemento8"];
    $elemento4=$_POST["elemento9"];
    $elemento5=$_POST["elemento10"];
    $lote=$_POST["lote"];
    $suma=$elemento1+$elemento2+$elemento3+$elemento5+$elemento4;
    $insertar="INSERT INTO DATOS(FECHA, LOTE, NOMBRE, ELEMENTO1, ELEMENTO2, ELEMENTO3, ELEMENTO4, ELEMENTO5, SUMA) VALUES('$fecha', '$lote', '$nombre', $elemento1, $elemento2, $elemento3,$elemento4,$elemento5, $elemento1+$elemento2+$elemento3+$elemento4+$elemento5)";
    $conexion->query($insertar);
 
    if($conexion->errno){
        echo  $conexion->connect_errno . "<br>";
    }
        $nombre=$_POST["nombre3"];
        $fecha=$_POST["fecha"];
        $elemento1=$_POST["elemento11"];
        $elemento2=$_POST["elemento12"];
        $elemento3=$_POST["elemento13"];
        $elemento4=$_POST["elemento14"];
        $elemento5=$_POST["elemento15"];
        $lote=$_POST["lote"];
        $suma=$elemento1+$elemento2+$elemento3+$elemento5+$elemento4;
 
        $insertar="INSERT INTO DATOS(FECHA, LOTE, NOMBRE, ELEMENTO1, ELEMENTO2, ELEMENTO3, ELEMENTO4, ELEMENTO5, SUMA) VALUES('$fecha', '$lote', '$nombre', $elemento1, $elemento2, $elemento3,$elemento4,$elemento5, $elemento1+$elemento2+$elemento3+$elemento4+$elemento5)";
        $conexion->query($insertar);
 
        if($conexion->errno){
            echo  $conexion->connect_errno . "<br>";
        }
            $nombre=$_POST["nombre4"];
            $fecha=$_POST["fecha"];
            $elemento1=$_POST["elemento16"];
            $elemento2=$_POST["elemento17"];
            $elemento3=$_POST["elemento18"];
            $elemento4=$_POST["elemento19"];
            $elemento5=$_POST["elemento20"];
            $lote=$_POST["lote"];
            $suma=$elemento1+$elemento2+$elemento3+$elemento5+$elemento4;
            $insertar="INSERT INTO DATOS(FECHA, LOTE, NOMBRE, ELEMENTO1, ELEMENTO2, ELEMENTO3, ELEMENTO4, ELEMENTO5, SUMA) VALUES('$fecha', '$lote', '$nombre', $elemento1, $elemento2, $elemento3,$elemento4,$elemento5, $elemento1+$elemento2+$elemento3+$elemento4+$elemento5)";
            $conexion->query($insertar);
 
            if($conexion->errno){
                echo  $conexion->connect_errno . "<br>";
            }
            }
 
?>
</body>
</html>
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
Imágen de perfil de Lucas
Val: 87
Ha aumentado 1 puesto en PHP (en relación al último mes)
Gráfica de PHP

Automatizar igualación a cero

Publicado por Lucas (46 intervenciones) el 01/05/2020 22:43:55
Actualización: También intente utilzar esta función después de cada parrafo pero no funcionó
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function remplazo0($elemento1, $elemento2, $elemento3, $elemento4, $elemento5){
if($elemento1 == ""){
    $elemento1 = 0;
}
if($elemento2 == ""){
    $elemento2 = 0;
}
if($elemento3 == ""){
    $elemento3 = 0;
}
if($elemento4 == ""){
    $elemento4 = 0;
}
if($elemento5 == ""){
    $elemento5 = 0;
}}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Julio
Val: 2.007
Plata
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Automatizar igualación a cero

Publicado por Julio (830 intervenciones) el 01/05/2020 23:08:33
Hola.

¿Cuál es tu problema, lo de insertar el valor vacío? El bucle del principio está bien, pero tendrás que incorporar algo que te indique cuándo toca un valor vacío, porque la comprobación que haces es sobre una variable ($elementogeneral) que acabas de definir, con lo cual nunca será null. El tema es saber eso, cuándo consideras que toca un valor vacío.

-----------------------------
Aprende la programación desde una perspectiva básica y simplista en Programación Básica
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Lucas
Val: 87
Ha aumentado 1 puesto en PHP (en relación al último mes)
Gráfica de PHP

Automatizar igualación a cero

Publicado por Lucas (46 intervenciones) el 01/05/2020 23:53:50
Hola Julio, perdón pero no entendí bien que me quisiste decir, con "valor vació" me refiero a cuando el usuario da al botón de Submit sin haber llenado antes alguno de los valores numéricos!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Julio
Val: 2.007
Plata
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Automatizar igualación a cero

Publicado por Julio (830 intervenciones) el 02/05/2020 00:17:12
Disculpa entonces, pero viendo tu código no entiendo exactamente qué quieres hacer.

-----------------------------
Aprende la programación desde una perspectiva básica y simplista en Programación Básica
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Lucas
Val: 87
Ha aumentado 1 puesto en PHP (en relación al último mes)
Gráfica de PHP

Automatizar igualación a cero

Publicado por Lucas (46 intervenciones) el 02/05/2020 00:25:49
Es un código de 20 valores numéricos, pero quiero que cuando alguien deje uno de los valores sin completar, este se autocomplete con el valor 0 a la hora de pasarse a la base de datos!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Lucas
Val: 87
Ha aumentado 1 puesto en PHP (en relación al último mes)
Gráfica de PHP

Automatizar igualación a cero

Publicado por Lucas (46 intervenciones) el 02/05/2020 01:41:38
Solución del problema por si alguien alguna vez le pasa algo parecido o le interesa (Había que usa programación POO):
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link href="formulario.css" rel="stylesheet">
    <meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no">
    <title>Formulario</title>
    <link href="https://fonts.googleapis.com/css?family=Baloo+Bhaina+2&display=swap" rel="stylesheet">
</head>
<body>
<div class="total">
    <div calss="titulo">
        <h1>Corte</h1>
    </div>
    <form name="form" method="post" action="formulario2.php">
        <div class="comunes">
            <label for="fecha">Fecha</label>
                <input type="date" name="fecha" >
            <label for="lote">Lote</label>
                <input name="lote" type="text" autocomplete="off">
        </div>
        <div class="cuerpo">
            <div class="nombre">
            <label for="nombre">Nombre</label><input type="text" name="nombre"autocomplete="off">
            </div>
            <input type="number" name="elemento1">
            <input type="number" name="elemento2">
            <input type="number" name="elemento3">
            <input type="number" name="elemento4">
            <input type="number" name="elemento5">
            <div class="nombre">
            <label for="nombre2">Nombre</label><input type="text" name="nombre2"autocomplete="off">
            </div>
            <input type="number" name="elemento6">
            <input type="number" name="elemento7">
            <input type="number" name="elemento8">
            <input type="number" name="elemento9">
            <input type="number" name="elemento10">
            <div class="nombre">
            <label for="nombre3">Nombre</label><input type="text" name="nombre3"autocomplete="off">
            </div>
            <input type="number" name="elemento11">
            <input type="number" name="elemento12">
            <input type="number" name="elemento13">
            <input type="number" name="elemento14">
            <input type="number" name="elemento15">
            <div class="nombre">
            <label for="nombre4">Nombre</label><input type="text" name="nombre4"autocomplete="off">
            </div>
            <input type="number" name="elemento16">
            <input type="number" name="elemento17">
            <input type="number" name="elemento18">
            <input type="number" name="elemento19">
            <input type="number" name="elemento20">
 
            <input type="submit" value="Listo" name="listo">
        </div>
    </div>
    </from>
</div>
    <!-- -------------------------------------------------------------------------------------------------------------------- -->
    <?php
$conexion= new mysqli ("localhost", "root", "", "formulario");
if($conexion->errno){
    echo "fallo en la conexión" . $conexion->connect_errno;
}
$conexion->set_charset("utf8");
 
class envio{
var $nombre;
var $fecha;
var $elementoa;
var $elementob;
var $elementoc;
var $elementod;
var $elementoe;
var $lote;
    function __construct(){
$this->fecha=$_POST["fecha"];
$this->lote=$_POST["lote"];
}
 
    function set_prop($primero, $segundo, $tercero, $cuarto, $quinto, $nombre){
        $this->elementoa=$primero;
        $this->elementob=$segundo;
        $this->elementoc=$tercero;
        $this->elementod=$cuarto;
        $this->elementoe=$quinto;
        $this->nombre=$nombre;
        if($this->elementoa == ""){
            $this->elementoa=0;
        }
        if($this->elementob == ""){
            $this->elementob=0;
        }
        if($this->elementoc == ""){
            $this->elementoc=0;
        }
        if($this->elementod == ""){
            $this->elementod=0;
        }
        if($this->elementoe == ""){
            $this->elementoe=0;
        }
    }
 
    function enviar($elementoa, $elementob, $elementoc, $elementod ,$elementoe, $conexion,$lote, $fecha, $nombre){
        $insertar="INSERT INTO DATOS(FECHA, LOTE, NOMBRE, ELEMENTO1, ELEMENTO2, ELEMENTO3, ELEMENTO4, ELEMENTO5, SUMA) VALUES('$fecha', '$lote', '$nombre', $elementoa, $elementob, $elementoc,$elementod,$elementoe, $elementoa+$elementob+$elementoc+$elementod+$elementoe)";
        $conexion->query($insertar);
        if($conexion->errno){
            echo  $conexion->connect_errno . "<br>";
        }
    }
}
if(isset($_POST["listo"])){
$fila1= new envio();
$fila1->set_prop($_POST["elemento1"],$_POST["elemento2"],$_POST["elemento3"],$_POST["elemento4"],$_POST["elemento5"],$_POST["nombre"]);
$fila1->enviar($fila1->elementoa,$fila1->elementob,$fila1->elementoc,$fila1->elementod,$fila1->elementoe, $conexion, $fila1->lote, $fila1->fecha, $fila1->nombre);
 
$fila2= new envio();
$fila2->set_prop($_POST["elemento6"],$_POST["elemento7"],$_POST["elemento8"],$_POST["elemento9"],$_POST["elemento10"],$_POST["nombre2"]);
$fila2->enviar($fila2->elementoa,$fila2->elementob,$fila2->elementoc,$fila2->elementod,$fila2->elementoe, $conexion, $fila2->lote, $fila2->fecha, $fila2->nombre);
 
$fila3= new envio();
$fila3->set_prop($_POST["elemento11"],$_POST["elemento12"],$_POST["elemento13"],$_POST["elemento14"],$_POST["elemento15"],$_POST["nombre3"]);
$fila3->enviar($fila3->elementoa,$fila3->elementob,$fila3->elementoc,$fila3->elementod,$fila3->elementoe, $conexion, $fila3->lote, $fila3->fecha, $fila3->nombre);
 
$fila4= new envio();
$fila4->set_prop($_POST["elemento16"],$_POST["elemento17"],$_POST["elemento18"],$_POST["elemento19"],$_POST["elemento20"],$_POST["nombre4"]);
$fila4->enviar($fila4->elementoa,$fila4->elementob,$fila4->elementoc,$fila4->elementod,$fila4->elementoe, $conexion, $fila4->lote, $fila4->fecha, $fila4->nombre);
}
?>
</body>
</html>
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar