PHP - Mostrar datos mysql cada x tiempo

 
Vista:
sin imagen de perfil

Mostrar datos mysql cada x tiempo

Publicado por Oscar (7 intervenciones) el 24/10/2017 16:18:59
Puse un post hace unas semanas, y sigo atascado en el mismo sitio, quiero desde unas consultas mysql desde php, mostrar unos datos cada x tiempo.
Os dejo el link, alli esta bien explicado.

https://www.lawebdelprogramador.com/foros/PHP/1627384-Consulta-mysql-php-javascript.html#last

He conseguido hacerlo con un documento en texto html, pero no consigo hacerlo enlazandolo con consultas php.
Os dejo el código y os subo el fichero, para que veáis lo que quiero que haga mi código. Por si podéis echarme una mano.

Gracias.

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
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1">
    	<title>Mostrar camiones</title>
<!--    	<link rel="stylesheet" type="text/css" href="css/actual.css" />-->
    	<style>
    		.normal {
    			background-color: grey;
    		}
    		.green {
    			background-color: green;
    		}
    		.oculto {
    			visibility: hidden;
    			display: none;
    		}
    		.visible {
    			visibility: visible;
    			display: table-row;
    		}
    	</style>
 
 
		<div class="reloj">
				<table class="negrita">
				<tr>
					<th>HORA</th>
					<th>ORIGEN</th>
					<th>DESTINO</th>
					<th>MUELLE</th>
					<th>MATRICULA</th>
				</tr>
				<tr name="infoTruck" class="green">
				  	<td>10:30</td>
				   	<td>Alcoy</td>
				    <td>Alicante</td>
				   	<td>32</td>
				   	<td name="truck">2354HCD</td>
				</tr>
				<tr name="infoTruck" class="normal">
				  	<td>10:20</td>
				   	<td>Alcoy</td>
				    <td>Valencia</td>
				   	<td>2</td>
				   	<td name="truck">8554DFG</td>
				</tr>
				<tr name="infoTruck" class="normal">
				  	<td>10:50</td>
				   	<td>Alicante</td>
				    <td>Barcelona</td>
				   	<td>4</td>
				   	<td name="truck">1124FDS</td>
				</tr>
				<tr name="infoTruck" class="normal">
				  	<td>10:30</td>
				   	<td>Murcia</td>
				    <td>Alicante</td>
				   	<td>1</td>
				   	<td name="truck">3324CFF</td>
				</tr>
			</table>
		</div>
		</br>
 
		<table>
			<tr name ="2354HCD" class="oculto">
			  	<td>Carga</td>
			   	<td>4000 KG</td>
			</tr>
			<tr name ="2354HCD" class="oculto">
			  	<td>Carga</td>
			   	<td>3000 KG</td>
			</tr>
			<tr name ="8554DFG" class="oculto">
			  	<td>Carga</td>
			   	<td>4400 KG</td>
			</tr>
			<tr name ="8554DFG" class="oculto">
			  	<td>Carga</td>
			   	<td>1000 KG</td>
			</tr>
			<tr name ="8554DFG" class="oculto">
			  	<td>Carga</td>
			   	<td>200 KG</td>
			</tr>
			<tr name ="1124FDS" class="oculto">
			  	<td>Carga</td>
			   	<td>3200 KG</td>
			</tr>
			<tr name ="1124FDS" class="oculto">
			  	<td>Carga</td>
			   	<td>1200 KG</td>
			</tr>
			<tr name ="3324CFF" class="oculto">
			  	<td>Carga</td>
			   	<td>4320 KG</td>
			</tr>
			<tr name ="3324CFF" class="oculto">
			  	<td>Carga</td>
			   	<td>2030 KG</td>
			</tr>
			<tr name ="3324CFF" class="oculto">
			  	<td>Carga</td>
			   	<td>1000 KG</td>
			</tr>
			<tr name ="3324CFF" class="oculto">
			  	<td>Carga</td>
			   	<td>20 KG</td>
			</tr>
		</table>
 
 
	<script type="text/javascript">
		var elementsFreight;
		var elementsTruck = document.getElementsByName("truck");
		var infosTrucks = document.getElementsByName("infoTruck");
		//var log = document.getElementById("log");
		var freightTruck;
		var posT = 0;
		var posF = 0;
		truckOut();
 
		function truckOut() {
			if (posT >= infosTrucks.length) {
				location.reload(true);
			}
 
			if (posT != 0) {
				elementsFreight[posF-1].className = "oculto";
				infosTrucks[posT-1].className = "normal";
			}
			debugger;
			infosTrucks[posT].className = "green";
			elementsFreight = document.getElementsByName(elementsTruck[posT].innerHTML);
			posF = 0;
			freightOut();
		};
 
		function freightOut() {
			debugger;
			if (posF >= elementsFreight.length) {
				++posT;
				setTimeout(truckOut, 0);
			}
 
			if (posF != 0) {
				elementsFreight[posF-1].className = "oculto";
			}
 
			elementsFreight[posF].className = "visible";
			//log.innerHTML = log.innerHTML + elementsTruck[posT].innerHTML + elementsFreight[posF].innerHTML + "<br/>";
			++posF;
			setTimeout(freightOut, 2000);
		};
 
	</script>
	</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 santi
Val: 588
Bronce
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Mostrar datos mysql cada x tiempo

Publicado por santi (204 intervenciones) el 24/10/2017 17:55:23
Hola,

deberías hacerlo con ajax y la función load:

1
<div id="resultado"></div><!-- aquí dentro va el contenido -->

1
2
3
4
5
jQuery(document).ready(function(){
    setInterval(function(){
          $('#resultado').load('datos.php');//datos.php es la página donde muestra los datos que los podrias sacar en pantalla con un echo o un print.
    }, 5000);//cada 5 segundos cargará la página dentro de "#resultado"
});

Recuerda cargar jquery antes del script -> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
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

Mostrar datos mysql cada x tiempo

Publicado por Oscar (7 intervenciones) el 25/10/2017 16:39:15
Muchas gracias por la respuesta, ya he conseguido hacerlo:

Con este bucle me guardaba en un array de valores, cada vez que consultaba la consulta.

1
2
3
4
5
6
7
while($fila = $consulta->fetch(PDO::FETCH_ASSOC))
{
 
    $valores[$i]="<td>".$fila['matricula']."</td>"."<td>".$fila['carga']."</td>";
    $i++;
 
}

Y después, una vez tengo mi array corriendo ha sido muy sencillo.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<script type="text/javascript">
    var matriculaactual = <?php echo json_encode($valores); ?>; (esto para pasar el array de php a javascript)
    var maximo = "<?php echo $maximo;?>";
    var nuevamatricula = -1;
 
function repetir() {
  nuevamatricula++;
  //document.write(maximo);
  if (nuevamatricula == maximo) {
 
    location.reload(true);
  }
 
  document.getElementById('matricula').innerHTML=matriculaactual[nuevamatricula];
 
  setTimeout("repetir()", 10*1000);
}
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