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; }
Related
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>
Thanks to stackoverflow and its great solution, I have found a way to limit the characters in a table but it doesn't work for me. I tried a lot but with no success.
This is my table
<?php
$result = mysqli_query($conn,"SELECT * FROM library ORDER BY `CreatedTime` DESC");
echo "<table class='table-fill' border='0' cellpadding='0' cellspacing='0'>
<tr>
<th position='fixed' overflow='hidden' width='10%'>Book Name</th>
<th width='5%'></th>
</tr>";
while($row = mysqli_fetch_array($result) ) {
echo "<tr>";
echo "<td colspan='2' style='padding-bottom: 0;'><a href='library.details.php?id=". $row['id']."' target='content' class='positiontitle-link'><font style='text-shadow: none; font-weight: 800;'>" . $row['bookname']. "</td>";
echo "</tr>";
echo "<tr style='border-top-width: 0; padding-top: 0;'>";
echo '<td style="max-height: 10px;">' . $str . '</td>';
echo "<td style=' padding-top: 0; padding-left: 15px; width: 40%;'> <font color='gray'>Author :</font> " .($row['authorname'] ). "</td>";
echo "<td width='5%' style=' padding-top: 0;'> <font color='gray'>Year Published </font>" . $row['yearpublished'] . "</td>";
echo "</tr>";
if (strlen($row['bookname']) > 1) $str = substr($row['bookname'], 0, 1) . "...";
}
echo"</table>";
?>
This is how it looks like
Any help will be appreciated.
I'm doing this out of my head so forgive me any format issues and such...
Move the string length check to just under the while.
Overwrite $row['bookname'] instead of creating $str.
Remove the line with:
echo '<td style="max-height: 10px;">' . $str . '</td>';
Result:
while($row = mysqli_fetch_array($result) ) {
if (strlen($row['bookname']) > 9) $row['bookname'] = substr($row['bookname'], 0, 9) . "...";
echo "<tr>";
echo "<td colspan='2' style='padding-bottom: 0;'><a href='library.details.php?id=". $row['id']."' target='content' class='positiontitle-link'><font style='text-shadow: none; font-weight: 800;'>" . $row['bookname']. "</td>";
echo "</tr>";
echo "<tr style='border-top-width: 0; padding-top: 0;'>";
echo "<td style=' padding-top: 0; padding-left: 15px; width: 40%;'> <font color='gray'>Author :</font> " .($row['authorname'] ). "</td>";
echo "<td width='5%' style=' padding-top: 0;'> <font color='gray'>Year Published </font>" . $row['yearpublished'] . "</td>";
echo "</tr>";
}
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 am trying to display the data output in to specific div within a table.
I am able to get the table setup and display the first row of the data within the table, but the rest of the output does not make it inside the table.
Anyone please can point out what am I doing wrong?
Thank you!
<?php include('header.php'); ?>
<?php
include_once 'scripts/db.php';
$result = mysqli_query($con,"SELECT * FROM employee");
?>
<div id="userlist">
<center><h3>User Listing</h3></center>
<?php
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>location</th>
</tr>";
?>
<?php
while($row = mysqli_fetch_array($result))
{?>
<div class="userlistoutput">
<?php
echo "<td width=120px>" . $row['firstname'] . "</td>";
echo "<td width=120px>" . $row['lastname'] . "</td>";
echo "<td width=120px>" . $row['location'] . "</td>";
echo "</tr>";
?>
</div>
<? echo "</table>" ?>
<?php
}?>
</div>
<?php
mysqli_close ($con);
?>
And this is the CSS for userlist:
#userlist {
list-style:none;
width: auto;
margin:30px auto 1px auto;
height:100%;
padding:0px 20px 0px 20px;
/* Rounded Corners */
border-radius: 10px;
/* Background color and gradients */
background:#F4F4F4;
background: -moz-linear-gradient(top, #EEEEEE, #BBBBBB); */
background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#EEEEEE), to(#BBBBBB));
/* Borders */
border: 1px solid #002232;
}
That is happening because you have put the </table> inside the while statement. Try changing it to:
<?php include('header.php');
include_once 'scripts/db.php';
$result = mysqli_query($con,"SELECT * FROM employee");
?>
<div id="userlist">
<center><h3>User Listing</h3></center>
<?php
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>location</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr class='userlistoutput'>;
echo "<td width='120px'>" . $row['firstname'] . "</td>";
echo "<td width='120px'>" . $row['lastname'] . "</td>";
echo "<td width='120px'>" . $row['location'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
mysqli_close ($con);
?>
I need to freeze the first row for the below table, here's my current code below
Please let me know if you need anymore information
Here's an image of the table:
<head>
<style>
table,td,th
{border-collapse:collapse;}
table.myTable td, table.myTable th { border:1px solid black;padding:5px;
font-family:Verdana, Arial, Helvetica, sans-serif;
color:#2C3539;
font-size:0.80em}
table
{width:100%;}
th{background-color:#B6B6B4;
height:10px;}
</style>
<table class="myTable">
<?php
//MySQL Database Connect
include 'connect.php';
echo "
<tr>
<th>Name</th>
<th>Location</th>
<th>Email</th>
<th>Mobile</th>
<th>IMEI</th>
<th>Phone</th>
<th>Message</th>
</tr></Freezing>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Location'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "<td>" . $row['Mobile'] . "</td>";
echo "<td>" . $row['IMEI'] . "</td>";
echo "<td>" . $row['Phone'] . "</td>";
echo "<td>" . $row['Message'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
What you could do instead is use the <thead> to segment your <th> tags. Then you could use either absolute of fixed positioning to have that section float above the others. Here is an example:
HTML
<thead>
<tr>
<th>Name</th>
<th>Location</th>
<th>Email</th>
<th>Mobile</th>
<th>IMEI</th>
<th>Phone</th>
<th>Message</th>
</tr>
</thead>
<tbody>
...
</tbody>
CSS
thead {
display: block;
position: absolute;
top: 0;
left: 0;
width: 100%
z-index: 100;
}
You may also need to add some padding to the top of the <tbody> tag so that the frozen row does not sit over top of any data. Additionally, absolute positioning will be relative to the nearest positioned ancestor, so you may need to add a position to the table as well.
table {
position: relative;
}
tbody {
padding-top: 1em;
}
try this it's using jquery though Freeze Header