Reputation: 5
Let me see if I can explain what I'm looking to do as clear as possible, since I'm not a programmer and I only understand basics.
In my office I use many queries to database (PostgreSQL) instead of looking for the data in many reports and then put all the conditions. I use NAVICAT for postgres to make the queries and then I export all the data to Excel. I would like that everyone could get the data and do my controlling job.
So, what I'm looking is to get the exact same result I get on NAVICAT or any SQL statement app. For this case, I have to consult tracking numbers on 1 table which has also have some shared data in other tables. My query is fine and it works perfectly.
In order to do this, I've created a textarea (named folio) where the tracking numbers must be typed. After this, I've found the way to make a string where every tracking number single quoted and comma separated (sorry if there's redundance on the code, I've been reading, copying, pasting and adapting until it worked:
if (isset($_POST["folio"])){
$folio=$_POST["folio"];
$lines= explode("|", preg_replace( "/([\r\n])+/", "|", $folio));
$output = "'".join("',\n'", $lines)."'";
Once with this, I make the connection to database and run the query, where $output is the result:
$conn = pg_connect($conn_string);
if (!$conn) {
$mensaje="Error de conexion.";
}
$result = pg_query($conn,"
SELECT
public.guias_detalles.guia,
public.estados.descripcion,
public.guias_detalles.fec_ent,
public.guias_detalles.hor_ent,
public.guias_detalles.resp_ent,
public.funcionarios.descripcion as driver
FROM
public.guias_detalles
LEFT JOIN public.estados ON public.guias_detalles.estado = public.estados.estado
LEFT JOIN public.mmensajeros ON public.guias_detalles.mmensajero = public.mmensajeros.mmensajero
LEFT JOIN public.funcionarios ON public.mmensajeros.funcionario = public.funcionarios.funcionario
WHERE
public.guias_detalles.guia IN
($output)");
if (!$result) {
$mensaje="Ha ocurrido un error de consulta.";
}
}
From this query I get the 6 values for each valid tracking number.
Now, to print this on the screen, I've seen this Table Thead and Tbody as the simple way to display the results like any SQL app, 6 columns and one row per tracking. To get this, this is what I've made:
<table width="100%" border="1" cellspacing="1" cellpadding="1" >
<thead border="1" cellspacing="1">
<tr style="color: white;">
<td>Guia</td>
<td>Estado</td>
<td>Fecha Entrega</td>
<td>Hora Entrega</td>
<td>Recibido por</td>
<td>Driver</td>
</tr>
</thead>
<tbody style="color: white;" border="1" cellspacing="1">
<?php
while ($row = pg_fetch_assoc($result)) {
$newline = "\r\n";
$newline;
echo '<td>'.$row['guia'];
echo '<td>'.$row['descripcion'];
echo '<td>'.$row['fec_ent'];
echo '<td>'.$row['hor_ent'];
echo '<td>'.$row['resp_ent'];
echo '<td>'.$row['driver']. $newline;
}
?>
</tbody>
</table>
As anyone can see, I've tried to break the line without luck. The results are printed in the same line, there's no line break.
I've also tried to use the foreach instead the while, but even I've readed the conditions, I don't really understand how to aplly it. I know there's a lot I'm missing here, but please if anyone could give me a hand I'll really appreciate it. Below a printscreen of the result I get, example is for 3 tracking numbers, 6 results for each, no line break.
Upvotes: 0
Views: 228
Reputation: 3638
You have to close the <td>
elements with </td>
.
At the moment your code produces something like this:
<tbody>
<td>123
<td>456
<td>789
</tbody>
This is not what a valid HTML table looks like. You need a row (<tr>
) for each entry and close the cells.
Something like this should do it:
<tbody style="color: white;" border="1" cellspacing="1">
<?php
while ($row = pg_fetch_assoc($result)) {
echo '<tr>';
echo '<td>'.$row['guia'].'</td>';
echo '<td>'.$row['descripcion'].'</td>';
echo '<td>'.$row['fec_ent'].'</td>';
echo '<td>'.$row['hor_ent'].'</td>';
echo '<td>'.$row['resp_ent'].'</td>';
echo '<td>'.$row['driver'].'</td>';
echo '</tr>';
}
?>
Upvotes: 0
Reputation: 2039
It is a html table: You have to add a <tr> at the beginning of each while iteration and </tr> at the end, and close de <td> as </td> of each cell; like in the <thead>.
while ($row = pg_fetch_assoc($result)) {
echo '<tr>';
echo '<td>'.$row['guia'].'</td>';
echo '<td>'.$row['descripcion'].'</td>';
echo '<td>'.$row['fec_ent'].'</td>';
echo '<td>'.$row['hor_ent'].'</td>';
echo '<td>'.$row['resp_ent'].'</td>';
echo '<td>'.$row['driver'].'</td>';
echo '</tr>';
}
Upvotes: 1