PHP - Combox

 
Vista:
sin imagen de perfil

Combox

Publicado por lean (10 intervenciones) el 25/01/2017 16:25:05
Hola buenas tardes, recién estoy aprendiendo Php y tengo una duda espero me puedan ayudar.


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
<?php
 
////////////////// CONEXION A LA BASE DE DATOS ////////////////////////////////////
 
$host="localhost";
$usuario="root";
$contraseña="root";
$base="itic";
 
$conexion= new mysqli($host, $usuario, $contraseña, $base);
if ($conexion -> connect_errno)
{
	die("Fallo la conexion:(".$conexion -> mysqli_connect_errno().")".$conexion-> mysqli_connect_error());
}
////////////////// VARIABLES DE CONSULTA////////////////////////////////////
 
$where="";
$nombre=$_POST['xnombre'];
$carrera=$_POST['xcarrera'];
 
 
////////////////////// BOTON BUSCAR //////////////////////////////////////
 
if (isset($_POST['buscar']))
{
 
 
 
	if (empty($_POST['xcarrera']))
	{
		$where="where nombre like '".$nombre."%'";
	}
 
	else if (empty($_POST['xnombre']))
	{
		$where="where carrera='".$carrera."'";
	}
 
	else
	{
		$where="where nombre like '".$nombre."%' and carrera='".$carrera."'";
	}
}
/////////////////////// CONSULTA A LA BASE DE DATOS ////////////////////////
 
$alumnos="SELECT * FROM alumnos $where ";
$resAlumnos=$conexion->query($alumnos);
$resCarreras=$conexion->query($alumnos);
 
if(mysqli_num_rows($resAlumnos)==0)
{
	$mensaje="<h1>No hay registros que coincidan con su criterio de búsqueda.</h1>";
}
?>
<html lang="es">
 
	<head>
		<title>Filtro de Búsqueda PHP</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
 
		<link href="css/estilos.css" rel="stylesheet">
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
 
	</head>
	<body>
		<header>
			<div class="alert alert-info">
			<h2>Filtro de Búsqueda PHP</h2>
			</div>
		</header>
		<section>
			<form method="post">
				<input type="text" placeholder="Nombre..." name="xnombre"/>
 
				<select name="xcarrera">
					<option value="">Carrera </option>
					<?php
						while ($registroCarreras = $resCarreras->fetch_array(MYSQLI_BOTH))
						{
							echo '<option value="'.$registroCarreras['carrera'].'">'.$registroCarreras['carrera'].'</option>';
						}
 
					?>
 
 
				</select>
 
 
 
				<button name="buscar" type="submit">Buscar</button>
			</form>
			<table class="table">
				<tr>
					<th>ID_Alumno</th>
					<th>Nombre</th>
					<th>Carrera</th>
					<th>Grupo</th>
				</tr>
 
				<?php
 
				while ($registroAlumnos = $resAlumnos->fetch_array(MYSQLI_BOTH))
				{
 
					echo'<tr>
						 <td>'.$registroAlumnos['id_alumno'].'</td>
						 <td>'.$registroAlumnos['nombre'].'</td>
						 <td>'.$registroAlumnos['carrera'].'</td>
						 <td>'.$registroAlumnos['grupo'].'</td>
						 </tr>';
				}
				?>
			</table>
 
			<?php
				echo $mensaje;
			?>
		</section>
	</body>
</html>


Lo que pretendo es hacer otros combobox para hacer un filtrado mas concreto
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 xve
Val: 3.943
Oro
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Combox

Publicado por xve (6935 intervenciones) el 25/01/2017 18:30:33
Hola Lean, no entiendo muy bien donde tienes el problema en colocar otro combo?? en obtener los valores? que valores quieres poner es este combo?
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
sin imagen de perfil

Combox

Publicado por lean (10 intervenciones) el 25/01/2017 18:52:30
En este caso quiero listar el valores de "carrera" y "id_alumno", lo que pretendo es poder tener mas filtros para una besuqueada mas exacta.
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 xve
Val: 3.943
Oro
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Combox

Publicado por xve (6935 intervenciones) el 31/01/2017 19:14:36
Hola Lean, la manera que yo lo haría según me comentas es así:
1
echo '<option value="'.$registroCarreras['id_alumo'].'">'.$registroCarreras['carrera'].'</option>';

De esta manera, la búsqueda en la base de datos la haces por el id que es único.

Es esto lo que buscas?
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

Combox

