PHP - Suma de tres Tablas en BD - PHPMyAdmin

 
Vista:
Imágen de perfil de Robinson

Suma de tres Tablas en BD - PHPMyAdmin

Publicado por Robinson (41 intervenciones) el 16/09/2015 04:56:00
Amigos tengo el siguiente inconveniente, vamos a ver quien puede echarme una mano,

Quisiera poder sumar tres tablas (con la misma estructura) las cuales tengo en una base de datos y mostrar los resultados en una tabla de totales tal y como lo tengo en un excel:

Les envio por archivo adjunto:
Imagen de lo que tengo en excel
Imagen lo que tengo realizado en PHP
Código de lo que tengo hasta el momento:

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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php require_once('../Connections/local.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  if (PHP_VERSION < 6) {
    $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;
}
}
 
$currentPage = $_SERVER["PHP_SELF"];
 
// -------------------------------LLAMADO A TABLA 01----------------------------------------- //
 
$maxRows_gest_cni_andes_merida = 10;
$pageNum_gest_cni_andes_merida = 0;
if (isset($_GET['pageNum_gest_cni_andes_merida'])) {
  $pageNum_gest_cni_andes_merida = $_GET['pageNum_gest_cni_andes_merida'];
}
$startRow_gest_cni_andes_merida = $pageNum_gest_cni_andes_merida * $maxRows_gest_cni_andes_merida;
 
mysql_select_db($database_local, $local);
$query_gest_cni_andes_merida = "SELECT * FROM gcni_andes_merida";
$query_limit_gest_cni_andes_merida = sprintf("%s LIMIT %d, %d", $query_gest_cni_andes_merida, $startRow_gest_cni_andes_merida, $maxRows_gest_cni_andes_merida);
$gest_cni_andes_merida = mysql_query($query_limit_gest_cni_andes_merida, $local) or die(mysql_error());
$row_gest_cni_andes_merida = mysql_fetch_assoc($gest_cni_andes_merida);
 
if (isset($_GET['totalRows_gest_cni_andes_merida'])) {
  $totalRows_gest_cni_andes_merida = $_GET['totalRows_gest_cni_andes_merida'];
} else {
  $all_gest_cni_andes_merida = mysql_query($query_gest_cni_andes_merida);
  $totalRows_gest_cni_andes_merida = mysql_num_rows($all_gest_cni_andes_merida);
}
$totalPages_gest_cni_andes_merida = ceil($totalRows_gest_cni_andes_merida/$maxRows_gest_cni_andes_merida)-1;
 
$queryString_gest_cni_andes_merida = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_gest_cni_andes_merida") == false &&
        stristr($param, "totalRows_gest_cni_andes_merida") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_gest_cni_andes_merida = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_gest_cni_andes_merida = sprintf("&totalRows_gest_cni_andes_merida=%d%s", $totalRows_gest_cni_andes_merida, $queryString_gest_cni_andes_merida);
 
// -------------------------------LLAMADO A TABLA 02----------------------------------------- //
 
$maxRows_gest_cni_andes_san_cristobal = 10;
$pageNum_gest_cni_andes_san_cristobal = 0;
if (isset($_GET['pageNum_gest_cni_andes_san_cristobal'])) {
  $pageNum_gest_cni_andes_san_cristobal = $_GET['pageNum_gest_cni_andes_san_cristobal'];
}
$startRow_gest_cni_andes_san_cristobal = $pageNum_gest_cni_andes_san_cristobal * $maxRows_gest_cni_andes_san_cristobal;
 
mysql_select_db($database_local, $local);
$query_gest_cni_andes_san_cristobal = "SELECT * FROM gcni_andes_san_cristobal";
$query_limit_gest_cni_andes_san_cristobal = sprintf("%s LIMIT %d, %d", $query_gest_cni_andes_san_cristobal, $startRow_gest_cni_andes_san_cristobal, $maxRows_gest_cni_andes_san_cristobal);
$gest_cni_andes_san_cristobal = mysql_query($query_limit_gest_cni_andes_san_cristobal, $local) or die(mysql_error());
$row_gest_cni_andes_san_cristobal = mysql_fetch_assoc($gest_cni_andes_san_cristobal);
 
if (isset($_GET['totalRows_gest_cni_andes_san_cristobal'])) {
  $totalRows_gest_cni_andes_san_cristobal = $_GET['totalRows_gest_cni_andes_san_cristobal'];
} else {
  $all_gest_cni_andes_san_cristobal = mysql_query($query_gest_cni_andes_san_cristobal);
  $totalRows_gest_cni_andes_san_cristobal = mysql_num_rows($all_gest_cni_andes_san_cristobal);
}
$totalPages_gest_cni_andes_san_cristobal = ceil($totalRows_gest_cni_andes_san_cristobal/$maxRows_gest_cni_andes_san_cristobal)-1;
 
