PHP Echo Table Column Alignment - php

I have this code that works fine in other pages that I made but doesn't work properly on my summary page.
<?php
//AAFES-date1
$sqlAAFES1 = "SELECT * FROM aafes WHERE dueDate ='$date1'";
$qAAFES1 = $pdo->prepare($sqlAAFES1);
$qAAFES1->execute(array($date1));
$dataAAFES1 = $qAAFES1->fetch(PDO::FETCH_ASSOC);
if ($dataAAFES1){
echo '<table class="table table-condensed table-hover">';
echo '<tr>';
foreach ($pdo->query($sqlAAFES1) as $rowAAFES1){
echo '<td width="60%">'.$rowAAFES1['facilityName'].'</td>';
echo '<td style="text-align:right" width="40%">'.$rowAAFES1['totalQty'].'</td>';
echo '</tr>';
echo '</table>';
};
};
?>
as you can see the 2nd row doesn't align with the first row.
while on the other page, I used the same foreach code, but alignment is perfect. So I wanna ask what seems to be the problem with this one.

Don't close foreach loop after table. Put <tr></tr> inside foreach loop.
if ($dataAAFES1){
echo '<table class="table table-condensed table-hover">';
foreach ($pdo->query($sqlAAFES1) as $rowAAFES1){
echo '<tr>';
echo '<td width="60%">'.$rowAAFES1['facilityName'].'</td>';
echo '<td style="text-align:right" width="40%">'.$rowAAFES1['totalQty'].'</td>';
echo '</tr>';
};
echo '</table>';
};

Related

display data in tabular format from database in wordpress page

I have look all day the whole internet and didn't find fetching data in tabular format. I have my short code and everything is ready, I have this code which is display data but not in good format as you can see in the screen shot I want data like this in WordPress page.
<?php
?>
<table border="1">
<tr>
<th>Char Item</th>
<th>Item Id</th>
<th>Item Description</th>
</tr>
<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM wp_orderlist" );
foreach ( $result as $print ) {
echo '<td>'. $print->char_item.'</td>';
echo '<td>'. $print->item_id.'</td>';
echo '<td>'. $print->Item_Description.'</td>';
}
?>
</table>
You forget to add tr tag. Try this:
echo '<tr>';
foreach ( $result as $print ) {
echo '<td>'. $print->char_item.'</td>';
echo '<td>'. $print->item_id.'</td>';
echo '<td>'. $print->Item_Description.'</td>';
}
echo '</tr>';

Display data from SQL query in an HTML table in PHP

I retrieved a list of data from an SQL database and now I would like to display it in a neat table rather than in a list. I managed to find a way to do this (probably not very elegant, though), but the column headers seem to be offset and I have not idea how to fix this.
I'm completely new to PHP, so any hints on how to solve this will be much appreciated!
echo '<table>';
echo '<tr>';
echo '<th>';
echo '<td>Word</td>';
echo '<td>Frequency</td>';
echo '</th>';
echo '</tr>';
$response = $db->query("SELECT * FROM frequencies WHERE freq BETWEEN 900 AND 910 ORDER BY freq");
while ($row = $response->fetch())
{
echo '<tr>';
echo '<td>'.$row['word'].'</td>';
echo '<td>'.$row['freq'].'</td>';
echo '</tr>';
}
echo '</table>';
$response->closeCursor();
A <th> element is a table header element and should be used instead of <td> (table data) element in your header row - it should never be a wrapper around <td> elements.
echo '<table>';
echo '<tr>';
echo '<th>Word</th>';
echo '<th>Frequency</th>';
echo '</tr>';
I prefer combining php and html
<table >
<thead>
<tr>
<th >Word</th>
<th >Frequency</th>
</tr>
</thead>
<?php
$response = $db->query("SELECT * FROM frequencies WHERE freq
BETWEEN 900 AND 910 ORDER BY freq");
?>
<tbody>
<?php
while ( $row = $response->fetch()) {
?>
<tr>
<td><?php echo $row['word']; ?></td>
<td><?php echo $row['freq']; ?></td>
</tr>
<?php }
$response->closeCursor();
?>
</tbody>
</table>

PHP - How to use Foreach to turn a list into a column rather than a row of items?

I have a database, and am trying to get a list using PHP with foreach, but the list are returning like this:
RiquelmeMacielLewBrMarcuus
But it like so:
RiquelmeMaciel
LewBr
Marcuus
My code:
<?php
include_once("conexao.php");
$select = "SELECT * FROM usuarios ORDER BY id";
$query = mysqli_query($conn, $select);
$rows = mysqli_fetch_assoc($query);
?>
<?php foreach($rows as $row){
echo '<td class="mdl-data-table__cell--non-numeric name">'.$row['nome'].'</td>';
} ?>
You need to insert table rows to create the rows:
<?php
//Incluindo a conexão com banco de dados
include_once("conexao.php");
$result_usuario = "SELECT * FROM usuarios ORDER BY id";
$resultado_usuario = mysqli_query($conn, $result_usuario);
$resultado = mysqli_fetch_assoc($resultado_usuario);
foreach($resultado_usuario as $teste){
echo '<tr>';
echo '<td class="mdl-data-table__cell--non-numeric name">'.$teste['nome'].'</td>';
echo '<td class="mdl-data-table__cell--non-numeric"></td>';
echo '<td class="mdl-data-table__cell--non-numeric"></td>';
echo '</tr>';
}
Now you just need to add in your variables for 'EMAIL' and 'SENHA'.
The problem in your foreach cycle - for eache element you output tag, but is a table cell tag.
If you want fill first column in your 3-column table, you must do something like this:
<?php foreach($resultado_usuario as $teste){
echo '<tr>';
echo '<td class="mdl-data-table__cell--non-numeric name">'.$teste['nome'].'</td>';
echo '<td>second column</td>';
echo '<td>third column</td>';
echo '</tr>';
} ?>
Your are displaying your data the wrong way, your <td> should be inside <tr> which should be inside a <table>:
<table>
<tr>
<th>Nome</th>
<th>Email</th>
<th>Senha</th>
</tr>
<?php
foreach ($resultado_usuario as $teste){
echo '<tr>';
echo '<td class="mdl-data-table__cell--non-numeric name">'.$teste['nome'].'</td>';
echo '<td class="mdl-data-table__cell--non-numeric email">'.$teste['email'].'</td>';
echo '<td class="mdl-data-table__cell--non-numeric senha">'.$teste['senha'].'</td>';
echo '</tr>';
}
?>
</table>
The data will then be displayed as you wanted it. Have a look at HTML - table.
If you want to change the order of your data from your SQL query, use the ORDER BY ... ASC|DESC.

PHP foreach using $variable->property

I have some code that runs reports from $results returning from mysql query.
The queries are relatively standard, but their total number is dynamic. So far I have static HTML where it has the same code repeated, but thats only if ok the number of queries == the number of markup repetitions.
So, I am trying to implement a new foreach loop that will determine how many "queries" there are, and only markup for each one, and then $total at the end.
To date I have had static php foreach loop like this, works well.
<div class="row">
<div class="col-xs-12">
<table class="table table-striped">
<thead>
<tr>
<th>Year</th>
<th>Month</th>
<th>Item</th>
<th class="text-right">Minimum</th>
<th class="text-right">Maximum</th>
<th class="text-right">Change</th>
</tr>
</thead>
<tbody>
<?php foreach ($results as $result) :
if ($result->id == 151){
echo '<tr>';
echo '<td>', $result->Year, '</td>';
echo '<td>', $result->Month, '</td>';
echo '<td class="text-centre">', $result->name, '</td>';
echo '<td class="text-right">', $result->min, '</td>';
echo '<td class="text-right">', $result->max, '</td>';
echo '<td class="text-right">', $result->cumulative, '</td>';
$thirdtotal += $result->change;
$total += $result->change;
echo '</tr>';
}
endforeach; ?>
Where I am stuck is, do i put the above inside another for each loop? My thought is, if a query exists, it will have an "id" field in the array, so for the number of "id", thats how many outputs you'll get.
<?php foreach ($results as $key => $value;) :
{
echo '<tr>';
echo '<td>' $value, '</td>';
echo '</tr>';
}
endforeach; ?>
This piece of test code doesnt work, am I using the key value correctly? Is it the correct approach to solve this problem and reduce the amount of php in the controller?
Thanks

Change cell color based on MYSQL value

I want to change td color using if statement but somehow my code is not affecting all rows
this is my code :
require_once("../model/materiel.class.php" . "");
$mt=new materiel();
$data=$mt->afficher_tous1();
echo '<table id="customers2" class="table datatable table-striped">';
echo "<thead>
<tr>
<th>Qte disponible</th>
<th>Alert</th>
</tr>
</thead>";
echo "<tbody>";
foreach($data as $t){
echo "<tr>";
if ($t['qte_disponible_m'] == 0){
echo "<td bgcolor='red'>".$t['qte_disponible_m']."</td>";
}else if ($t['qte_disponible_m'] > $t['alert_m']){
echo "<td bgcolor='green'>".$t['qte_disponible_m']."</td>";
}else if ($t['qte_disponible_m'] == $t['alert_m']){
echo "<td bgcolor='yellow'>".$t['qte_disponible_m']."</td>";
}
echo "<td>".$t['alert_m']."</td>";
echo "</tr>";
}
echo "</tbody>";
echo"</table>";
the problem i have see the screenshot below :
If statement is like jumping next row
Add a class with the CSS background-color property (with !important if needed) to the TD instead of bgcolor. The bgcolor gets overwritten by the table-striped class.

Categories