How can I display the data of a PHP query in an HTML table?

0

Greetings, I have the following problem, it turns out that I am making a query to my database with different fields, and I am getting the results as follows.

    while($fila=mysqli_fetch_array($resultados, MYSQLI_ASSOC)){

echo "<table class= main-container>";
echo $fila ['COD_CONSULTA']. "";
echo $fila ['CED_PAC']. "";
echo $fila ['NOM_PAC']. "";
echo $fila ['APE_PAC']. "";
echo $fila ['SEX_PAC']. "";
echo $fila ['ALT_PAC']. "";
echo $fila ['PESO_PAC']. "";
echo $fila ['FECHA']. "";
echo $fila ['TIPO_CONSULTA']. "";
echo $fila ['SINTOMAS']. "";
echo $fila ['OBSERV']. "";
echo $fila ['HIS_PAC']. "";
echo $fila ['MEDI_PAC']. "";
echo $fila ['OPERADO']. "";
echo $fila ['ALERGIAS']. "</table>";

echo "<br> ";
echo "<br> ";
}

mysqli_close($conexion);

}

Well, I have a table created in HTML and I want those results to be shown to me in the table so I can apply a CSS style and make it look nice Does anyone know how?

This is my table.

<div id="main-container">
<table>
	<thead>
		<tr>
			<th>Codigo Consulta</th><th>Cedula</th><th>Nombres</th><th>Apellidos</th><th>Estatura</th><th>Peso</th><th>Tipo consulta</th><th>Sintomas</th><th>Observaciones</th><th>Medicamentos actuales</th><th>Alergias</th>
		</tr>
	</thead>

	<tr>
		<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>
	</tr>

</div>

EDIT: This is the code of the whole page in case someone wants to see it (Here I still do not have the table added, if someone can help me with that, because I have in the first place a box of search and when making the query reload the page and shows the query)

<?php 

	function ejecuta_consulta($labusqueda){
	include("conexiond.php");

	$conexion= mysqli_connect($db_host,$db_usuario,$db_contra);

	if(mysqli_connect_errno()){

		echo "Fallo al conectar con la base de datos";
		exit();
	}

	mysqli_select_db($conexion,$db_nombre) or die ("No se encuentra la base de datos.");


	$consulta = "SELECT datosbasicos.CED_PAC,datosbasicos.NOM_PAC,datosbasicos.APE_PAC,datosbasicos.SEX_PAC,datosmedicos.COD_CONSULTA,datosmedicos.ALT_PAC,datosmedicos.PESO_PAC,datosmedicos.FECHA,datosmedicos.TIPO_CONSULTA,datosmedicos.SINTOMAS,datosmedicos.OBSERV,datosmedicos.HIS_PAC,datosmedicos.MEDI_PAC,datosmedicos.OPERADO,datosmedicos.ALERGIAS FROM datosbasicos INNER JOIN datosmedicos ON datosbasicos.CED_PAC=datosmedicos.CED_PAC WHERE datosbasicos.CED_PAC LIKE '%$labusqueda%'";

	$resultados = mysqli_query($conexion,$consulta);

	while($fila=mysqli_fetch_array($resultados, MYSQLI_ASSOC)){

	echo "<table class= main-container>";
	echo $fila ['COD_CONSULTA']. "";
	echo $fila ['CED_PAC']. "";
	echo $fila ['NOM_PAC']. "";
	echo $fila ['APE_PAC']. "";
	echo $fila ['SEX_PAC']. "";
	echo $fila ['ALT_PAC']. "";
	echo $fila ['PESO_PAC']. "";
	echo $fila ['FECHA']. "";
	echo $fila ['TIPO_CONSULTA']. "";
	echo $fila ['SINTOMAS']. "";
	echo $fila ['OBSERV']. "";
	echo $fila ['HIS_PAC']. "";
	echo $fila ['MEDI_PAC']. "";
	echo $fila ['OPERADO']. "";
	echo $fila ['ALERGIAS']. "</table>";

	echo "<br> ";
	echo "<br> ";
	}

	mysqli_close($conexion);

	}
?>
<!DOCTYPE html>
<html>
<head>
	<title>Sistema de historias médicas - Dr. Darling Davila</title>
	<meta charset="utf-8">
	<link rel="stylesheet" href="css/bootstrap.css">
	<link rel="stylesheet" type="text/css" href="css/estilo.css">
	<link href="https://fonts.googleapis.com/css?family=Lato|Roboto" rel="stylesheet">