Publicado por lean (10 intervenciones) el 01/02/2017 17:36:17
Lo estoy haciendo de esta forma, en la que quiero que tenga los dos conceptos el combobox por carrera y por id_alumno, pongo el codigo como lo tengo actualmente y pero no me muestra los datos de id_alumno para realizar la busqueda.



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
<?php
 
////////////////// CONEXION A LA BASE DE DATOS ////////////////////////////////////
 
$host="localhost";
$usuario="root";
$contraseña="root";
$base="itic";
 
$conexion= new mysqli($host, $usuario, $contraseña, $base);
if ($conexion -> connect_errno)
{
	die("Fallo la conexion:(".$conexion -> mysqli_connect_errno().")".$conexion-> mysqli_connect_error());
}
////////////////// VARIABLES DE CONSULTA////////////////////////////////////
 
$where="";
$nombre=$_POST['xnombre'];
$carrera=$_POST['xcarrera'];
$limit=$_POST['xregistros'];
$id_alumno=$_POST['xid_alumno'];
 
////////////////////// BOTON BUSCAR //////////////////////////////////////
 
if (isset($_POST['buscar']))
{
 
 
 
	if (empty($_POST['xcarrera']))
	{
		$where="where nombre like '".$nombre."%'";
	}
 
	else if (empty($_POST['xnombre']))
	{
		$where="where id_alumno='".$id_alumno."'";
	}
	else if (empty($_POST['xid_alumno']))
 
	{
		$where="where carrera='".$carrera."'";
}
	else
	{
		$where="where nombre like '".$nombre."%' and carrera='".$carrera."%' and grupo='".$grupo."%' and id_alumno='".$id_alumno."'";
	}
}
/////////////////////// CONSULTA A LA BASE DE DATOS ////////////////////////
 
$alumnos="SELECT * FROM alumnos $where $limit";
$resAlumnos=$conexion->query($alumnos);
$resCarreras=$conexion->query($alumnos);
 
if(mysqli_num_rows($resAlumnos)==0)
{
	$mensaje="<h1>No hay registros que coincidan con su criterio de búsqueda.</h1>";
}
?>
<html lang="es">
 
	<head>
		<title>Filtro de Búsqueda PHP</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
 
		<link href="css/estilos.css" rel="stylesheet">
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
 
	</head>
	<body>
		<header>
			<div class="alert alert-info">
			<h2>Filtro de Búsqueda PHP</h2>
			</div>
		</header>
		<section>
			<form method="post">
				<input type="text" placeholder="Nombre..." name="xnombre"/>
				<select name="xcarrera">
					<option value="">Carrera </option>
					<?php
						while ($registroCarreras = $resCarreras->fetch_array(MYSQLI_BOTH))
						{
							echo '<option value="'.$registroCarreras['carrera'].'">'.$registroCarreras['carrera'].'</option>';
													}
					?>
				</select>
 
				<select name="xid_alumno">
					<option value="">id </option>
					<?php
						while ($registroC = $resCarreras->fetch_array(MYSQLI_BOTH))
						{
							echo '<option value="'.$registroCarreras['id_alumno'].'">'.$registroCarreras['id_alumno'].'</option>';
													}
					?>
				</select>
 
 
				<select name="xregistros">
					<option value="">No. de Registros</option>
					<option value="limit 3">3</option>
					<option value="limit 6">6</option>
					<option value="limit 9">9</option>
				</select>
				<button name="buscar" type="submit">Buscar</button>
			</form>
			<table class="table">
				<tr>
					<th>ID_Alumno</th>
					<th>Nombre</th>
					<th>Carrera</th>
					<th>Grupo</th>
				</tr>
 
				<?php
 
				while ($registroAlumnos = $resAlumnos->fetch_array(MYSQLI_BOTH))
				{
 
					echo'<tr>
						 <td>'.$registroAlumnos['id_alumno'].'</td>
						 <td>'.$registroAlumnos['nombre'].'</td>
						 <td>'.$registroAlumnos['carrera'].'</td>
						 <td>'.$registroAlumnos['grupo'].'</td>
						 </tr>';
				}
				?>
			</table>
 
			<?
				echo $mensaje;
			?>
		</section>
	</body>
</html>


Muchas gracias por la ayuda.
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 xve
Val: 3.943
Oro
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Combox

Publicado por xve (6935 intervenciones) el 01/02/2017 20:08:10
Tal cual lo tienes ahora esta bien!!!

Revisa el codigo fuente de la pagina, para ver que realmente los <select> tienen valores...
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