PHP - listbox y tablas html

 
Vista:

listbox y tablas html

Publicado por Diego Yanez (1 intervención) el 02/06/2016 16:03:23
Hola a todos junto con saludar quisiera preguntar como puedo hacer para que de un listbox me cargue una tabla html con datos de la bd mysql cada vez que presiono un id de la listbox estoy super complicado con eso dejo mi codigo y de antemano a quien pueda guiarme o mostrar como se puede hacer les estaré agradecido,

conexion.php

1
2
3
4
5
6
7
8
9
10
<?php
define ('HOST','127.0.0.1');
define ('USER','xxx'); //modificar segun tu Servidor Mysql
define ('PASS','xxxx'); //modificar segun tu Servidor Mysql
define ('DB','proyecto2');
 
$link = mysql_connect ( HOST, USER, PASS );
mysql_select_db ( DB, $link );
 
?>

inventario.php

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
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>inventarios</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="funcionajax.js"></script>
</head>
 
<header></header>
<body>
<section>
<center>
 
 
<table width="800" border="1" cellspacing="0">
<tr>
<td style="width:100px" class="style2" align="center"><select id="establecimiento">
<!--llenar los select con datos de bd -->
<?php require 'conexion.php';
$cons = mysql_query("SELECT * FROM Establecimiento ORDER BY nombre");
echo '<option selected="selected" disabled="disabled">Establecimiento</option>';
while($cons2 = mysql_fetch_array($cons)){
echo '<option value="'.$cons2['idEstablecimiento'].'">'.$cons2['nombre'].'</option>';
} ?> </select>
</td>
 
<td style="width:100px" class="style2" align="center"><select id="departamento"></select>
 
</td>
 
<br>
<br>
 
</section>
 
 
<!--llenar tabla de articulos-->
<?php include("conexion.php");
 
$art=mysql_query("SELECT * FROM proyecto2.Articulos ORDER BY descripcion",$link );
$numeroRegistro=mysql_num_rows($art);
if($numeroRegistro==0){
echo "No se han encontrado productos para mostrar";
}
?>
<table width="800" border="1" cellspacing="0">
<tr>
<td><div align="center"><span class="style1"><strong>Listado de Articulos</strong></span></div></td>
</tr>
</table>
<table width="800" border="1" cellspacing="0">
<tr>
<td style="width: 52px" class="style2">Id </td>
<td style="width: 222px" class="style2">Descripcion</td>
<td style="width: 188px" class="style2">Stock</td>
<td style="width: 92px" class="style2">Estado</td>
</tr>
</table>
 
<table width="800" border="1" cellspacing="0">
<!--genera la cantidad de filas segun la cantidad de datos en articulos -->
<?php
while($fila=mysql_fetch_array($art))
{
?>
 
<tr>
 
<td style="width: 52px" class="style2"> <?php echo $fila["idArticulos"];?> </td>
<td style="width: 222px" class="style2"> <?php echo $fila["descripcion"]; ?> </td>
<td style="width: 188px" class="style2"> <?php echo$fila["stock"];?> </td>
<td style="width: 92px" class="style2"> <?php echo $fila["estado"];?> </td>
 
</tr>
<?php } ?>
</table>
</body>
</html>


agregardepartamento.php

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
<?php
 
require 'conexion.php';
 
$id = $_POST['id'];
$dep = mysql_query("SELECT * FROM Departamento WHERE idEstablecimiento = '$id' ORDER BY nombre");
 
 
//mysql_num_rows valida si tiene datos $dep
if(mysql_num_rows($dep)>0){
 
echo '<option selected="selected" disabled="disabled"> Departamento </option>';
 
 
 
while($dep2 = mysql_fetch_array($dep)){
 
 
 
echo '<option value="'.$dep2['idDepartamento'].'">'.$dep2['nombre'].'</option>';
 
 
}
 
 
}
 
 
?>

funcionajax.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$(function(){
$('#establecimiento').on('change', function(){
 
var id = $('#establecimiento').val();
var url = 'agregadepartamento.php';
$.ajax({
type:'POST',
url: url,
data: 'id='+id,
success: function(data){
$('#departamento option').remove();
$('#departamento').append(data);
}
});
 
return false;
 
 
});
});

Base de datos mysql

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
CREATE DATABASE IF NOT EXISTS `proyecto2`
USE `proyecto2`;
 
CREATE TABLE `Articulos` (
`idArticulos` int(11) NOT NULL,
`descripcion` varchar(45) DEFAULT NULL,
`stock` varchar(45) DEFAULT NULL,
`estado` varchar(45) DEFAULT NULL,
`idDepartamento` int(11) DEFAULT NULL,
PRIMARY KEY (`idArticulos`),
KEY `fk_Articulos_1_idx` (`idDepartamento`),
CONSTRAINT `fk_Articulos_1` FOREIGN KEY (`idDepartamento`) REFERENCES `Departamento` (`idDepartamento`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
 
 
 
CREATE TABLE `ArticulosEstablecimiento` (
`idestablecimiento` int(11) DEFAULT NULL,
`idArticulo` int(11) DEFAULT NULL,
KEY `estabarti_idx` (`idestablecimiento`),
KEY `fk_ArticulosEstablecimiento_2_idx` (`idArticulo`),
CONSTRAINT `fk_ArticulosEstablecimiento_1` FOREIGN KEY (`idestablecimiento`) REFERENCES `Establecimiento` (`idEstablecimiento`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_ArticulosEstablecimiento_2` FOREIGN KEY (`idArticulo`) REFERENCES `Articulos` (`idArticulos`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
 
CREATE TABLE `Departamento` (
`idDepartamento` int(11) NOT NULL,
`nombre` varchar(45) DEFAULT NULL,
`encargado` varchar(45) DEFAULT NULL,
`idEstablecimiento` int(11) DEFAULT NULL,
PRIMARY KEY (`idDepartamento`),
KEY `fk_Departamento_1_idx` (`idEstablecimiento`),
CONSTRAINT `fk_Departamento_1` FOREIGN KEY (`idEstablecimiento`) REFERENCES `Establecimiento` (`idEstablecimiento`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
 
 
CREATE TABLE `Establecimiento` (
`idEstablecimiento` int(11) NOT NULL AUTO_INCREMENT,
`nombre` varchar(45) NOT NULL DEFAULT '',
`direccion` varchar(45) DEFAULT NULL,
`responsable` varchar(45) DEFAULT NULL,
PRIMARY KEY (`idEstablecimiento`,`nombre`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
 
 
CREATE TABLE `usuarios` (
`idusuario` int(11) NOT NULL AUTO_INCREMENT,
`nombreusuario` varchar(45) DEFAULT NULL,
`login` varchar(45) DEFAULT NULL,
`password` varchar(45) DEFAULT NULL,
`perfil` varchar(45) DEFAULT NULL,
PRIMARY KEY (`idusuario`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
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