Weird issue with table on domdocument - php

I have a very weird issue with table cell.
My previous post
How to append element to another element using php
My code is like the following
$dom = new DomDocument();
$dom->loadHTML($html]);
$tbodies = $dom->getElementsByTagName('tbody');
foreach ($tbodies as $tbody) {
$table = $dom->createElement('table');
$table->setAttribute('width',500);
$table->setAttribute('style','border:2px solid #8C8C8C;text-align:center;table-layout:fixed; border-collapse:separate;');
$tbody->parentNode->replaceChild($table, $tbody);
$table->appendChild($tbody);
}
$returnText .=$dom->saveHTML();
From my previous pose, I got my answer but it seems like the new html table doesn't have border in the table cell.
So my table like
___________
|cell cell |
|cell cell |
|___________|
but I want every cell has border.
I am sure my original html table cell has no inline style addressing cell border too.
Can anyone helps?
Thanks!

There is no border on the cells because in css the table tag is styled separately from the cells td or th tag. See here: http://www.quackit.com/html/codes/tables/html_table_border.cfm
edit: Better link.

I have never used domdocument but i see this line :
$table->setAttribute('style','border:2px solid #8C8C8C;text-align:center;table-layout:fixed; border-collapse:separate;');
Instead of adding style attribute that will stylish your table and not td cells , try to add a class for your table that will stylish both table and cells :
$table->setAttribute('class','test');
and add a css file or a style that containts your class :
<style type='text/css'>
.test{
border:2px solid #8C8C8C;
text-align:center;
table-layout:fixed;
border-collapse:separate;
}
.test th,.test td{border:2px solid #8C8C8C;}
</style>

Related

How do I conditionally set height of <td> accounting for amount of data in it when Ajax builds the table?

I build a table using Ajax/PHP/MySql. In the table one column is for comments, sometimes the comments are quite long but I don't want to show all of them because it distorts the table look so I set it up like this;
echo ("<td id=\"comments:$row[recordID]\">
<div class='scrollable'> $row[comments] </div> </td>");
And the CSS is;
div.scrollable {
width: 100%;
height: 75px;
margin: 0;
overflow: auto;
}
The problem is the height of the cell is locked at 75px because of the div on the CSS, but if the cell is empty I want it set to its default, not that imposed by the div.scrollable CSS.
I tried using 'inherit' for the height, but that of course cancels out the overflow and defaults the height to show everything in the cell.
What do I have to do to get an empty cell to have the same height as the other columns in the table, but not make all the other cells in the row its height?
Simply set the class if the comment is empty.
$class = empty($row[comments]) ? 'nonscrollable' : 'scrollable' ;
echo ("<td id=\"comments:$row[recordID]\">
<div class='$class'>$row[comments]</div> </td>");

Table td width.. in diffrent tr

I'm trying to set td width to diffrent px on each row.
So i generate a table tag with HTML.
Then i loop out a table with PHP.
If i put all td´s in one tr it works, but i got all td on same row.
I'd like to have just one td on each tr.
What i'm doing is building rectangles with diffrent length in a list.
Later on, they will be draggable.
Code
echo "<tr><td class='tdElements' id='".$row['id']."' style='width:".$row['element_langd']."px' draggable='true' ondragstart='drag(event)'>".$row['element_langd']."</td></tr>";
Try:
echo
"<div id='".$row['id']."' style='width:".$row['element_langd']."px'
draggable='true' ondragstart='drag(event)'>"
.$row['element_langd'].
"</div>";
You can explicitly define the width of an individual <div> using css and, so each rectangle should be sized according to the $row['element_langd'] value.

Alternating Color HTML Table PHP

I am trying to get my table to alternate colors but I am having some difficulty.
if ($i % 2 == 0)
$color = "grey";
else
$color = "white"; $i++;
$table .= "<tr style=backround-color=$color>";
This does not work. I have tried this as well but it did not work either.
$table .= "<tr:nth-child(even) {background: #CCC}; tr:nth-child(odd) {background: #FFF}; >";
You misspelled background and you don't use = in CSS, you use :. I also added quotes around your attribute values as it is best practice:
$table .= "<tr style='background-color:$color'>";
The last line in your question isn't even close to valid HTML or CSS. Looks kinda neat though.
I found an interesting link here that does what you are looking for. I put the code from the link into a jsfiddle and here is the css styles that I got from the link:
.TFtableCol{
width:100%;
border-collapse:collapse;
}
.TFtableCol td{
padding:7px; border:#4e95f4 1px solid;
}
/* improve visual readability for IE8 and below */
.TFtableCol tr{
background: #b8d1f3;
}
/* Define the background color for all the ODD table columns */
.TFtableCol tr td:nth-child(odd){
background: #b8d1f3;
}
/* Define the background color for all the EVEN table columns */
.TFtableCol tr td:nth-child(even){
background: #dae5f4;
}

Render excel worksheet in html, using php, including rich text and hyperlinks

I'm looking for a way to read an excel file (xls format, but could probably use xlsx if necessary), and convert that file into an html table, preserving the rich text in the cells, cell borders, as well as the hyperlinks.
I've looked at https://code.google.com/p/php-excel-reader2/, which looks pretty good except some borders are missed and hyperlink targets have missing letter at end of filename (weird). I could probably debug these if I have to, but another problem with this is that this code base isn't supported any more. See http://www.steeplechasers.org/racecalendar-php-excel.php for example (spreadsheet is at http://www.steeplechasers.org/racecalendar.xls ).
<?php
error_reporting(E_ALL ^ E_NOTICE);
require_once 'excel_reader2.php';
$data = new Spreadsheet_Excel_Reader("racecalendar.xls");
?>
<html>
<head>
<style>
table.excel {
border-style:ridge;
border-width:1;
border-collapse:collapse;
font-family:sans-serif;
font-size:12px;
}
table.excel thead th, table.excel tbody th {
background:#CCCCCC;
border-style:ridge;
border-width:1;
text-align: center;
vertical-align:bottom;
}
table.excel tbody th {
text-align:center;
width:20px;
}
table.excel tbody td {
vertical-align:bottom;
}
table.excel tbody td {
padding: 0 3px;
border: 1px solid #EEEEEE;
}
</style>
</head>
<body>
<?php echo $data->dump(true,true); ?>
</body>
</html>
I also looked at https://github.com/PHPOffice/PHPExcel, which seems like it should be able to do this somehow, but using simple generateSheetData, borders rich text and hyperlinks don't seem to be preserved, and I'm having trouble groking the docs to see how to copy these attributes into html. See http://www.steeplechasers.org/racecalendar-PHPExcel.php for example (same input file).
<?php
error_reporting(E_ALL ^ E_NOTICE);
require_once 'PHPExcel/Classes/PHPExcel.php';
$filename = 'racecalendar.xls';
//$filetype = PHPExcel_IOFactory::identify($filename);
$reader = PHPExcel_IOFactory::createReaderForFile($filename);
$reader->setReadDataOnly(true);
$excel = $reader->load($filename);
$writer = PHPExcel_IOFactory::createWriter($excel, "HTML");
?>
<html>
<head>
</head>
<style>
<?php
echo $writer->generateStyles(false);
?>
</style>
<body>
<?php
echo $writer->generateSheetData();
?>
</body>
</html>
Convert HTML to Excel Rich Text and vice versa seems to indicate this isn't a feature of PHPExcel yet.
PHPExcel - Preserve rich text format on import didn't answer the question at all, as far as I could tell.
Is there another way to do this directly, or some way to do this with PHPExcel?
NOTE: I would actually like to iterate through the cells because I want to add a column to the html table as I go -- I used code to dump the whole spreadsheet as simple examples of the package behaviors, and this is as far as I have gotten in my investigation.
You're setting
$reader->setReadDataOnly(true);
when you read the spreadsheet file
This tells PHPExcel to ignore any formatting information in the spreadsheet file, and only to read the raw data in the cells. It means that all formatting (borders, number formatting styles, font colours, etc) will be ignored
The solution is not to set $reader->setReadDataOnly(true);
EDIT
As a quick hack for the URLs, you can modify /Classes/PHPExcel/Reader/Excel5.php
Lines 4564 and 4565
$url = self::_encodeUTF16(substr($recordData, $offset, $us - 2), false);
$url .= $hasText ? '#' : '';
change to
$url = self::_encodeUTF16(substr($recordData, $offset, $us - 2), false);
$nullOffset = strpos($url, 0x00);
if ($nullOffset)
$url = substr($url,0,$nullOffset);
$url .= $hasText ? '#' : '';
It fixes the URLs (note that php-excel-reader2 loses the last character of the URL) by testing for a null string terminator; and doesn't appear to have any adverse effects though it isn't adjusting the offset to allow for the adjustment. I need to check the BIFF specs and run a few more tests against both BIFF 5 and BIFF8 files with different link types before I'll commit anything back to the github repo)
Here is a link that might help: link.
Also, you can get the XML version of Excel (Excel is stored as XML I believe) and then just analyze the XML with PHP and put in a table on your website. Hope this helps.

Evenly spacing multiple line item text without using a table

I'm trying to create an unordered list of <a>text1 text2 text3</a> elements, with a while loop. This list is then styled using #sidebar li a in my CSS.
My problem is that the text1, text2, text3 that is passed into each <a> element in my while loop can take on different lengths and I would like for them to be spaced equally like a table. However, I CANNOT use a table, because to format like a table, requires me to do this....
<li><a><tr><td>text1</td> <td>text2</td> <td>text3</td></tr></a></li>...
and because of that, my CSS "background" image will repeat for EACH <td>, when I only want the background image ONCE for each <tr>...(using different style tags than shown below)
Is there a way to change my while loop to space my text1,text2,text3 evenly like a table (without using a table) and maintain my CSS background image ONCE per each <li>? Any help would be INCREDIBLY appreciated!
My PHP file
while($row = mysql_fetch_assoc($result))
{
echo "<ul id=\"sidebar\">";
echo "<li>" . $row['column1'] . " ". $row['column2']. " ". $row['column 3']."</li></ul>";
}
My CSS file
#sidebar li a {
background: url(../images/sidebar.gif) no-repeat 0px 0px;
}
while($row = mysql_fetch_assoc($result))
{
echo "<ul id=\"sidebar\">;
echo "<li><span class="psuedo-col">" . $row['column1'] . "</span> <span class="psuedo-col">". $row['column2']. "</span> <span class="psuedo-col">". $row['column 3']."</span></li></ul>";
}
Add <span>s around the content from the $row['...'], in order that the css has something to serve as a hook, and set an explicit width on those spans. Bearing in mind that if the content of the spans is too large it will either require an overflow rule (hidden, visible or auto) or your content will start to look odd.
As an example
span.psuedo-col {
display: inline-block;
width: 10em;
overflow: hidden;
}
Or you could use
`display: block;
/* other stuff */
float: left; /* or right, depending on your alignment requirements */
The floats, obviously, will take the contents of the spans out of the flow of the document, perhaps causing the <li> itself to collapse, sine it'll have no content.
Asfar as I understand your question, you want your LI elements to have a fixed width like TD in a table:
#sidebar li {
float:left;
width:33%; /* three columns with equal width */
}

Categories