$queryString_gest_cni_andes_san_cristobal = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_gest_cni_andes_san_cristobal") == false &&
        stristr($param, "totalRows_gest_cni_andes_san_cristobal") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_gest_cni_andes_san_cristobal = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_gest_cni_andes_san_cristobal = sprintf("&totalRows_gest_cni_andes_san_cristobal=%d%s", $totalRows_gest_cni_andes_san_cristobal, $queryString_gest_cni_andes_san_cristobal);
 
// -------------------------------LLAMADO A TABLA 03----------------------------------------- //
 
$maxRows_gest_cni_andes_vigia = 10;
$pageNum_gest_cni_andes_vigia = 0;
if (isset($_GET['pageNum_gest_cni_andes_vigia'])) {
  $pageNum_gest_cni_andes_vigia = $_GET['pageNum_gest_cni_andes_vigia'];
}
$startRow_gest_cni_andes_vigia = $pageNum_gest_cni_andes_vigia * $maxRows_gest_cni_andes_vigia;
 
mysql_select_db($database_local, $local);
$query_gest_cni_andes_vigia = "SELECT * FROM gcni_andes_el_vigia";
$query_limit_gest_cni_andes_vigia = sprintf("%s LIMIT %d, %d", $query_gest_cni_andes_vigia, $startRow_gest_cni_andes_vigia, $maxRows_gest_cni_andes_vigia);
$gest_cni_andes_vigia = mysql_query($query_limit_gest_cni_andes_vigia, $local) or die(mysql_error());
$row_gest_cni_andes_vigia = mysql_fetch_assoc($gest_cni_andes_vigia);
 
if (isset($_GET['totalRows_gest_cni_andes_vigia'])) {
  $totalRows_gest_cni_andes_vigia = $_GET['totalRows_gest_cni_andes_vigia'];
} else {
  $all_gest_cni_andes_vigia = mysql_query($query_gest_cni_andes_vigia);
  $totalRows_gest_cni_andes_vigia = mysql_num_rows($all_gest_cni_andes_vigia);
}
$totalPages_gest_cni_andes_vigia = ceil($totalRows_gest_cni_andes_vigia/$maxRows_gest_cni_andes_vigia)-1;
 
$queryString_gest_cni_andes_vigia = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_gest_cni_andes_vigia") == false &&
        stristr($param, "totalRows_gest_cni_andes_vigia") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_gest_cni_andes_vigia = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_gest_cni_andes_vigia = sprintf("&totalRows_gest_cni_andes_vigia=%d%s", $totalRows_gest_cni_andes_vigia, $queryString_gest_cni_andes_vigia);
 
 
 
session_start();
 
// -------------------------------iNICIO DE SESION----------------------------------------- //
 
require("conexion.php");
$usu=$_SESSION['usuario'];
$consulta11=mysql_query("SELECT * FROM usuario where usuario='$usu' ") or die(mysql_error());
$pacientes=mysql_fetch_array($consulta11);
$usu=$pacientes['nombre']." ".$pacientes['apellido'];
?>
 
<!-- -----------------------------TABLA 01------------------------------------------ -->
 
