PHP - Query

 
Vista:
sin imagen de perfil
Val: 557
Bronce
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Query

Publicado por zendi (1056 intervenciones) el 11/02/2018 23:59:36
Buenas tardes tengo este codigo donde se hace un proceso para recoger los gastos de un condominio y luego debo repartir estos gastos a cada propietario en otra tabla son 11 registros para 11 propietarios que debo asignar, pero el problema es que esta repitiendo los gastos dos veces la idea es que debe ser una sola vez para cada propietario. Por favor si alguien pudiera ayudarme, estoy tambien investigando.
Nota: el query o consulta esta en negrita. Le coloqué un Sum pero no se de que forma podría hacer que sea 11 registros.
Este es el Query:

1
2
3
4
5
6
7
8
9
10
SELECT
    propietarios.nroapto,
    propietarios.cedula,
    sum(gastos1.monto) AS total,
    gastos1.fecha,
    gastos1.codigo
   FROM propietarios,
    gastos1,tipos_gastos
  WHERE gastos1.cod_tipo_gasto = tipos_gastos.codigo
  GROUP BY propietarios.nroapto, propietarios.cedula,  gastos1.fecha, gastos1.codigo

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
<?php
error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE);
include("funciones.php");
    $connect = pg_connect("host=localhost port=5432 dbname=administra user=postgres password=1234");
//rutina para la fecha
///alternar('2015-09-30 00:00:00');
require_once("claseelim.php");
/*$mile = new Ranimile();
*/?>
<html>
<head>
      <script language="JavaScript" src="calendario.js"></script>
      <script language="JavaScript" src="overlib_mini.js"></script>
<SCRIPT type="text/javascript">
var patron = new Array(2,2,4)
var patron2 = new Array(1,3,3,3,3)
function mascara(d,sep,patron,nums){
if(d.valant != d.value){
	val = d.value
	largo = val.length
	val = val.split(sep)
	val2 = ''
	for(r=0;r<val.length;r++){
		val2 += val[r]
	}
	if(nums){
		for(z=0;z<val2.length;z++){
		  if(isNaN(val2.charAt(z))){
			letra = new RegExp(val2.charAt(z),"g")
			val2 = val2.replace(letra,"")
		   }
		}
	}
	val = ''
	val3 = new Array()
	for(s=0; s<patron.length; s++){
		val3[s] = val2.substring(0,patron[s])
		val2 = val2.substr(patron[s])
	}
	for(q=0;q<val3.length; q++){
		if(q ==0){
			val = val3[q]
		}
		else{
			if(val3[q] != ""){
				val += sep + val3[q]
			}
		}
	}
	d.value = val
	d.valant = val
	}
}
</SCRIPT>
 
</head>
<body>
<?php $hoy=date('d-m-Y'); ?>
<?php
/*$dia = '2015-09-30 00:00:00';
if (isset($dia))
{
   $datos=$mile->rarrob($dia);
}*/
?>
   <div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>
 
<FORM name="form1" action="resumendeuda1.php" method="post">
<fieldset style="width:auto"><LEGEND>Gastos Realizados durante el Mes</LEGEND>
<table align="center" border='1' bordercolor='#00CC99' bgcolor="#99CC00">
<!--       <tr>
	   <td align="left">Fecha</td>
	   
		<td class="sr"><input type="text"  name="fecha" size="10" maxlength="10" align="right"/></td>
	   </tr>
-->
<!-- <TR>
	<td>Fecha:</td>
    <td><input type="text" size=10 maxlength=10 name="fecha" onKeyUp="mascara(this,'-',patron,true)" title="Colocar la Fecha Actual cuando no ha habido gastos en el Mes" onFocus="siguienteCampo ='direccion'"><a href=javascript:show_calendar('form1.fecha'); onMouseOver="window.status='Date Picker'; overlib('Haga Click aqui para escoger la fecha Actual del calendario cuando no ha habido gastos en el MES.'); return true;" onMouseOut="window.status=''; nd(); return true;"><img src="iconos/calendar.gif" align=center width=24 height=22 border=0></a></td>
</TR> -->
 
 
</table>
 
<table align="center" border='1' bordercolor='#00CC99' bgcolor="#99CC00">
    <tr>
		<th width='200' align="center">Gasto
		<th width='100' align="center">Monto
		<th width='100' align="center">Fecha
   </tr>
