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.
Related
I am totally new to PHP and mySQL. I have pulled this data from mySQL database for a wordpress website but I would like to simplify my code so that it's more flexible so that I could customize my tables more easily. Any suggestion on where to start and also is there a standard way to do this ?
<?php
global $wpdb;
$result1 = $wpdb->get_results("SELECT ID, post_title, post_date, guid, post_type FROM wp_posts");
$result2 = $wpdb->get_results("SELECT name, term_id FROM wp_terms");
$result3 = $wpdb->get_results("SELECT object_id, term_taxonomy_id FROM wp_term_relationships");
echo "<style>";
echo "body {font-family: Arial;}";
echo ".table_container { padding: 10px 12px 0px 12px; border: 1px solid #ccc; }";
echo ".table_container th { background-color:white; color:black; font-weight:bold; border-left: 1px solid white;}";
echo "</style></head>";
echo "<body>";
echo "<div class=\"table_container\"><table>";
echo "<tr><th style=\"padding-left:10px;\">Yvirskrift</th><th>Dato</th><th>Talari</th><th>Lurta</th></tr>";
foreach ($result1 as $row1) {
if ($row1->post_type == "podcast") {
echo "<tr>";
echo "<td> $row1->post_title </td>";
$date = date("d-m-Y", strtotime($row1->post_date));
echo "<td> $date </td>";
foreach ($result3 as $row3) {
if ($row1->ID == $row3->object_id) {
foreach ($result2 as $row2) {
if($row3->term_taxonomy_id == $row2->term_id){
echo "<td> $row2->name </td>";
}
}
}
}
$listen = "<a href=$row1->guid>Lurta her</a>";
echo "<td> $listen </td>";
echo "</tr>";
}
}
echo "</table></div>";
?>
For loops needed to populate table. But I keep getting an extra two columns that repeat the data in the first two columns. What am I getting wrong with my for loops? All help is appreciated.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Calculating Interest</title>
<style>
table {
border: 3px solid black;
border-color: black;
text-align: left;
padding: 1px;
}
th {
background-color: #01008A;
color: white;
}
tr:nth-child(odd) {background-color: #AED8E6}
</style>
</head>
<body>
<table>
<tr>
<th>Year</th>
<th>Amount on deposit</th>
</tr>
<?php
for($i = 1; $i <= 10; $i++) {
echo "<tr>";
for ($j = 1; $j <= 2; $j++) {
$p = 1000;
$r = 0.05;
$n = 1;
echo "<td>" . $i . "</td>";
echo "<td>" . ($p = number_format(($p * pow((1 + $r), $i)), 2)) . "</td>";
$n++;
}
echo "</tr>";
}
?>
</table>
</body>
</html>
You have loop with $j that no needed! So use this:
<?php
for($i = 1; $i <= 10; $i++) {
echo "<tr>";
$p = 1000;
$r = 0.05;
$n = 1;
echo "<td>" . $i . "</td>";
echo "<td>" . ($p = number_format(($p * pow((1 + $r), $i)), 2)) . "</td>";
$n++;
echo "</tr>";
}
?>
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>
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);
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; }