View Full Version : Help on converting CSV to HTML for Widget
pityocamptes
05-20-2015, 10:46 PM
I'm looking to get the printout of this file into the main forum side boxes (widget). I have this code and it seems to work when running in php, but completely screws up the widget box when pasting into the forum manager boxes. Can anyone take a look at it and help me out? Thanks.
<?php
echo "<html><body><table>nn";
$f = fopen("https://www.fdic.gov/bank/individual/failed/banklist.csv", "r");
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>n";
}
fclose($f);
echo "n</table></body></html>";
?>
Replicant
05-21-2015, 12:42 AM
I was able to get this to work, however I had to adjust the sideblock width to 1350 px to accomadate the width of the table. Your code was fine, you just need to return the html to the widget instead of echo.
ob_start();
echo '<table>';
$f = fopen("https://www.fdic.gov/bank/individual/failed/banklist.csv", "r");
while (($line = fgetcsv($f)) !== false) {
echo '<tr>';
foreach ($line as $cell) {
echo '<td>' . htmlspecialchars($cell) . '</td>';
}
echo '</tr>';
}
fclose($f);
echo '</table>';
$html = ob_get_clean();
return $html;
pityocamptes
05-21-2015, 01:53 AM
Thanks. I have this code to drop rows that I do not need, [2], [3],[6]. Which I was able to do in this code, however, I would like to offset each row with a different color (horizontally) and the arrays seem to be populating the table vertically (along with color)... thoughts? Thanks.
For some reason the color for the offset "row" colors is not coming out right in html - when it displays. Its been a while since I've coded, please feel free to give advice or clean up suggestions... thanks.
<?php
$count = 1;
$input = 'https://www.fdic.gov/bank/individual/failed/banklist.csv';
echo "<html><body><table border=1 width=250>";
echo "<th bgcolor=#222937><FONT COLOR=WHITE FACE=Geneva, Arial SIZE=3>Bank Name</FONT></th>";
echo "<th bgcolor=#222937><FONT COLOR=WHITE FACE=Geneva, Arial SIZE=3>City</FONT></th>";
echo "<th bgcolor=#222937><FONT COLOR=WHITE FACE=Geneva, Arial SIZE=3>Acq. Institution</FONT></th>";
echo "<th bgcolor=#222937><FONT COLOR=WHITE FACE=Geneva, Arial 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 = '#0066FF';
else
$rowColor = '#222937';
echo "<td bgcolor= .$rowColor.><FONT COLOR=D3AB04 FACE=Geneva, Arial SIZE=2>" . htmlspecialchars($row) . "</FONT></td>";
$count++;
}
}
fclose($ih);
echo "</table></body></html>";
}
?>
Replicant
05-21-2015, 02:59 AM
I added a class to the rows and use CSS to color the backgrounds. You could add a class for the TD tags as well and use CSS to set the font and color too.<?php
$count = 1;
$input = 'https://www.fdic.gov/bank/individual/failed/banklist.csv';
echo "<html><body><table border=1 width=250>";
echo "<style>
tr.row:nth-child(odd) {
background: blue;
}
tr.row:nth-child(even) {
background: black;
}
</style>";
echo "<th bgcolor=#222937><FONT COLOR=WHITE FACE=Geneva, Arial SIZE=3>Bank Name</FONT></th>";
echo "<th bgcolor=#222937><FONT COLOR=WHITE FACE=Geneva, Arial SIZE=3>City</FONT></th>";
echo "<th bgcolor=#222937><FONT COLOR=WHITE FACE=Geneva, Arial SIZE=3>Acq. Institution</FONT></th>";
echo "<th bgcolor=#222937><FONT COLOR=WHITE FACE=Geneva, Arial 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 class=row >";
foreach ($outputData as $row)
{
echo "<td ><FONT COLOR=D3AB04 FACE=Geneva, Arial SIZE=2>" . htmlspecialchars($row) . "</FONT></td>";
$count++;
}
}
fclose($ih);
echo "</table></body></html>";
}
?>
pityocamptes
05-21-2015, 03:01 AM
I was getting help from stack overflow. This looks like it might work, have not tried it yet. The poster indicated that code needs to be changed to get this to work in a widget. What do I need to change in the following code to get it to work, along with where to I place the css file (additional.css)??? Thanks.
<?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>";
}
?>
<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>
--------------- Added 1432184580 at 1432184580 ---------------
I added a class to the rows and use CSS to color the backgrounds. You could add a class for the TD tags as well and use CSS to set the font and color too.<?php
$count = 1;
$input = 'https://www.fdic.gov/bank/individual/failed/banklist.csv';
echo "<html><body><table border=1 width=250>";
echo "<style>
tr.row:nth-child(odd) {
background: blue;
}
tr.row:nth-child(even) {
background: black;
}
</style>";
echo "<th bgcolor=#222937><FONT COLOR=WHITE FACE=Geneva, Arial SIZE=3>Bank Name</FONT></th>";
echo "<th bgcolor=#222937><FONT COLOR=WHITE FACE=Geneva, Arial SIZE=3>City</FONT></th>";
echo "<th bgcolor=#222937><FONT COLOR=WHITE FACE=Geneva, Arial SIZE=3>Acq. Institution</FONT></th>";
echo "<th bgcolor=#222937><FONT COLOR=WHITE FACE=Geneva, Arial 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 class=row >";
foreach ($outputData as $row)
{
echo "<td ><FONT COLOR=D3AB04 FACE=Geneva, Arial SIZE=2>" . htmlspecialchars($row) . "</FONT></td>";
$count++;
}
}
fclose($ih);
echo "</table></body></html>";
}
?>
Ok, thanks. That is what someone on stackoverflow did. Could you please let me know what I need to change in the above code to get it to work in the widget? You mentioned something about the object? Also, how do I get the css to work? Thanks again!
--------------- Added 1432184792 at 1432184792 ---------------
Hey that worked nice!!! How do I get rid of the borders? ALso, what do I need to change in the code to get it to run in a widget? Thanks again!
Replicant
05-21-2015, 03:09 AM
Remove the php, body, and html tags, wrap it up in a object like on post 2 and return the object string. Put the CSS in the additional.css or you can write it straight into style tags if you want.
pityocamptes
05-21-2015, 03:10 AM
Is it possible to add this to the code to get it to scroll? Thanks.
var $table = $('table.scroll'),
$bodyCells = $table.find('tbody tr:first').children(),
colWidth;
// Adjust the width of thead cells when window resizes
$(window).resize(function() {
// Get the tbody columns width array
colWidth = $bodyCells.map(function() {
return $(this).width();
}).get();
// Set the width of thead columns
$table.find('thead tr').children().each(function(i, v) {
$(v).width(colWidth[i]);
});
}).resize(); // Trigger resize handler
Replicant
05-21-2015, 03:17 AM
That code as-is looks like it will affect all tables on the page. Just looking at it without testing it, it looks like it will work. Add some script tags to it and throw it in an ad block and give it a try. You may have to use the TFtable selector to have the JS only affect the table you're working on.
pityocamptes
05-21-2015, 03:43 AM
Thanks again! I am trying to add a header at the top, which would remain static, allowing the table to scroll under it. I can't seem to get it to work, and for some reason the header at the top is not the same width as the table below even though both are set to 250 for width...
<div>
<table style=border: none; border-collapse: collapse; border=0 cellspacing=0 cellpadding=0 align=center width=250>
<tr>
<th bgcolor=#222937><FONT COLOR=WHITE FACE=Geneva, Arial SIZE=3>Bank Name</FONT></th>
<th bgcolor=#222937><FONT COLOR=WHITE FACE=Geneva, Arial SIZE=3>City</FONT></th>
<th bgcolor=#222937><FONT COLOR=WHITE FACE=Geneva, Arial SIZE=3>Acq. Institution</FONT></th>
<th bgcolor=#222937><FONT COLOR=WHITE FACE=Geneva, Arial SIZE=3>Closing Date</FONT></th>
</tr>
</table>
</div>
<?php
$count = 1;
$input = 'https://www.fdic.gov/bank/individual/failed/banklist.csv';
echo "<style>
tr.row:nth-child(odd) {
background: blue;
}
tr.row:nth-child(even) {
background: black;
}
table.tBorder{
width:250; /* or what you need it to be */
height: auto; /* make this the size you want the data to be displayed in */
overflow: auto; /* this adds the scroll bars to the div when any data exceeds its hieght */
}
td.border{
width: 50%;
border-top: 1px solid #000000;
border-bottom: 0px solid #000000; /* move spacing to the cell */
}
</style>";
echo "<html><body><table class=tBorder style=border: none; border-collapse: collapse; border=0 cellspacing=0 cellpadding=0 align=center width=250>";
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 class=row >";
foreach ($outputData as $row)
{
echo "<td class=border align=center ><FONT COLOR=D3AB04 FACE=Geneva, Arial SIZE=2>" . htmlspecialchars($row) . "</FONT></td>";
$count++;
}
}
fclose($ih);
echo "</table></body></html>";
}
?>
Replicant
05-21-2015, 04:37 AM
What you're trying to do with the scrolling feature and fixed header can be done with CSS. There's no reason to bring javascript into the picture for something this simple. Google "CSS table fixed header". You should find tons of examples to go by.
pityocamptes
05-23-2015, 04:51 PM
oK. Here is what I have so far to test on my site. I need the widget content to be 236px by 110px... for some reason it is not working... I am getting massive overflow on the side box. Anyone??? Thanks.
ob_start();
$input = 'https://www.fdic.gov/bank/individual/failed/banklist.csv';
echo "<style>
tr.row:nth-child(odd) {
background: blue;
}
tr.row:nth-child(even) {
background: black;
}
table.tBorder{
width:236; /* or what you need it to be */
height: auto; /* make this the size you want the data to be displayed in */
overflow: auto; /* this adds the scroll bars to the div when any data exceeds its hieght */
}
td.border{
width: 50%;
border-top: 1px solid #000000;
border-bottom: 0px solid #000000; /* move spacing to the cell */
}
</style>";
echo '<table class=tBorder >';
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 class=row >";
foreach ($outputData as $row)
{
echo "<td class=border align=center ><FONT COLOR=D3AB04 FACE=Geneva, Arial SIZE=2>" . htmlspecialchars($row) . "</FONT></td>";
}
}
}
fclose($ih);
echo '</table>';
$html = ob_get_clean();
return $html;
--------------- Added 1432407996 at 1432407996 ---------------
I found this code, but have no idea how to incorporate it into the code above... HELP...
<div id="table_wrapper">
<div id="tbody">
<table>
<tr>
<td class="td1">1</td><td class="td2">2</td><td class="td3">3</td>
</tr>
... more rows here
</table>
</div>
</div>
#table_wrapper{background:tomato;border:1px solid olive;float:left;}
#tbody{height:80px;overflow-y:auto;width:400px;background:yellow;}
table{border-collapse:collapse; width:100%;}
td{padding:1px 5px; /* pixels */
border-right:1px solid red; /* to avoid the hacks for the padding */
border-bottom:1px solid red;}
.td1{width:100px;}
.td2{width:140px;}
.td3{border-right-width:0;} /* optional */
<div id="table_wrapper">
<div id="header">
<div id="head1">Left</div>
<div id="head2">Center</div>
<div id="head3">Right Column</div>
</div>
<div id="tbody">
<table>
<tr>
<td class="td1">1</td><td class="td2">2</td><td class="td3">3</td>
</tr>
... more rows here
</table>
</div>
</div>
#header{width:400px;background:DodgerBlue;border-bottom:1px solid red;}
#header div{padding:1px 5px;float:left;border-right:1px solid orange;}
#header #head1{width:100px;} /* the same as td1 */
#header #head2{width:140px;} /* the same as td2 */
#header #head3{float:none;border-right-width:0}
Note: padding-left/right and width of the header columns.
The width of the #thead and #tbody is same.
--------------- Added 1432412880 at 1432412880 ---------------
Getting a little closer, not sure what is going on here... thanks.
ob_start();
$input = 'https://www.fdic.gov/bank/individual/failed/banklist.csv';
echo "<style>
.parentTbl table {
border-spacing: 0;
border-collapse: collapse;
border: 0;
width: 236px;
}
.childTbl table {
border-spacing: 0;
border-collapse: collapse;
border: 1px solid #d7d7d7;
width: 236px;
}
.childTbl th,
.childTbl td {
border: 1px solid #d7d7d7;
}
.scrollData {
width: 236;
height: 110px;
overflow-x: hidden;
}
tr.row:nth-child(odd) {
background: blue;
}
tr.row:nth-child(even) {
background: black;
}
/* table.tBorder{
width:250; /* or what you need it to be */
height: auto; /* make this the size you want the data to be displayed in */
overflow: auto; /* this adds the scroll bars to the div when any data exceeds its hieght */
}
td.border{
width: 50%;
border-top: 1px solid #000000;
border-bottom: 0px solid #000000; /* move spacing to the cell */
}
*/
</style>";
echo '<div class=parentTbl>';
echo '<table>';
echo '<tr>';
echo '<td>';
echo '<div class=childTbl>';
echo '<table class=childTbl>';
echo '<tr>';
echo "<th>Bank Name</th>";
echo "<th>City</th>";
echo "<th>Acq. Institution</th>";
echo "<th>Closing Date</th>";
echo '</tr>';
echo '</table>';
echo '</div>';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td>';
if (false !== ($ih = fopen($input, 'r')))
{
fgetcsv($ih);
while (false !== ($data = fgetcsv($ih)))
{
$outputData = array($data[0], $data[1], $data[4], $data[5]);
echo '<div class=scrollData childTbl>';
echo '<table>';
echo '<tr class=row >';
foreach ($outputData as $row)
{
echo "<td class=border align=center ><FONT COLOR=D3AB04 FACE=Geneva, Arial SIZE=2>" . htmlspecialchars($row) . "</FONT></td>";
}
echo '</tr>';
echo '</table>';
echo '</div>';
}
}
fclose($ih);
echo '</td>';
echo '</tr>';
echo '</table>';
echo '</div>';
$html = ob_get_clean();
return $html;
--------------- Added 1432435559 at 1432435559 ---------------
Ok, finally got the code to how I want it. How do I get a javascript and the php code to run in the widget box??? Thanks.
<?php
$input = 'https://www.fdic.gov/bank/individual/failed/banklist.csv';
echo "<style>
.parentTbl table {
border-spacing: 0;
border-collapse: collapse;
border: 0;
width: 220px;
table-layout: fixed
}
.childTbl table {
border-spacing: 0;
border-collapse: collapse;
border: none; /* 1px solid #d7d7d7; */
width: 219px;
table-layout: fixed
}
.childTbl th,
.childTbl td {
font-size: 12px;
font-weight:bold;
background: #222937;
color: white;
width: 10px;
border-bottom: 1pt solid red;
cursor: pointer;
}
.scrollData {
width: 236px;
height: 110px;
overflow-x: hidden;
}
td.border{
color: #D3AB04;
font-size: 11px;
width: 10px;
}
tr.row:nth-child(odd) {
background: #222937;
}
tr.row:nth-child(even) {
background: black;
}
</style>";
echo '<div class=parentTbl>';
echo '<table>';
echo '<tr>';
echo '<td>';
echo '<div class=childTbl>';
echo '<table class=childTbl>';
echo '<thead>';
echo '<tr>';
echo '<th onclick="sort_table(people, 0, asc1); asc1 *= -1; asc2 = 1; asc3 = 1; asc4 = 1;">Bank Name</th>';
echo '<th onclick="sort_table(people, 1, asc2); asc2 *= -1; asc3 = 1; asc4 = 1; asc1 = 1;">City</th>';
echo '<th onclick="sort_table(people, 2, asc3); asc3 *= -1; asc4 = 1; asc1 = 1; asc2 = 1;">Acq. Inst.</th>';
echo '<th onclick="sort_table(people, 3, asc4); asc4 *= -1; asc1 = 1; asc2 = 1; asc3 = 1;">Closing Date</th>';
echo '</tr>';
echo '</thead>';
echo '</table>';
echo '</div>';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td>';
echo '<div class=scrollData childTbl>';
echo '<table>';
echo '<tbody id=people>';
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 class=row>';
foreach ($outputData as $row)
{
echo "<td class=border>" . htmlspecialchars($row) . "</td>";
}
echo '</tr>';
}
}
echo '</tbody>';
echo '</table>';
echo '</div>';
fclose($ih);
echo '</td>';
echo '</tr>';
echo '</table>';
echo '</div>';
?>
<script type="text/javascript">
var people, asc1 = 1,
asc2 = 1,
asc3 = 1;
asc4=1;
window.onload = function () {
people = document.getElementById("people");
}
function sort_table(tbody, col, asc){
var rows = tbody.rows, rlen = rows.length, arr = new Array(), i, j, cells, clen;
// fill the array with values from the table
for(i = 0; i < rlen; i++){
cells = rows[i].cells;
clen = cells.length;
arr[i] = new Array();
for(j = 0; j < clen; j++){
arr[i][j] = cells[j].innerHTML;
}
}
// sort the array by the specified column number (col) and order (asc)
arr.sort(function(a, b){
return (a[col] == b[col]) ? 0 : ((a[col] > b[col]) ? asc : -1*asc);
});
for(i = 0; i < rlen; i++){
arr[i] = "<td class=border>"+arr[i].join("</td><td class=border>")+"</td>";
}
tbody.innerHTML = "<tr class=row>"+arr.join("</tr><tr class=row>")+"</tr>";
}
</script>
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.