</head>
<body>
	<?php
		$mibusqueda=$_GET["buscar"];

		$mipag=$_SERVER["PHP_SELF"];

		if($mibusqueda!=NULL){
			ejecuta_consulta($mibusqueda);
		}else{
			echo ("<form action='". $mipag . "' method='GET'>
				<img src='imagenes/header.png'>
					<h2>Busqueda de paciente</h2>
					<div class='contenedor'>
					<input type='text' name='buscar' class='input-100 text-center inline-block col-md-6 btn-enviar espacio-arriba'></label>

					<input type='submit' name='enviando' value='Consulta' class='text-center inline-block col-md-12 espacio-arriba btn-enviar'>
				</div>
				</form>");
		}
	?>

 
<!--</body>  // Esta seria la tabla que me debe aparecer al hacer la consulta, no se como agregarla.
</html>

<div id="main-container">
<table>
	<thead>
		<tr>
			<th>Codigo Consulta</th><th>Cedula</th><th>Nombres</th><th>Apellidos</th><th>Estatura</th><th>Peso</th><th>Tipo consulta</th><th>Sintomas</th><th>Observaciones</th><th>Medicamentos actuales</th><th>Alergias</th>
		</tr>
	</thead>

	<tr>
		<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>
	</tr>

</div>
    
asked by Pablo Pernia 20.08.2017 в 07:13
source

1 answer

1

You can make the function ejecuta_consulta return an array ( array ) with the patient data and then run it through a loop to print each element within the corresponding column ( td ).

<?php

    function ejecuta_consulta($labusqueda)
    {
        include("conexiond.php");

        $conexion= mysqli_connect($db_host, $db_usuario, $db_contra);

        if (mysqli_connect_errno()) {
            echo "Fallo al conectar con la base de datos";
            exit();
        }

        mysqli_select_db($conexion, $db_nombre) or die("No se encuentra la base de datos.");


        $consulta = "SELECT datosbasicos.CED_PAC,datosbasicos.NOM_PAC,datosbasicos.APE_PAC,datosbasicos.SEX_PAC,datosmedicos.COD_CONSULTA,datosmedicos.ALT_PAC,datosmedicos.PESO_PAC,datosmedicos.FECHA,datosmedicos.TIPO_CONSULTA,datosmedicos.SINTOMAS,datosmedicos.OBSERV,datosmedicos.HIS_PAC,datosmedicos.MEDI_PAC,datosmedicos.OPERADO,datosmedicos.ALERGIAS FROM datosbasicos INNER JOIN datosmedicos ON datosbasicos.CED_PAC=datosmedicos.CED_PAC WHERE datosbasicos.CED_PAC LIKE '%$labusqueda%'";

        $resultados = mysqli_query($conexion, $consulta);

        $filas = array(); // Crea la variable $filas y se le asigna un array vacío
        // (Si la consulta no devuelve ningún resultado, la función por lo menos va a retornar un array vacío)

        while ($fila=mysqli_fetch_array($resultados, MYSQLI_ASSOC)) {
            $filas[] = $fila; // Añade el array $fila al final de $filas
        }

        mysqli_close($conexion);

        return $filas; // Devuelve el array $filas
    }
?>

<!DOCTYPE html>
<html>

<head>
    <title>Sistema de historias médicas - Dr. Darling Davila</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="css/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="css/estilo.css">
    <link href="https://fonts.googleapis.com/css?family=Lato|Roboto" rel="stylesheet">
</head>

<body>
    <?php
        $mibusqueda=$_GET["buscar"];

        $mipag=$_SERVER["PHP_SELF"];

        if ($mibusqueda!=null) {
            $pacientes = ejecuta_consulta($mibusqueda);
    ?>

        <div id="main-container">
            <table>
                <thead>
                    <tr>
                        <th>Codigo Consulta</th>
                        <th>Cedula</th>
                        <th>Nombres</th>
                        <th>Apellidos</th>
                        <th>Estatura</th>
                        <th>Peso</th>
                        <th>Tipo consulta</th>
                        <th>Sintomas</th>
                        <th>Observaciones</th>
                        <th>Medicamentos actuales</th>
                        <th>Alergias</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    // Si la variable $pacientes esta definida y no está vacía
                    if (isset($pacientes) && !empty($pacientes)) {
                        // Recorre cada $paciente dentro del array $pacientes
                        foreach ($pacientes as $paciente) {
                            ?>
                        <tr>
                            <td><?php echo $paciente['COD_CONSULTA'] ?></td>
                            <td><?php echo $paciente['CED_PAC'] ?></td>
                            <td><?php echo $paciente['NOM_PAC'] ?></td>
                            <td><?php echo $paciente['APE_PAC'] ?></td>
                            <td><?php echo $paciente['SEX_PAC'] ?></td>
                            <td><?php echo $paciente['ALT_PAC'] ?></td>
                            <td><?php echo $paciente['PESO_PAC'] ?></td>
                            <td><?php echo $paciente['FECHA'] ?></td>
                            <td><?php echo $paciente['TIPO_CONSULTA'] ?></td>
                            <td><?php echo $paciente['SINTOMAS'] ?></td>
                            <td><?php echo $paciente['OBSERV'] ?></td>
                            <td><?php echo $paciente['HIS_PAC'] ?></td>
                            <td><?php echo $paciente['HIS_PAC'] ?></td>
                            <td><?php echo $paciente['MEDI_PAC'] ?></td>
                            <td><?php echo $paciente['OPERADO'] ?></td>
                            <td><?php echo $paciente['ALERGIAS'] ?></td>
                        </tr>
                    <?php
                        }
                    } ?>
                </tbody>
        </div>
    <?php
        } else {
            echo("<form action='". $mipag . "' method='GET'>
                <img src='imagenes/header.png'>
                    <h2>Busqueda de paciente</h2>
                    <div class='contenedor'>
                    <input type='text' name='buscar' class='input-100 text-center inline-block col-md-6 btn-enviar espacio-arriba'></label>

                    <input type='submit' name='enviando' value='Consulta' class='text-center inline-block col-md-12 espacio-arriba btn-enviar'>
                </div>
                </form>");
        }
     ?>

</body>

</html>
    
answered by 20.08.2017 / 08:10
source