PHP - Mejorar archivo de busqueda en php / mysql

 
Vista:
Imágen de perfil de jose
Val: 184
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Mejorar archivo de busqueda en php / mysql

Publicado por jose (71 intervenciones) el 08/12/2023 08:20:54
Hola

Tengo una página de busqueda en una web personal. Me gustaría mejorar el código php para que el código no sea tan enrevesado, no se me ocurre como.

Alguien podría decirme como lo haría sin que haya tanto código y más fácil de gestionar.

Os dejo el código de la página. Saludos,

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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
<?php
// Deshabilita la caché en el lado del cliente
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
 
require('../../session/cookie.php');
ob_start();
session_start();
if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {
  header("location:../../index.php");
}
require('../../conn/connection.php');
include '../../php/phpScript.php';
 
?>
 
<!DOCTYPE html>
<html class="no-js" lang="en">
 
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Gestión personal de J&J - Movimientos</title>
  <meta name="author" content="Jose Luis Rz Az">
  <meta name="copyright" content="Copyright owner - Jose Luis Rodriguez Andradez" />
	<meta name="robots" content="noindex,nofollow" />
  <!-- links -->
  <link rel="shortcut icon" href="../../images/facturas.ico">
  <!-- Style CSS  -->
  <link rel="stylesheet" type="text/css" href="../../css/default.css">
  <link rel="stylesheet" type="text/css" href="../../css/component.css">
  <link rel="stylesheet" type="text/css" href="../../css/style.css">
  <script type="text/javascript" src="../../js/modernizr.custom.js"></script>
  <script type="text/javascript" src="../../js/jscript.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <script src="../../js/jquery.dlmenu.js"></script>
 
  <script type="text/javascript">
    $(function() {
      $('#dl-menu').dlmenu({
        animationClasses: {
          classin: 'dl-animate-in-5',
          classout: 'dl-animate-out-5'
        }
      });
    });
  </script>
 
</head>
 
