Queries in PHP and MYSQL

0

I make a payroll system which I have problems with the query module I have the tables called worker that is my main table, hence the foreign keys of my tables "area", "position" and cg_turno. In my insert module I have no problem as I insert by description and in my BD they are inserted by ID. What I need is that it shows the description in the graphical interface since in my BD it is registered only with the IDs of the foreign keys. Thanks / Regards.

<!DOCTYPE html>
<?php
session_start();
if (@!$_SESSION['user']) {
	header("Location:index.php");
}elseif ($_SESSION['rol']==2) {
	header("Location:index2.php");
}
?>
<html lang="en">
  

  
    <meta charset="utf-8">
    <title>Gesti&oacuten de Nómina</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    

    <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet"/>

    <link rel="shortcut icon" href="assets/ico/favicon.ico">
    <link rel="apple-touch-icon-precomposed" sizes="144x144" href="assets/ico/apple-touch-icon-144-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="114x114" href="assets/ico/apple-touch-icon-114-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="72x72" href="assets/ico/apple-touch-icon-72-precomposed.png">
    <link rel="apple-touch-icon-precomposed" href="assets/ico/apple-touch-icon-57-precomposed.png">
  </head>
<body data-offset="40" background="images/fondotot.jpg" style="background-attachment: fixed">

<div id="page">
			<div class="header">
				<a href="#menu"></a>
				Sistema Integral de Nómina
			</div><br> </br>
			


<div class="container">
<header class="header">
<div class="row">
	<?php
	include("include/cabecera.php");
	?>
</div>
</header>

  <!-- Navbar
    ================================================== -->

<div class="navbar">
  <div class="navbar-inner">
	<div class="container">
	  <div class="nav-collapse">
		<ul class="nav">
			<li class=""><a href="index2.php">BIENVENIDO</a></li>
			 
	
		</ul>
		<form action="#" class="navbar-search form-inline" style="margin-top:6px">
		
		</form>
		<ul class="nav pull-right">
		<li><a href="">Bienvenido <strong><?php echo $_SESSION['user'];?></strong> </a></li>
			  <li><a href="desconectar.php"> Cerrar Sesión </a></li>			 
		</ul>
	  </div><!-- /.nav-collapse -->
	</div>
  </div><!-- /navbar-inner -->
</div>

<br />

			 
             <p>&nbsp;&nbsp;&nbsp<strong>Impresi&oacute;n de Reporte</strong>
             <a href="app/reportes/reporte_trabajador.php" target="_blank"><img src="images/printer.png"  width="50" height="50" 



<!-- ======================================================================================================================== -->
<div class="row">
	
	
		
	<div class="span12">

		<div class="caption">
		
<!--///////////////////////////////////////////////////Empieza cuerpo del documento interno////////////////////////////////////////////-->
		<center><h2> Administración de Trabajadores Registrados</h2></center>
		<div class="well well-small">
		<hr class="soft"/>
		<center><h4>Trabajadores Registrados</h4></center>
		<div class="row-fluid">
		



			<?php

				require("connect_db.php");
				$sql=("SELECT * FROM trabajador");
	
//la variable  $mysqli viene de connect_db que lo traigo con el require("connect_db.php");
				$query=mysqli_query($mysqli,$sql);

				echo "<table border='1'; class='table table-hover';>";
					echo "<tr class='warning'>";
					   echo "<td>No. de Control</td>";
						echo "<td>Nombre</td>";
						echo "<td>Apellido Paterno</td>";
						echo "<td>Apellido Materno</td>";
						echo "<td>NSS</td>";
						echo "<td>CURP</td>";
						echo "<td>RFC</td>";
						echo "<td>Puesto</td>";
						echo "<td>Area</td>";
						echo "<td>Turno</td>";
						echo "<td>Empresa</td>";
						echo "<td>Editar</td>";
						
					echo "</tr>";

			    
			?>
			  
			<?php 
				 while($arreglo=mysqli_fetch_array($query)){
				  	echo "<tr class='success'>";
				    	echo "<td>$arreglo[0]</td>";
				    	echo "<td>$arreglo[1]</td>";
						echo "<td>$arreglo[2]</td>";
						echo "<td>$arreglo[3]</td>";
						echo "<td>$arreglo[4]</td>";
						echo "<td>$arreglo[5]</td>";
						echo "<td>$arreglo[6]</td>";
						echo "<td>$arreglo[7]</td>";
						echo "<td>$arreglo[8]</td>";
						echo "<td>$arreglo[9]</td>";
						echo "<td>$arreglo[10]</td>";
				    	

				    	echo "<td><a href='actualizar_trabajador.php?id_control=$arreglo[0]'><img src='images/actualizar.gif' class='img-rounded'></td>";
						
						

						
					echo "</tr>";
				}


			?>
			
				  
			  			  
			  
		
		
		<div class="span8">
		
		</div>	
		</div>	
		<br/>
		


		<!--EMPIEZA DESLIZABLE-->
		
		 <!--TERMINA DESLIZABLE-->



		
		
		</div>

		


		

<!--///////////////////////////////////////////////////Termina cuerpo del documento interno////////////////////////////////////////////-->
</div>

	</div>
</div>
<!-- Footer
      ================================================== -->


</div><!-- /container -->

    <!-- Le javascript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="bootstrap/js/jquery-1.8.3.min.js"></script>
    <script src="bootstrap/js/bootstrap.min.js"></script>
	</style>
	
	<?php
	    include("includes/menus.php"); ?>
    	</div>
    	<!-- Placed at the end of the document so the pages load faster -->
		<!--<script src="includes/jquery-1.9.1.min.js"></script>-->
		<script src="js/bootstrap/js/bootstrap.min.js"></script>
		<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
		<script src="js/ie10-viewport-bug-workaround.js"></script>
		<!-- Add in any FooTable dependencies we may need -->
		<script src="js/moment.min.js"></script>
		<!-- Add in FooTable itself -->
		<script src="js/compiled/footable.js"></script>
		<!-- Initialize FooTable -->
		<script>
			jQuery(function($){
				$('.table').footable();
			});
		</script>
  </body>
  </html>
    
asked by Oscar_DR 14.02.2018 в 09:27
source

1 answer

0

To obtain data from different related tables you can join (JOIN) each table using the columns that put them in relation:

For example:

SELECT 
        t.nombre,
        t.apellido,
        a.descripcion_area, 
        p.descripcion_puesto 
FROM trabajador t 
INNER JOIN area   a ON t.id_area=a.id_area 
INNER JOIN puesto p ON t.id_puesto=p.id_puesto

In this consultation you can:

  • get the fields you need from any of the tables. You just have to put the alias of the table, point, column name and you're done.
  • note that we have used an alias for each table, intuitively the first letter that each table would represent. It is not necessary to use ...FROM tabla as t , with putting the name of the table followed by the alias is enough. This practice simplifies the query, especially when there are several columns and / or table names that are too long, so that you do not have to repeat in each% nombre_tabla.col1, nombre_otra_tabla.colX...
  • the use of INNER JOIN means that it will bring only the records that have matches in both joined tables. If you want to bring also those that do not have matches in any of the tables you can use LEFT JOIN ... There are other types of JOIN , you can study them in the MySQL documentation or in some of the Stackoverflow questions / answers, for example is: What is the difference between an inner and an outer join? .
  • you will thus obtain a set of results that you can read by programming and present as you need it.

I hope it serves you.

    
answered by 14.02.2018 в 14:06