</table>
 
<?php
  error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE);
 
$todosgastos = "SELECT * FROM gastos1 ORDER BY descripcion";
$seleccionados = @pg_query($connect,$todosgastos);
/*$dia6 = '2013-09-30 00:00:00';
$dia5 = $dia6;
$dia4 = $dia5;
$dia3 = $dia4;
$dia2 = $dia3;
$dia1 = $dia2;
*/
$total = 0;
//function alternar($dia1) { 
//if (time() >= strtotime($dia))
//    { 
//      unlink("prueba.html");
//    }
//}
while($select2 = @pg_fetch_array($seleccionados)):
     $articulo    = $select2['codigo'];
	 $descripcion = $select2['descripcion'];
	 $monto       = $select2['monto'];
	 $fecha       = $select2['fecha'];
     echo"<table align='center' border='1' bordercolor='#FFCC99'>
	      <tr class='sr'>
			<td width='200'>$descripcion
			<td width='100' align='right'>$monto
			<td width='100' align='right'>$fecha
		  </tr></table>";
   	  $total +=$monto;
	  $ntotal = $total;
endwhile;
echo "<br><table align='center' border='1' bordercolor='#FFCC99' bgcolor='#99CC00'>
	        <tr class='sr'>
	          <td width='150' style='color:#000000'>Total
			  <td width='150' align='right'>$ntotal
  		    </tr></table>";
?>
</fieldset>
<input type="submit" name="enviar" value="Enviar" title="Proceso para crear la deuda"/>
<INPUT TYPE="BUTTON" VALUE="Salir" ONCLICK="window.location.href='menu1.php'">
</FORM>
<?php
 
$day = date('d');
//if ($day>29 && isset($_POST['enviar']))
if (isset($_POST['enviar']))
   {
if($day < 28)
     {
      echo "<script>alert('No es la Fecha para Crear la Deuda')</script>";
     }
else {
$total = "
SELECT
    propietarios.nroapto,
    propietarios.cedula,
    sum(gastos1.monto) AS total,
    gastos1.fecha,
    gastos1.codigo
   FROM propietarios,
    gastos1,tipos_gastos
  WHERE gastos1.cod_tipo_gasto = tipos_gastos.codigo
  GROUP BY propietarios.nroapto, propietarios.cedula,  gastos1.fecha, gastos1.codigo";
    $asignadeuda = @pg_query($connect,$total);
     while($resultados =@pg_fetch_array($asignadeuda)):
          $codigo  = $resultados['codigo'];
          $nroapto = $resultados['nroapto'];
		  $cedula  = $resultados['cedula'];
		  $monto   = $resultados['total'];
		  $fecha  = $resultados['fecha'];
		  $hora = time();
		if(isset($_POST['enviar']))
		{
			$hoy = $_POST['fecha'];
			$día   = substr($hoy,0,2);
			$mes   = substr($hoy,3,2);
			$anio  = substr($hoy,6,4);
			$hoy1 = $anio."-".$mes."-".$día;
echo $asig = "INSERT INTO deuda(codigo_gasto,monto,nroapto,fecha,cedula,hora) VALUES ('$codigo',$monto,'$nroapto','$fecha','$cedula',$hora)";
			 //  echo "<script type='text/javascript'>
			 // alert('Los registros fueron AGREGADOS a la DEUDA');
			 // window.location='resumendeuda1.php';
			 // </script>";
		}
		else
		{
		  $asig = "INSERT INTO deuda(codigo_gasto,monto,nroapto,fecha,cedula,hora) VALUES ('$codigo',$monto,'$nroapto','$hoy1','$cedula',$hora)";
			echo "<script type='text/javascript'>
			alert('Los registros fueron AGREGADOS con la Fecha de los Gastos');
			window.location='resumendeuda1.php';
			</script>";
		}
		$borrar ="DELETE FROM gastos1";
		@pg_query($connect,$borrar);
		@pg_query($connect,$asig);
	  // $reserva = $ntotal * .5/100;
   //   echo $reser = "INSERT INTO reserva(fecha,monto) VALUES ('$hoy',$reserva)";
   //    @pg_query($connect,$reser);
      endwhile;
 
    }
}
 
 
?>
</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