PHP - Sumar valores de un tabla dinamica

 
Vista:
sin imagen de perfil

Sumar valores de un tabla dinamica

Publicado por Manuel (2 intervenciones) el 24/03/2022 18:37:14
Hola buenas tades.

Por favor su apoyo con lo siguiente:

Tengo un formulario que me devuelve los datos almacenado en un bd en mysql filtrador por el nombre de usuario. Todo funciona bien.

Lo que no logro realizar es que se muestre la suma de los campos totals y totalbs y que se muestre al final de la consulta, lo que me muestra es 0 en la esquina superior izquierda

Abajo mi codigo. Apenas empiezo con esto de php y mysql me disculpan el poco orden.

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
141
142
143
144
145
146
147
148
149
150
151
<?php require_once('Connections/EDD.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
 
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
 
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
 
$maxRows_consulta = 20;
$pageNum_consulta = 0;
if (isset($_GET['pageNum_consulta'])) {
  $pageNum_consulta = $_GET['pageNum_consulta'];
}
$startRow_consulta = $pageNum_consulta * $maxRows_consulta;
 
$varusuario_consulta = "-1";
if (isset($_GET['usuario'])) {
  $varusuario_consulta = $_GET['usuario'];
}
mysql_select_db($database_EDD, $EDD);
$query_consulta = sprintf("SELECT usuario, `date`, telefono, producto, cantidad, piso, totald, totalbs, preciop FROM t_pedidos WHERE usuario LIKE %s ORDER BY `date` DESC", GetSQLValueString("%" . $varusuario_consulta . "%", "text"));
$query_limit_consulta = sprintf("%s LIMIT %d, %d", $query_consulta, $startRow_consulta, $maxRows_consulta);
$consulta = mysql_query($query_limit_consulta, $EDD) or die(mysql_error());
$row_consulta = mysql_fetch_assoc($consulta);
 
if (isset($_GET['totalRows_consulta'])) {
  $totalRows_consulta = $_GET['totalRows_consulta'];
} else {
  $all_consulta = mysql_query($query_consulta);
  $totalRows_consulta = mysql_num_rows($all_consulta);
}
$totalPages_consulta = ceil($totalRows_consulta/$maxRows_consulta)-1;
?><style type="text/css">
<!--
body {
	margin-left: 3cm;
	margin-top: 2cm;
	margin-right: 3cm;
	margin-bottom: 2cm;
	background-color: #CCCCCC;
}
.Estilo2 {font-size: 18px}
-->
</style><form name="form1" method="get" action="dp.php?usuario=usuario">
  <label></label><label></label>
  <p> </p>
  <table width="1105" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td align="center" valign="middle" nowrap bgcolor="#CC99FF">Buscar deuda</td>
      <td align="center" valign="middle" nowrap bgcolor="#CC99FF"><input type="text" name="usuario" id="usuario"></td>
      <td align="center" valign="middle" nowrap bgcolor="#CC99FF"><input type="submit" name="button" id="button" value="Consultar">
      <input name="volver" type="button" id="volver" onClick="location='cp.php'" value="Volver"></td>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
    </tr>
    <tr>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
    </tr>
    <tr>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
    </tr>
    <tr>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
      <td align="center" valign="middle" nowrap> </td>
    </tr>
    <tr>
      <td height="30" align="center" valign="middle" nowrap bordercolor="#333333" bgcolor="#CC99FF"><span class="Estilo2">Usuario</span></td>
      <td align="center" valign="middle" nowrap bordercolor="#333333" bgcolor="#CC99FF"><span class="Estilo2">Fecha</span></td>
      <td align="center" valign="middle" nowrap bordercolor="#333333" bgcolor="#CC99FF"><span class="Estilo2">Teléfono</span></td>
      <td align="center" valign="middle" nowrap bordercolor="#333333" bgcolor="#CC99FF"><span class="Estilo2">Producto</span></td>
      <td align="center" valign="middle" nowrap bordercolor="#333333" bgcolor="#CC99FF"><span class="Estilo2">Cantidad</span></td>
      <td align="center" valign="middle" nowrap bordercolor="#333333" bgcolor="#CC99FF"><span class="Estilo2">Piso</span></td>
      <td align="center" valign="middle" nowrap bordercolor="#333333" bgcolor="#CC99FF"><span class="Estilo2">Total en $</span></td>
      <td align="center" valign="middle" nowrap bordercolor="#333333" bgcolor="#CC99FF"><span class="Estilo2">Total en Bs.</span></td>
      <td align="center" valign="middle" nowrap bordercolor="#333333" bgcolor="#CC99FF"><span class="Estilo2">Precio Producto</span></td>
    </tr>
    <?php do { ?>
      <tr>
        <td align="center" valign="middle" nowrap bordercolor="#333333" bgcolor="#8BF8F8"><?php echo $row_consulta['usuario']; ?></td>
        <td align="center" valign="middle" nowrap bordercolor="#333333" bgcolor="#8BF8F8"><?php echo $row_consulta['date']; ?></td>
        <td align="center" valign="middle" nowrap bordercolor="#333333" bgcolor="#8BF8F8"><?php echo $row_consulta['telefono']; ?></td>
        <td align="center" valign="middle" nowrap bordercolor="#333333" bgcolor="#8BF8F8"><?php echo $row_consulta['producto']; ?></td>
        <td align="center" valign="middle" nowrap bordercolor="#333333" bgcolor="#8BF8F8"><?php echo $row_consulta['cantidad']; ?></td>
        <td align="center" valign="middle" nowrap bordercolor="#333333" bgcolor="#8BF8F8"><?php echo $row_consulta['piso']; ?></td>
        <td align="center" valign="middle" nowrap bordercolor="#333333" bgcolor="#8BF8F8"><?php echo $row_consulta['totald']; ?></td>
        <td align="center" valign="middle" nowrap bordercolor="#333333" bgcolor="#8BF8F8"><?php echo $row_consulta['totalbs']; ?></td>
        <td align="center" valign="middle" nowrap bordercolor="#333333" bgcolor="#8BF8F8"><?php echo $row_consulta['preciop']; ?></td>
 
      </tr>
      <?php } while ($row_consulta = mysql_fetch_array ($consulta));{
	   $total= 0;
	   $total = $total + $consulta['totald'];
    }
    echo $total;
	   ?>
  </table>
  <p> </p>
</form>
<?php
mysql_free_result($consulta);
?>
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 javier
Val: 1.542
Bronce
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Sumar valores de un tabla dinamica

Publicado por javier (547 intervenciones) el 24/03/2022 20:42:45
Hola

si puedes haz un print_r() a lo que te devuelve la consulta y lo pegas aqui

a ver que datos te devuelve

Saludos
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
sin imagen de perfil

Sumar valores de un tabla dinamica

Publicado por Manuel (2 intervenciones) el 28/03/2022 16:51:00
Me sigue mostrando 0

<?php }
while ($row_consulta = mysql_fetch_array ($consulta));{
$total= 0;
$total = $total + $consulta['totald']; // Sumar variable $total + resultado de la consulta
print_r($total);
}
?>


2022-03-28_10-49-20
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 javier
Val: 1.542
Bronce
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Sumar valores de un tabla dinamica

Publicado por javier (547 intervenciones) el 29/03/2022 17:57:34
Hola

saca la variable $total fuera del bucle y la pones antes.

A cada vuielta del bucle la vuelves a poner a cero
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