<table border="1" align="center" bgcolor="#FFFFFF" width="1280">
  <tr bgcolor="#E0E0E0">
    <td><strong>Indicador</strong></td>
    <td><strong>Año</strong></td>
    <td><strong>Región</strong></td>
    <td><strong>CDS</strong></td>
    <td><strong>Ene</strong></td>
    <td><strong>Feb</strong></td>
    <td><strong>Mar</strong></td>
    <td><strong>Abr</strong></td>
    <td><strong>May</strong></td>
    <td><strong>Jun</strong></td>
    <td><strong>Jul</strong></td>
    <td><strong>Ago</strong></td>
    <td><strong>Sep</strong></td>
    <td><strong>Oct</strong></td>
    <td><strong>Nov</strong></td>
    <td><strong>Dic</strong></td>
    <td><strong>Total</strong></td>
 </tr>
  <?php do { ?>
    <tr>
      <td bgcolor="#E0E0E0"><strong>&nbsp;&nbsp;&nbsp;<?php echo $row_gest_cni_andes_merida['Id']; ?>&nbsp;&nbsp;</strong></td>
      <td><?php echo $row_gest_cni_andes_merida['year']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_merida['Region']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_merida['CDS']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_merida['Enero']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_merida['Febrero']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_merida['Marzo']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_merida['Abril']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_merida['Mayo']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_merida['Junio']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_merida['Julio']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_merida['Agosto']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_merida['Septiembre']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_merida['Octubre']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_merida['Noviembre']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_merida['Diciembre']; ?>&nbsp; </td>
      <td><strong> <?php echo $suma=$row_gest_cni_andes_merida['Enero']+ $row_gest_cni_andes_merida['Febrero']+ $row_gest_cni_andes_merida['Marzo']+ $row_gest_cni_andes_merida['Abril']+ $row_gest_cni_andes_merida['Mayo']+ $row_gest_cni_andes_merida['Junio']+ $row_gest_cni_andes_merida['Julio']+ $row_gest_cni_andes_merida['Agosto']+ $row_gest_cni_andes_merida['Septiembre']+ $row_gest_cni_andes_merida['Octubre']+ $row_gest_cni_andes_merida['Noviembre']+ $row_gest_cni_andes_merida['Diciembre'] ?>&nbsp; </strong></td>
    </tr>
    <?php } while ($row_gest_cni_andes_merida = mysql_fetch_assoc($gest_cni_andes_merida)); ?>
</table>
 
 
<p>&nbsp;</p>
<p>&nbsp;</p>
 
<!-- -----------------------------TABLA 02------------------------------------------ -->
 
NO COLOCO EL CÓDIGO DE LA TABLA 2 PARA QUE NO SEA TAN LARGO
 
<!-- -----------------------------TABLA 03------------------------------------------ -->
 
<table border="1" align="center" bgcolor="#FFFFFF" width="1280">
  <tr bgcolor="#E0E0E0">
    <td><strong>Indicador</strong></td>
    <td><strong>Año</strong></td>
    <td><strong>Región</strong></td>
    <td><strong>CDS</strong></td>
    <td><strong>Ene</strong></td>
    <td><strong>Feb</strong></td>
    <td><strong>Mar</strong></td>
    <td><strong>Abr</strong></td>
    <td><strong>May</strong></td>
    <td><strong>Jun</strong></td>
    <td><strong>Jul</strong></td>
    <td><strong>Ago</strong></td>
    <td><strong>Sep</strong></td>
    <td><strong>Oct</strong></td>
    <td><strong>Nov</strong></td>
    <td><strong>Dic</strong></td>
    <td><strong>Total</strong></td>
 </tr>
  <?php do { ?>
    <tr>
      <td bgcolor="#E0E0E0"><strong>&nbsp;&nbsp;&nbsp;<?php echo $row_gest_cni_andes_vigia['Id']; ?>&nbsp;&nbsp;</strong></td>
      <td><?php echo $row_gest_cni_andes_vigia['year']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_vigia['Region']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_vigia['CDS']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_vigia['Enero']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_vigia['Febrero']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_vigia['Marzo']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_vigia['Abril']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_vigia['Mayo']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_vigia['Junio']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_vigia['Julio']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_vigia['Agosto']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_vigia['Septiembre']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_vigia['Octubre']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_vigia['Noviembre']; ?>&nbsp; </td>
      <td><?php echo $row_gest_cni_andes_vigia['Diciembre']; ?>&nbsp; </td>
      <td><strong> <?php echo $suma=$row_gest_cni_andes_vigia['Enero']+ $row_gest_cni_andes_vigia['Febrero']+ $row_gest_cni_andes_vigia['Marzo']+ $row_gest_cni_andes_vigia['Abril']+ $row_gest_cni_andes_vigia['Mayo']+ $row_gest_cni_andes_vigia['Junio']+ $row_gest_cni_andes_vigia['Julio']+ $row_gest_cni_andes_vigia['Agosto']+ $row_gest_cni_andes_vigia['Septiembre']+ $row_gest_cni_andes_vigia['Octubre']+ $row_gest_cni_andes_vigia['Noviembre']+ $row_gest_cni_andes_vigia['Diciembre'] ?>&nbsp; </strong></td>
    </tr>
    <?php } while ($row_gest_cni_andes_vigia = mysql_fetch_assoc($gest_cni_andes_vigia)); ?>
</table>
 
 
</body>
</html>
 
<?php
mysql_free_result($gest_cni_andes_merida);
?>
<?php
mysql_free_result($gest_cni_andes_san_cristobal);
?>
<?php
mysql_free_result($gest_cni_andes_vigia);
?>
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