<body>
<?php include('../menus/menu_movimiento.html'); ?>
    <header>
      <br><br><br>
      <h1>Gestion de cuentas, vehículos e inmuebles<span class="clearfix">Gestión
          personal</span></h1>
    </header>
 
  <br>
 
  <div id="base">
 
 
    <div>
      <h1>Busqueda de movimientos</h1>
    </div>
 
    <div id="base">
 
      <div>
        <center>
          <form name="formulario" method="POST" enctype="multipart/form-data">
            <br>
            <label>Título :</label>
            <select name="titulo">
              <option value="" disabled selected>Selecciona</option>
 
              <?php
              $query = "select DISTINCTROW titulo from titulo order by titulo";
              $result = mysqli_query($conn, $query);
              while ($row = $result->fetch_assoc()) {
                echo '<option value="' . $row['titulo'] . '">' . $row['titulo'] . '</option>';
              }
              ?>
            </select><br><br>
 
            <!--Titulo:
					<input type="text" name="titulo" placeholder="Escriba el concepto de Titulo" size="25" maxlength="50" required />*<br><br>-->
 
            <label>Entidad :</label> <input type="text" name="entidad" size="50" maxlength="50" /><br><br>
            <INPUT TYPE='checkbox' Name='gasto' value='gasto'>Gasto
            <INPUT TYPE='checkbox' Name='ingreso' value='ingreso'>Ingreso <br><br>
 
            <label>Vivienda:</label>
            <select name="vivienda">
              <option value="" disabled selected>Opcional</option>
              <?php
 
              $query = "select DISTINCTROW tabla_index,alias from viviendas";
              $result = mysqli_query($conn, $query);
              while ($row_vivienda = $result->fetch_assoc()) {
                echo '<option value="' . $row_vivienda['tabla_index'] . '">' . $row_vivienda['alias'] . '</option>';
              }
              ?>
            </select><br><br>
 
            <label>Descripción a buscar :</label> <input type="text" name="descripcion" size="50" maxlength="50" /><br><br>
 
            Fecha inicial:
            <input type="date" name="fecha_inicial" /><br><br>
            Fecha final:
            <input type="date" name="fecha_final" /><br><br>
 
            <INPUT TYPE='checkbox' Name='sumar' value='sumar'>Sumar<br><br>
 
            <input class="button" type="submit" name="submit" value="Buscar" />
            <input class="button" type="reset">
          </form>
        </center>
 
 
        <br><br>
        <?php
 
        if (!isset($_POST["submit"]) && isset($_GET['sql'])) {
          require('../../conn/connection.php');
          $sql = $_GET['sql'];
          $result = mysqli_query($conn, $sql);
 
          busqueda($result,$sql);
        }
 
        $query_total = "tabla_index, titulo, entidad, concat(cantidad,' €') cantidad, DATE_FORMAT(fecha,'%d/%m/%Y') as fecha1 , operacion, doc, renta, vivienda, inquilino,extra,extraPisos,descripcion";
        $query = '';
 
 
 
        if (isset($_POST["submit"])) {
 
          $sql ='';
          $result = '';
 
          //switch( $_POST['campo'] ) {
          //Caso de busqueda por título
          //case 'titulo':
 
 
          $titulo = $_POST['titulo'];
          $entidad =  $_POST['entidad'];
          $gasto = (isset($_POST['gasto'])) ? 1 : 0;
          $ingreso = (isset($_POST['ingreso'])) ? 1 : 0;
          $vivienda = $_POST['vivienda'];
          $descripcion = $_POST['descripcion'];
          $fecha_inicial = $_POST['fecha_inicial'];
          $fecha_final = $_POST['fecha_final'];
          $sumar = (isset($_POST['sumar'])) ? 1 : 0;
 
          //Sin suma
          if (empty($sumar)) {
            if ((empty($_POST['fecha_inicial'])) && (empty($_POST['fecha_final']))) {
 
              if (($titulo !== "") && (empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Titulo');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and fecha < now() order by fecha desc;";
              } elseif (($titulo == "") && (!empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Entidad');
                $query = "Select {$query_total} from movimientos where entidad like '%" . $entidad . "%' and fecha < now() order by fecha desc;";
              } elseif (($titulo == "") && (empty($entidad)) && ($vivienda !== "") && (empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Vivienda');
                $query = "Select {$query_total} from movimientos where vivienda = '" . $vivienda . "' and fecha < now() order by fecha desc;";
              } elseif (($titulo == "") && (empty($entidad)) && ($vivienda == "") && (!empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Descripción');
                $query = "Select {$query_total} from movimientos where descripcion like '%" . $descripcion . "%' and fecha < now() order by fecha desc;";
              } elseif (($titulo == "") && (empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (!empty($gasto)) && (empty($ingreso))) {
                queryTipo('Gastos');
                $query = "Select {$query_total} from movimientos where operacion = 'gasto' and fecha < now() order by fecha desc;";
              } elseif (($titulo == "") && (empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (empty($gasto)) && (!empty($ingreso))) {
                queryTipo('Ingresos');
                $query = "Select {$query_total} from movimientos where operacion = 'ingreso' order by fecha desc;";
 
                //Titulo y entidad
              } elseif (($titulo !== "") && (!empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Titulo y entidad');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and entidad like '%" . $entidad . "%' and fecha < now() order by fecha desc;";
                //Titulo y vivienda
              } elseif (($titulo !== "") && (empty($entidad)) && ($vivienda !== "") && (empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Titulo y vivienda');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and vivienda = '" . $vivienda . "' and fecha < now() order by fecha desc;";
                //Titulo y descripcion
              } elseif (($titulo !== "") && (empty($entidad)) && ($vivienda == "") && (!empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Titulo y descripcion');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and descripcion like '%" . $descripcion . "%' and fecha < now() and fecha < now() order by fecha desc;";
                //Titulo, entidad y descripcion
              } elseif (($titulo !== "") && (!empty($entidad)) && ($vivienda == "") && (!empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Titulo, entidad y descripcion');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and entidad like '%" . $entidad . "%' and descripcion like '%" . $descripcion . "%' and fecha < now() order by fecha desc;";
                //Titulo, entidad, vivienda y descripcion
              } elseif (($titulo !== "") && (!empty($entidad)) && ($vivienda !== "") && (!empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Titulo, entidad, vivienda y descripcion');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and entidad like '%" . $entidad . "%' and vivienda = '" . $vivienda . "' and descripcion like '%" . $descripcion . "%' and fecha < now() order by fecha desc;";
                //Titulo y gasto
              } elseif (($titulo !== "") && (empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (!empty($gasto)) && (empty($ingreso))) {
                queryTipo('Titulo y gasto');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and operacion = 'gasto' and fecha < now() order by fecha desc;";
                //Titulo y ingreso
              } elseif (($titulo !== "") && (empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (empty($gasto)) && (!empty($ingreso))) {
                queryTipo('Titulo e ingreso');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and operacion = 'ingreso' and fecha < now() order by fecha desc;";
                //entidad y vivienda
              } elseif (($titulo == "") && (!empty($entidad)) && ($vivienda !== "") && (empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Entidad y vivienda');
                $query = "Select {$query_total} from movimientos where entidad like '%" . $entidad . "%' and vivienda = '" . $vivienda . "' and fecha < now() order by fecha desc;";
                //entidad y descripcion
              } elseif (($titulo == "") && (!empty($entidad)) && ($vivienda == "") && (!empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Entidad y descripcion');
                $query = "Select {$query_total} from movimientos where entidad like '%" . $entidad . "%' and descripcion like '%" . $descripcion . "%' and fecha < now() order by fecha desc;";
              }
 
              //Con fechas y sin sumar
            } else {
 
              if (($titulo !== "") && (empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Fechas');
                $query = "Select {$query_total} from movimientos where fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' order by fecha desc;";
              } elseif (($titulo !== "") && (empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Titulo y fechas');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
              } elseif (($titulo == "") && (!empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Entidad y fechas');
                $query = "Select {$query_total} from movimientos where entidad like '%" . $entidad . "%' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
              } elseif (($titulo == "") && (empty($entidad)) && ($vivienda !== "") && (empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Vivendas y fechas');
                $query = "Select {$query_total} from movimientos where vivienda = '" . $vivienda . "' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
              } elseif (($titulo == "") && (empty($entidad)) && ($vivienda == "") && (!empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Descripción y fechas');
                $query = "Select {$query_total} from movimientos where descripcion like '%" . $descripcion . "%' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
              } elseif (($titulo == "") && (empty($entidad)
              ) && ($vivienda == "") && (empty($descripcion)) && (!empty($gasto)) && (empty($ingreso))) {
                queryTipo('Gastos y fechas');
                $query = "Select {$query_total} from movimientos where operacion = 'gasto' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
              } elseif (($titulo == "") && (empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (empty($gasto)) && (!empty($ingreso))) {
                queryTipo('Ingreso y fechas');
                $query = "Select {$query_total} from movimientos where operacion = 'ingreso' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
 
                //Titulo y entidad
              } elseif (($titulo !== "") && (!empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Titulo, entidad y fechas');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and entidad like '%" . $entidad . "%' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                //Titulo y vivienda
              } elseif (($titulo !== "") && (empty($entidad)) && ($vivienda !== "") && (empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Titulo, vivienda y fechas');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and vivienda = '" . $vivienda . "' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                //Titulo y descripcion
              } elseif (($titulo !== "") && (empty($entidad)) && ($vivienda == "") && (!empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Titulo, descripcion y fechas');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and descripcion like '%" . $descripcion . "%' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                //Titulo, entidad y descripcion
              } elseif (($titulo !== "") && (!empty($entidad)) && ($vivienda == "") && (!empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Titulo, entidad, descripcion y fechas');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and entidad like '%" . $entidad . "%' and descripcion like '%" . $descripcion . "%' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                //Titulo, entidad, vivienda y descripcion
              } elseif (($titulo !== "") && (!empty($entidad)) && ($vivienda !== "") && (!empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Titulo, entidad, vivienda, descripcion y fechas');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and entidad like '%" . $entidad . "%' and vivienda = '" . $vivienda . "' and descripcion like '%" . $descripcion . "%' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                //Titulo y gasto
              } elseif (($titulo !== "") && (empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (!empty($gasto)) && (empty($ingreso))) {
                queryTipo('Titulo, gastos y fechas');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and operacion = 'gasto' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                //Titulo y ingreso
              } elseif (($titulo !== "") && (empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (empty($gasto)) && (!empty($ingreso))) {
                queryTipo('Titulo, ingreso y fechas');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and operacion = 'ingreso' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
              } elseif (($titulo == "") && (!empty($entidad)) && ($vivienda !== "") && (empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('entidad, vivienda y fechas');
                $query = "Select {$query_total} from movimientos where entidad like '%" . $entidad . "%' and vivienda = '" . $vivienda . "' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                //entidad y descripcion
              } elseif (($titulo == "") && (!empty($entidad)) && ($vivienda == "") && (!empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('entidad, descripción y fechas');
                $query = "Select {$query_total} from movimientos where entidad like '%" . $entidad . "%' and descripcion like '%" . $descripcion . "%' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
              }
            } //Fin con fecha y sin sumar
            $result = mysqli_query($conn, $query);
            busqueda($result,$query);
            //Inicio Con suma y sin fecha
          } else {
            if ((empty($_POST['fecha_inicial'])) && (empty($_POST['fecha_final']))) {
 
              if (($titulo !== "") && (empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Sumar cantidad de Titulo');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where titulo = '" . $titulo . "' and fecha < now() order by fecha desc;";
              } elseif (($titulo == "") && (!empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Sumar cantidad de Entidad');
                $query = "Select {$query_total} from movimientos where entidad like '%" . $entidad . "%' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where entidad like '%" . $entidad . "%' and fecha < now() order by fecha desc;";
              } elseif (($titulo == "") && (empty($entidad)) && ($vivienda !== "") && (empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Sumar cantidad de Vivienda');
                $query = "Select {$query_total} from movimientos where vivienda = '" . $vivienda . "' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where vivienda = '" . $vivienda . "' and fecha < now() order by fecha desc;";
              } elseif (($titulo == "") && (empty($entidad)) && ($vivienda == "") && (!empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Sumar cantidad de Descripción');
                $query = "Select {$query_total} from movimientos where descripcion like '%" . $descripcion . "%' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where descripcion like '%" . $descripcion . "%' and fecha < now() order by fecha desc;";
              } elseif (($titulo == "") && (empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (!empty($gasto)) && (empty($ingreso))) {
                queryTipo('Sumar cantidad de Gastos');
                $query = "Select {$query_total} from movimientos where operacion = 'gasto' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where operacion = 'gasto' and fecha < now() order by fecha desc;";
              } elseif (($titulo == "") && (empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (empty($gasto)) && (!empty($ingreso))) {
                queryTipo('Sumar cantidad de Ingresos');
                $query = "Select {$query_total} from movimientos where operacion = 'ingreso' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where operacion = 'ingreso' and fecha < now() order by fecha desc;";
                //Titulo y entidad
              } elseif (($titulo !== "") && (!empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Sumar cantidad de Titulo y entidad');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and entidad like '%" . $entidad . "%' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where titulo = '" . $titulo . "' and entidad like '%" . $entidad . "%' and fecha < now() order by fecha desc;";
                //Titulo y vivienda
              } elseif (($titulo !== "") && (empty($entidad)) && ($vivienda !== "") && (empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Sumar cantidad de Titulo y vivienda');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and vivienda = '" . $vivienda . "' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where titulo = '" . $titulo . "' and vivienda = '" . $vivienda . "' and fecha < now() order by fecha desc;";
                //Titulo y descripcion
              } elseif (($titulo !== "") && (empty($entidad)) && ($vivienda == "") && (!empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Sumar cantidad de Titulo y descripcion');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and descripcion like '%" . $descripcion . "%' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where titulo = '" . $titulo . "' and descripcion like '%" . $descripcion . "%' and fecha < now() order by fecha desc;";
                //Titulo, entidad y descripcion
              } elseif (($titulo !== "") && (!empty($entidad)) && ($vivienda == "") && (!empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Sumar cantidad de Titulo, entidad y descripcion');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and entidad like '%" . $entidad . "%' and descripcion like '%" . $descripcion . "%' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where titulo = '" . $titulo . "' and entidad like '%" . $entidad . "%' and descripcion like '%" . $descripcion . "%' and fecha < now() order by fecha desc;";
                //Titulo, entidad, vivienda y descripcion
              } elseif (($titulo !== "") && (!empty($entidad)) && ($vivienda !== "") && (!empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Sumar cantidad de Titulo, entidad, vivienda y descripcion');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and entidad like '%" . $entidad . "%' and vivienda = '" . $vivienda . "' and descripcion like '%" . $descripcion . "%' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos  where titulo = '" . $titulo . "' and entidad like '%" . $entidad . "%' and vivienda = '" . $vivienda . "' and descripcion like '%" . $descripcion . "%' and fecha < now() order by fecha desc;";
                //Titulo y gasto
              } elseif (($titulo !== "") && (empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (!empty($gasto)) && (empty($ingreso))) {
                queryTipo('Sumar cantidad de Titulo y gasto');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and operacion = 'gasto' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where titulo = '" . $titulo . "' and operacion = 'gasto' and fecha < now() order by fecha desc;";
                //Titulo y ingreso
              } elseif (($titulo !== "") && (empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (empty($gasto)) && (!empty($ingreso))) {
                queryTipo('Sumar cantidad de Titulo e ingreso');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and operacion = 'ingreso' order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where titulo = '" . $titulo . "' and operacion = 'ingreso' and fecha < now() order by fecha desc;";
                //entidad y vivienda
              } elseif (($titulo == "") && (!empty($entidad)) && ($vivienda !== "") && (empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Sumar cantidad de Entidad y vivienda');
                $query = "Select {$query_total} from movimientos where entidad like '%" . $entidad . "%' and vivienda = '" . $vivienda . "' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where entidad like '%" . $entidad . "%' and vivienda = '" . $vivienda . "' and fecha < now() order by fecha desc;";
                //entidad y descripcion
              } elseif (($titulo == "") && (!empty($entidad)) && ($vivienda == "") && (!empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Sumar cantidad de Entidad y descripcion');
                $query = "Select {$query_total} from movimientos where entidad like '%" . $entidad . "%' and descripcion like '%" . $descripcion . "%' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where entidad like '%" . $entidad . "%' and descripcion like '%" . $descripcion . "%' and fecha < now() order by fecha desc;";
              }
            } else { //inicio fecha y sumar
 
              if (($titulo !== "") && (empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Sumar cantidad de Titulo y fechas');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where titulo = '" . $titulo . "' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
              } elseif (($titulo == "") && (!empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Sumar cantidad de Entidad y fechas');
                $query = "Select {$query_total} from movimientos where entidad like '%" . $entidad . "%' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where entidad like '%" . $entidad . "%' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
              } elseif (($titulo == "") && (empty($entidad)) && ($vivienda !== "") && (empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Sumar cantidad de Vivendas y fechas');
                $query = "Select {$query_total} from movimientos where vivienda = '" . $vivienda . "' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where vivienda = '" . $vivienda . "' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
              } elseif (($titulo == "") && (empty($entidad)) && ($vivienda == "") && (!empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Sumar cantidad de Descripción y fechas');
                $query = "Select {$query_total} from movimientos where descripcion like '%" . $descripcion . "%' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where descripcion like '%" . $descripcion . "%' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
              } elseif (($titulo == "") && (empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (!empty($gasto)) && (empty($ingreso))) {
                queryTipo('Sumar cantidad de Gastos y fechas');
                $query = "Select {$query_total} from movimientos where operacion = 'gasto' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos  where operacion = 'gasto' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
              } elseif (($titulo == "") && (empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (empty($gasto)) && (!empty($ingreso))) {
                queryTipo('Sumar cantidad de Ingreso y fechas');
                $query = "Select {$query_total} from movimientos where operacion = 'ingreso' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where operacion = 'ingreso' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                //Titulo y entidad
              } elseif (($titulo !== "") && (!empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Sumar cantidad de Titulo, entidad y fechas');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and entidad like '%" . $entidad . "%' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where titulo = '" . $titulo . "' and entidad like '%" . $entidad . "%' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                //Titulo y vivienda
              } elseif (($titulo !== "") && (empty($entidad)) && ($vivienda !== "") && (empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Sumar cantidad de Titulo, vivienda y fechas');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and vivienda = '" . $vivienda . "' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where titulo = '" . $titulo . "' and vivienda = '" . $vivienda . "' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                //Titulo y descripcion
              } elseif (($titulo !== "") && (empty($entidad)) && ($vivienda == "") && (!empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Sumar cantidad de Titulo, descripcion y fechas');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and descripcion like '%" . $descripcion . "%' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where titulo = '" . $titulo . "' and descripcion like '%" . $descripcion . "%' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                //Titulo, entidad y descripcion
              } elseif (($titulo !== "") && (!empty($entidad)) && ($vivienda == "") && (!empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Sumar cantidad de Titulo, entidad, descripcion y fechas');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and entidad like '%" . $entidad . "%' and descripcion like '%" . $descripcion . "%' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where titulo = '" . $titulo . "' and entidad like '%" . $entidad . "%' and descripcion like '%" . $descripcion . "%' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                //Titulo, entidad, vivienda y descripcion
              } elseif (($titulo !== "") && (!empty($entidad)) && ($vivienda !== "") && (!empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Sumar cantidad de Titulo, entidad, vivienda, descripcion y fechas');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and entidad like '%" . $entidad . "%' and vivienda = '" . $vivienda . "' and descripcion like '%" . $descripcion . "%' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where titulo = '" . $titulo . "' and entidad like '%" . $entidad . "%' and vivienda = '" . $vivienda . "' and descripcion like '%" . $descripcion . "%' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                //Titulo y gasto
              } elseif (($titulo !== "") && (empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (!empty($gasto)) && (empty($ingreso))) {
                queryTipo('Sumar cantidad de Titulo, gastos y fechas');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and operacion = 'gasto' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where titulo = '" . $titulo . "' and operacion = 'gasto' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                //Titulo y ingreso
              } elseif (($titulo !== "") && (empty($entidad)) && ($vivienda == "") && (empty($descripcion)) && (empty($gasto)) && (!empty($ingreso))) {
                queryTipo('Sumar cantidad de Titulo, ingreso y fechas');
                $query = "Select {$query_total} from movimientos where titulo = '" . $titulo . "' and operacion = 'ingreso' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where titulo = '" . $titulo . "' and operacion = 'ingreso' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
              } elseif (($titulo == "") && (!empty($entidad)) && ($vivienda !== "") && (empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Sumar cantidad de entidad, vivienda y fechas');
                $query = "Select {$query_total} from movimientos where entidad like '%" . $entidad . "%' and vivienda = '" . $vivienda . "' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where entidad like '%" . $entidad . "%' and vivienda = '" . $vivienda . "' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                //entidad y descripcion
              } elseif (($titulo == "") && (!empty($entidad)) && ($vivienda == "") && (!empty($descripcion)) && (empty($gasto)) && (empty($ingreso))) {
                queryTipo('Sumar cantidad de entidad, descripción y fechas');
                $query = "Select {$query_total} from movimientos where entidad like '%" . $entidad . "%' and descripcion like '%" . $descripcion . "%' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
                $query_suma = "Select sum(cantidad) as cantidad from movimientos where entidad like '%" . $entidad . "%' and descripcion like '%" . $descripcion . "%' and fecha between '" . $fecha_inicial . "' and '" . $fecha_final . "' and fecha < now() order by fecha desc;";
              }
            }
 
            $result = mysqli_query($conn, $query);
            $result_suma = mysqli_query($conn, $query_suma);
            busqueda($result,$query);
            suma($result_suma);
          } //fin con suma
 
        } //Fin submit
 
 
 
        ?>
 
        <?php
        /** Para comprobar la busqueda en test */
        function queryTipo($tipo)
        {
          //echo $tipo;
        }
 
        function busqueda($result,$query) {
 
          require('../../conn/connection.php');
        ?>
 
          <div class="tablaSearchMovimientos">
 
            <center>
              <table>
                <tbody>
                  <tr>
                    <th>Título</th>
                    <th>Entidad</th>
                    <th>Cantidad</th>
                    <th>Fecha</th>
                    <th>Operación</th>
                    <th>Documento</th>
                    <th>renta</th>
                    <th>Vivienda</th>
                    <th>Inquilino</th>
                    <th>Expirado</th>
                    <th>Extra</th>
                  </tr>
                  <?php
 
 
                  //$query="Select {$query_total} from movimientos where titulo like '".$palabra."' order by fecha desc;";
 
                  //$count_rows = mysqli_num_rows($result);
 
                  while ($row = $result->fetch_assoc()) {
                    $extraTexto = '';
						        if ($row['extra'] == '1') { $extraTexto = 'Extra'; }
						        if ($row['extraPisos']== '1') { $extraTexto = 'Extra de Pisos'; }
                    $vivienda =  $row['vivienda'];
                    $inquilino = $row['inquilino'];
 
 
                  ?>
                    <tr bgcolor="<?php ColorBackGround($row['titulo']); ?>">
 
 
                      <td><?php echo $row['titulo']; ?></td>
                      <td><?php echo $row['entidad']; ?></td>
                      <td><?php echo $row['cantidad']; ?></td>
                      <td><?php echo $row['fecha1']; ?></td>
                      <td><?php echo $row['operacion']; ?>
                      <td><?php if (empty($row['doc'])) {
                            echo 'No hay imagen </td>';
                          } else {
                          ?>
                          <a class="image-link" href='../../upload/<?php echo $row['doc']; ?>' target="_blank"><?php echo $row['doc']; ?></a>
                        <?php
                          } ?>
                      </td>
                      <td><?php echo $row['renta']; ?></td>
                      <td>
                        <?php
                        $queryVivienda = "SELECT * from viviendas where tabla_index = '" . $vivienda . "'";
                        $resultVivienda = mysqli_query($conn, $queryVivienda);
                        $rowVivienda = mysqli_fetch_assoc($resultVivienda);
                        echo $rowVivienda['alias'];
                        ?> </td>
 
                      <td><?php $queryInquilino = "SELECT * from arrendatarios where tabla_index = '" . $inquilino . "'";
                          $resultInquilino  = mysqli_query($conn, $queryInquilino);
                          $rowInquilino  = mysqli_fetch_assoc($resultInquilino);
                          if (isset($rowInquilino['alias']))  echo $rowInquilino['alias'];  else echo " - "; ?> </td>
                      <td><?php echo $extraTexto;?></td>
 
                      <td><a href="edit.php?GetID=<?php echo $row['tabla_index']; ?>" target="_blank"><img src="../../../images/writing.png" alt="Editar Registro" title="Editar Registro"></td>
                      <td><a href="del_register.php?Del=<?php echo $row['tabla_index']; ?>&Page=search&sql=<?php echo $query; ?>" onclick="return eliminar()"><img src="../../../images/delete.png" alt="Borrar Registro" title="Borrar Registro"></td>
                     <!-- <td><a href="#" onclick="delRegister($row['tabla_index'])"><img src="../../../images/delete.png" alt="Borrar Registro" title="Borrar Registro"></td>-->
                      <td><a href="../../upload/<?php echo $row['doc']; ?>" download><img src="../../../images/download.png" alt="Bajar archivo" title="Bajar archivo"></td>
                    </tr>
                  <?php } //Fin Fetch Result
                  ?>
                </tbody>
 
              </table>
            </center>
          </div>
 
        <?php  }
 
 
        function suma($result_suma)
        {
 
          $row = $result_suma->fetch_assoc();
          echo '<div style="color:red;font-size: 30px;"> Total : ' . $row['cantidad'] . '</div>';
        }
 
        ?>
 
 
        <?php
        /* Cerrar la conexión */
        mysqli_close($conn);
        ?>
</body>
 
</html>
<?php
ob_end_flush();
?>
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 Mauro
Val: 2.761
Oro
Ha aumentado 1 puesto en PHP (en relación al último mes)
Gráfica de PHP

Mejorar archivo de busqueda en php / mysql

Publicado por Mauro (1036 intervenciones) el 08/12/2023 11:55:26
Hola Jose:

Hay varias cosas para mirar. Yo empezaría por intentar separar el código que genera la UI (El html) del que realiza las consultas a la base de datos y demás lógica de negocio.

Algo que podría serte útil es implementar un sistema de templates. Twig es mi favorito, pero puede ser algo complejo para arrancar... de pronto Smarty puede resultarte mejor para arrancar.

También probablemente te puedas beneficiar de estructurar tu código siguiendo el paradigma de Orientación a Objetos.
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 jose
Val: 184
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Mejorar archivo de busqueda en php / mysql

Publicado por jose (71 intervenciones) el 08/12/2023 18:51:27
Hola Mauro,

El tema de separar html y php, lo estoy haciendo poco a poco. Cuando cree la web lo hice todo junto. Estoy utilizando MVC, pero son tantas páginas que es mucho tiempo,

En cuanto a si utilizar templates o POO en php, optaría más por POO tendría que ver como hacerlo. Pero creo que sería lo mejor.

Muchísimas gracias Mauro,
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar