Why is the out output is in columns not rows. I basically want the rows to be alternating colors, which I found out is not working - seems to be columns.
<?php
$count = 0;
$input = 'https://www.fdic.gov/bank/individual/failed/banklist.csv';
echo "<html><body><table width=250>";
echo "<th bgcolor=#222937><FONT COLOR=WHITE SIZE=3>Bank Name</FONT></th>";
echo "<th bgcolor=#222937><FONT COLOR=WHITE SIZE=3>City</FONT></th>";
echo "<th bgcolor=#222937><FONT COLOR=WHITE SIZE=3>Acq. Institution</FONT></th>";
echo "<th bgcolor=#222937><FONT COLOR=WHITE SIZE=3>Closing Date</FONT></th>";
if (false !== ($ih = fopen($input, 'r')))
{
fgetcsv($ih);
while (false !== ($data = fgetcsv($ih)))
{
$outputData = array($data[0], $data[1], $data[4], $data[5]);
echo "<tr>";
foreach ($outputData as $row)
{
if($count % 2 == 0)
$rowColor = '#000000';
else
$rowColor = '#222937';
echo "<td bgcolor='. $rowColor . '><FONT COLOR=D3AB04 SIZE=2>" . htmlspecialchars($row) . "</FONT></td>";
$count++;
}
echo "</tr>";
}
fclose($ih);
echo "</table></body></html>";
}
?>
Use below code
<?php
$count = 0;
$input = 'https://www.fdic.gov/bank/individual/failed/banklist.csv';
echo "<html><body><table width='250' class='TFtable' >";
echo "<tr>";
echo "<th bgcolor=#222937><FONT COLOR=WHITE SIZE=3>Bank Name</FONT></th>";
echo "<th bgcolor=#222937><FONT COLOR=WHITE SIZE=3>City</FONT></th>";
echo "<th bgcolor=#222937><FONT COLOR=WHITE SIZE=3>Acq. Institution</FONT></th>";
echo "<th bgcolor=#222937><FONT COLOR=WHITE SIZE=3>Closing Date</FONT></th>";
echo "</tr>";
if (false !== ($ih = fopen($input, 'r')))
{
fgetcsv($ih);
while (false !== ($data = fgetcsv($ih)))
{
$outputData = array($data[0], $data[1], $data[4], $data[5]);
echo "<tr>";
foreach ($outputData as $row)
{
echo "<td><FONT COLOR=D3AB04 SIZE=2>" . htmlspecialchars($row) . "</FONT></td>";
$count++;
}
echo "</tr>";
}
fclose($ih);
echo "</table></body></html>";
}
?>
and CSS
<style type="text/css">
.TFtable{
width:100%;
border-collapse:collapse;
}
.TFtable td{
padding:7px; border:#4e95f4 1px solid;
}
/* provide some minimal visual accomodation for IE8 and below */
.TFtable tr{
background: #b8d1f3;
}
/* Define the background color for all the ODD background rows */
.TFtable tr:nth-child(odd){
background: #b8d1f3;
}
/* Define the background color for all the EVEN background rows */
.TFtable tr:nth-child(even){
background: #dae5f4;
}
</style>
Related
I need to get data from an CSV file to an HTML table with php. I've did it with filtering and that works like a charm. Now i tried adding it if $filter isn't there but for some reason it puts it outside the table.
my code for getting $filter:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$filter = $_POST["search"];
}
so there wont be any variable with the name $filter if there is no POST request.
here is my code for getting the csv data to a HTML table:
if (($HandleAnalog = fopen("csv/Analog/ADC_DAC.csv", "r")) !== FALSE) {
echo "<table id='ADC_DAC' style='width:100%; border: 1px solid black;'>";
while (($data = fgetcsv($HandleAnalog, 1000, ";")) !== FALSE) {
$num = count($data);
$row++;
if(strpos($data[4],$filter) !== false || strpos($data[2],$filter) !== false ||strpos($data[5],$filter) !== false){
echo "<tr style='border: 1px solid black;'>";
for ($c=0; $c < $num; $c++) {
if ($row == 0) {
echo "<th style='border: 1px solid black;'>";
$dat = str_replace(',', '.', $data[$c]);
echo $dat;
echo "</th>";
$row++;
}
echo "<td style='border: 1px solid black;'>";
$dat = str_replace(',', '.', $data[$c]);
echo $dat;
echo "</td>";
}
echo "</tr>";
}else if ($filter == null) {
echo "<tr style='border: 1px solid black;'>";
for ($c=0; $c < $num; $c++) {
if ($row == 0) {
echo "<th style='border: 1px solid black;'>";
$dat = str_replace(',', '.', $data[$c]);
echo $dat;
echo "</th>";
$row++;
}
echo "<td style='border: 1px solid black;'>";
$dat = str_replace(',', '.', $data[$c]);
echo $dat;
echo "</td>";
}
echo "</tr>";
}
}
echo "</table>";
}
fclose($HandleAnalog);
But for some reason it doesn't show the data when I don't filter it, while I did exactly the same.
How it looks when i Filter:
How it looks when I don't filter:
And if I look at the elements in my browser I see that it closes the table before the non-filtered values are.
Thanks in advance :)
Please initialize $filter, because you are testing for an unset variable (several warnings or notices can impact PHP performance, even if error reporting is disabled)
Initialize $filter to null
First evaluate for the absence of $filter (Just my suggestion, not a rule, but would be faster to ommit calling functions when there is nothing to search...)
So:
$filter = null;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$filter = $_POST["search"];
}
echo "<table id='ADC_DAC' style='width:100%; border: 1px solid black;'>";
while (($data = fgetcsv($HandleAnalog, 1000, ";")) !== FALSE) {
$num = count($data);
$row++;
if ($filter == null) {
echo "<tr style='border: 1px solid black;'>";
for ($c=0; $c < $num; $c++) {
if ($row == 0) {
echo "<th style='border: 1px solid black;'>";
$dat = str_replace(',', '.', $data[$c]);
echo $dat;
echo "</th>";
$row++;
}
echo "<td style='border: 1px solid black;'>";
$dat = str_replace(',', '.', $data[$c]);
echo $dat;
echo "</td>";
}
echo "</tr>";
}else if(strpos($data[4],$filter) !== false || strpos($data[2],$filter) !== false ||strpos($data[5],$filter) !== false){
echo "<tr style='border: 1px solid black;'>";
for ($c=0; $c < $num; $c++) {
if ($row == 0) {
echo "<th style='border: 1px solid black;'>";
$dat = str_replace(',', '.', $data[$c]);
echo $dat;
echo "</th>";
$row++;
}
echo "<td style='border: 1px solid black;'>";
$dat = str_replace(',', '.', $data[$c]);
echo $dat;
echo "</td>";
}
echo "</tr>";
}
}
echo "</table>";
}
fclose($HandleAnalog);
The following code reads a .csv file and generates an HTML table. I want to decide which column (field) will have all data aligned left or center. How can I do that?
<?php
$colhead = "#9BBB59";
$colrow = "#D7E4BC";
echo "<table style=\"text-align: left; margin-left: auto; margin-right: auto; font-family: Helvetica,Arial,sans-serif; border: 1px solid black; padding:1px; border-spacing: 1px; border-collapse: separate; frame=\"border\" rules=\"none\">";
echo "<tbody>";
$row = 1;
if (($handle = fopen("myfile.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$num = count($data);
if ($row == 1) {
echo "<tr>";
}else{
echo "<tr>";
}
for ($c=0; $c < $num; $c++) {
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
}
if ($row == 1) {
// ------------- HEADER
echo "<td style=\"background-color:$colhead; text-align:center; vertical-align:middle; color:#ffffff; font-size:16px; font-weight:bold; padding:4px;\"> ".$value." </td>";
}else{
// ------------- GENERIC ROW
echo "<td style=\"background-color:$colrow; text-align:left; vertical-align:middle; color:#000000; font-size:14px; padding:4px; border:0px solid\"> ".$value." </td>";
}
}
if ($row == 1) {
echo '</tr>';
}else{
echo '</tr>';
}
$row++;
}
echo '</tbody></table>';
echo '</center>';
fclose($handle);
}
?>
<?php
$dom = new DomDocument();
$dom -> load("file.xml");
$data = $dom->getElementsByTagName('Data');
$counter = 0; // Set the entry counter
echo( "<table>");
foreach($data as $node) {
if ($counter % 3 == 0) {
echo '<tr>';
}
echo "<td>". $node -> textContent . "<td>";
if($counter % 3 == 0) {
echo '</tr>';
}
$counter++; // Increment the counter
}
echo( "</table>");
?>
It's not the cleanest code, but should work.
I would like to display two picture variables in a single cell
http://i.imgur.com/9ZuRhnD.gif
$result = mysqli_query($query)
or die(mysql_error());
if (mysqli_num_rows($result) > 0) {
echo "<table border='0' style='border-collapse: collapse;border-color: white;'>";
$i = 0;
while($row = mysqli_fetch_array($result)){
$Limg = $row['Limg'];
$Limgt = $row['Limgt'];
if ($i==0) {
echo "<tr>\n";
}
echo "<td align='center' width='140'>" . "<img src=\"{$row['Limgt']}\">" ."</td>";
echo "<td align='center' width='40'>" . "<img src=\"{$row['Limg']}\">" ."</td>";
$i++;
if ($i == $items) {
echo "</tr>\n";
$i = 0;
}
}
echo '</table>';
}else {
echo 'no records found';
}
?>
What am I missing
You're putting them each in their own cell () - try this:
$result = mysqli_query($query)
or die(mysql_error());
if (mysqli_num_rows($result) > 0) {
echo "<table border='0' style='border-collapse: collapse;border-color: white;'>";
$i = 0;
while($row = mysqli_fetch_array($result)){
$Limg = $row['Limg'];
$Limgt = $row['Limgt'];
if ($i==0) {
echo "<tr>\n<td>";
}
echo "<img src=\"{$row['Limgt']}\">";
echo "<img src=\"{$row['Limg']}\">";
$i++;
if ($i == $items) {
echo "</td></tr>\n";
$i = 0;
}
}
echo '</table>';
}else {
echo 'no records found';
}
?>
you are making them in two TDs so you have to make them in one TD like that
echo "<td align='center' width='140'>
<img src=\"{$row['Limgt']}\">
<img src=\"{$row['Limg']}\">
</td>";
Here i don't have any coding related problem. I'm trying to fetch the details from multiple database tables. fetch results working fine. But alignment is my problem here.
You can see a image.. In that image voucher status column is working fine. But, another two column ( room status & meal status values ) is always on the last row. How to align those (room and meal status) values properly (like voucher status column)? i hope you can understand my problem...
<?php
echo "<table width=1090 border=1 style=\"border: #ddd;\" align=center cellspacing=4 cellpadding=10>";
echo "<tr class=thvoucher>";
echo "<th width=30>Voucher Number</th>";
echo "<th width=80>Reference Number</th>";
echo "<th width=200>Guest Name</th>";
echo "<th width=150>Voucher Status</th>";
echo "<th width=150>Room Status</th>";
echo "<th width=150>Meal Status</th>";
echo "</tr>";
for($i = $start; $i < $end; $i++)
{
if ($i == $total_results)
{
break;
}
while($row = $stmt->fetch())
{
$voucherid = $row['VoucherID'];
$ref = $row['VoucherReference'];
$gname = $row['GuestName'];
$vstatus = $row['ActiveStatus'];
echo "<tr class=voucherstyle" . $cls . ">";
echo "<td>$voucherid</td>"; **// FIRST TD**
echo "<td>$ref</td>"; **// SECOND TD**
echo "<td>$gname</td>"; **// THIRD TD**
if( $vstatus != 'empty' ) **// FOURTH TD**
{
if( $vstatus == 'Y' )
{
echo "<td class=green >Active</td>";
}
else if( $vstatus == 'N' )
{
echo "<td class=red >Inactive</td>";
}
}
else
{
echo "<td><form method=post onsubmit=\"return mstatusvalidate(this)\"; action='voucherstatus_update.php?id_meal= $voucherid '><select name=mealstatus><option value=empty></option><option value=active>Active</option><option value=inactive>Inactive</option></select><input type=submit class=update_status title=\"Update $gname voucher status\" value=Update></form></td>";
}
}
while($r_row = $r_stmt->fetch())
{
$rstatus = $r_row['ActiveStatus'];
// same like above if else statement **// FIFTH TD**
}
while($m_row = $m_stmt->fetch())
{
$mstatus = $m_row['ActiveStatus'];
// same like above if else statement **// SIXTH TD**
}
echo "</tr>";
}
echo "</table>";
?>
You have 6 th elements in your first row, but I only see 3 td elements and no tr closing tags on subsequent rows. This is likely your issue.
I would suspect that a cursory inspection of your source output would have made this pretty clear.
try this
<?php
echo "<table width=1090 border=1 style=\"border: #ddd;\" align=center cellspacing=4 cellpadding=10>";
echo "<tr class=thvoucher>";
echo "<th width=30>Voucher Number</th>";
echo "<th width=80>Reference Number</th>";
echo "<th width=200>Guest Name</th>";
echo "<th width=150>Voucher Status</th>";
echo "<th width=150>Room Status</th>";
echo "<th width=150>Meal Status</th>";
echo "</tr>";
for($i = $start; $i < $end; $i++)
{
if ($i == $total_results)
{
break;
}
while($row = $stmt->fetch())
{
$voucherid = $row['VoucherID'];
$ref = $row['VoucherReference'];
$gname = $row['GuestName'];
$vstatus = $row['ActiveStatus'];
echo "<tr class=voucherstyle" . $cls . ">";
echo "<td>$voucherid</td>"; **// FIRST TD**
echo "<td>$ref</td>"; **// SECOND TD**
echo "<td>$gname</td>"; **// THIRD TD**
if( $vstatus != 'empty' ) **// FOURTH TD**
{
if( $vstatus == 'Y' )
{
echo "<td class=green >Active</td>";
}
else if( $vstatus == 'N' )
{
echo "<td class=red >Inactive</td>";
}
}
else
{
echo "<td><form method=post onsubmit=\"return mstatusvalidate(this)\"; action='voucherstatus_update.php?id_meal= $voucherid '><select name=mealstatus><option value=empty></option><option value=active>Active</option><option value=inactive>Inactive</option></select><input type=submit class=update_status title=\"Update $gname voucher status\" value=Update></form></td>";
}
while($r_row = $r_stmt->fetch())
{
$rstatus = $r_row['ActiveStatus'];
// same like above if else statement **// FIFTH TD**
}
while($m_row = $m_stmt->fetch())
{
$mstatus = $m_row['ActiveStatus'];
// same like above if else statement **// SIXTH TD**
}
echo "</tr>";
}
}
echo "</table>";
?>
I got it working using this while condition while(($row = $stmt->fetch()) && ($r_row = $r_stmt->fetch()) && ($m_row = $m_stmt->fetch())). Before i've used three separate while loops. Now, i've some other ideas. i tried it, now its working fine and alignment also perfect. Before i didn't post my if else statement code. So, everyone thought i've used just 3 td's. Sorry for confused everyone... I've posted my correct coding below..
<?php
echo "<table width=1090 border=1 style=\"border: #ddd;\" align=center cellspacing=4 cellpadding=10>";
echo "<tr class=thvoucher>";
echo "<th width=30>Voucher Number</th>";
echo "<th width=80>Reference Number</th>";
echo "<th width=200>Guest Name</th>";
echo "<th width=150>Voucher Status</th>";
echo "<th width=150>Room Status</th>";
echo "<th width=150>Meal Status</th>";
echo "</tr>";
for($i = $start; $i < $end; $i++)
{
if ($i == $total_results)
{
break;
}
while(($row = $stmt->fetch()) && ($r_row = $r_stmt->fetch()) && ($m_row = $m_stmt->fetch()))
{
$voucherid = $row['VoucherID'];
$ref = $row['VoucherReference'];
$gname = $row['GuestName'];
$vstatus = $row['ActiveStatus'];
echo "<tr class=voucherstyle" . $cls . ">";
echo "<td>$voucherid</td>"; **1st TD**
echo "<td>$ref</td>"; **2nd TD**
echo "<td>$gname</td>"; **3rd TD**
if( $vstatus != 'empty' ) **4th TD**
{
if( $vstatus == 'Y' )
{
echo "<td class=green >Active</td>";
}
else if( $vstatus == 'N' )
{
echo "<td class=red >Inactive</td>";
}
}
else
{
echo "<td><form method=post onsubmit=\"return mstatusvalidate(this)\"; action='voucherstatus_update.php?id= $voucherid '><select name=mealstatus><option value=empty></option><option value=active>Active</option><option value=inactive>Inactive</option></select><input type=submit class=update_status title=\"Update $gname voucher status\" value=Update></form></td>";
}
$rstatus = $r_row['ActiveStatus'];
// if else statement **5th TD**
$mstatus = $m_row['ActiveStatus'];
// if else statement **6th TD**
echo "</tr>";
}
}
echo "</table>";
?>
I am looking to style my table columns for example make Author columns display for example "the book titles" in bold red while the prices in italic blue etc...
2012-03-11,paul,the book title,386,10,256
my php code
echo "<table cellspacing='0' cellpadding='0'> \n\n
<thead>
<tr>
<th>Date</th>
<th>Author</th>
<th>Title</th>
<th>Prices</th>
<th>Sales</th>
<th>Rank</th>
</tr>
</thead>";
$f = fopen("local/bookdata.csv", "r");
$i = 0;
while (($line = fgetcsv($f)) !== false) {
echo "<tr class='$class".(($i%2)?'odd':'even')."'>";
foreach ($line as $cell) {
echo "<td style='font-weight: bold;'>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
$i++;
}
fclose($f);
echo "\n</table>";
Any help most welcome
php
$classes = array('', '', 'title', 'price' , '', '');
// ...
foreach ($line as $idx=>$cell) {
echo "<td class='{$classes[$idx]}'>". htmlspecialchars($cell) . "</td>";
}
css
td.title { font-weight: bold; color: red; }
td.price { font-style: italic; color: blue; }