Parsing a complicated HTML table with PHP - php

I successfully parsed a dynamic table with the following PHP code:
$docH = new DOMDocument();
$docH->loadHTMLFile($url);
//get everything inside the body element:
$bodyH = $docH->getElementsByTagName('body')->item(0);
foreach ($bodyH->childNodes as $childNode) {
echo $docH->saveHTML($childNode);
}
Parsed HTML Table:
<table>
<tr>
<td>5CG</td>
<td>aass</td>
<td>sxs</td>
<td>sx</td>
<td>EK</td>
<td></td>
<td>72</td>
</tr>
<td></td>
<td>samplxs</td>
<td>xs</td>
<td></td>
<td>xss</td>
<td>fkxsx aus</td>
<td>s</td>
</tr>
<td></td>
<td>5AH.</td>
<td>ds</td>
<td>d</td>
<td>sdf</td>
<td>sdfsdf aus</td>
<td></td>
</tr>
<tr>
<td>6CG</td>
<td>3.</td>
<td>sfd</td>
<td></td>
<td>scs</td>
<td>das aus</td>
<td>a</td>
</tr>
<tr>
<td>7DG</td>
<td>6.</td>
<td>s</td>
<td>s</td>
<td>sD</td>
<td>sdsa.</td>
<td></td>
</tr>
<td></td>
<td>samplxs</td>
<td>xs</td>
<td></td>
<td>xss</td>
<td>fkxsx aus</td>
<td>s</td>
</tr>
<tr>
<td>7DG, 7CG, 7CR</td>
<td>6.</td>
<td>NsdR</td>
<td>s</td>
<td>SP</td>
<td>fasdlt aus</td>
<td>s</td>
</tr>
<td></td>
<td>samplxs</td>
<td>xs</td>
<td></td>
<td>xss</td>
<td>fkxsx aus</td>
<td>s</td>
</tr>
<tr>
<td>9BR</td>
<td>6.</td>
<td>FEI</td>
<td>sa</td>
<td>DE</td>
<td>fasdad aus</td>
<td></td>
</tr>
<tr>
<td>9AR, 9BR, 9CR</td>
<td>62.</td>
<td>BEH</td>
<td></td>
<td>sd</td>
<td>fasda aus</td>
<td></td>
</tr>
<tr>
<td></td>
<td>6.</td>
<td>MLR</td>
<td></td>
<td>FdR</td>
<td>fsdfaus</td>
<td></td>
</tr>
<tr>
<td>E10C</td>
<td>6.</td>
<td>sdf</td>
<td>d</td>
<td>d</td>
<td>fsdfs aus</td>
<td></td>
</tr>
<tr>
</table>
But my goal is to just show the content of the table the user wants by asking for just the <tr> elements in which the first <td> of the first <tr> includes some text until there is another <tr> which first <td> has a different content.
For example: If the user types "9BR" into an input field, I just want him to see:
9BR
6.
FEI
sa
DE
fasdad aus
9AR, 9BR, 9CR
62.
BEH
sd
fasda aus
6.
MLR
FdR
fsdfaus
If he types in 5CG:
<tr>
<td>5CG</td>
<td>aass</td>
<td>sxs</td>
<td>sx</td>
<td>EK</td>
<td></td>
<td>72</td>
</tr>
<td></td>
<td>samplxs</td>
<td>xs</td>
<td></td>
<td>xss</td>
<td>fkxsx aus</td>
<td>s</td>
</tr>
Or if 6CG just:
<tr>
<td>6CG </td>
<td>3. </td>
<td>sfd </td>
<td> </td>
<td>scs </td>
<td>das aus</td>
<td>a </td>
</tr>

Using XPath, something like this should do the trick
http://de3.php.net/manual/en/class.domxpath.php
$xpath = new DomXpath($docH);
$trs = $xpath->query('//tr[td[1][contains(text(), "BR9")]]');
find all tr which first td contains text "anything"
as for the following ´tr´s with empty first td
this might not be the most elegant form to query this, but would work:
$query = '
//tr[td[1][contains(text(), "anything")]]
|
//tr[td[1][contains(text(), "anything")]]
/following-sibling::tr[td[1][not(text())] and preceding-sibling::tr[1][td[1][not(text()) or contains(text(), "anything")]]]
';
find all tr which first td contains text "anything"
also find all tr which first td is empty and whose preceding siblings (trs) first td
is also empty or contains text "anything"
example: http://3v4l.org/q6eDu

Related

Codeigniter/CSS PDF Printout Align PDF Position

Problem: Currently I have problem aligning the PDF that I am creating as it is different with normal CSS that I know.
Problem:
What I need it to be solve:
Make it align, for the image I provided, I wanted "No.SAG" below "Old"
This is the code:
<table class="unstyledTable">
<thead>
<tr>
<th>Name:</th>
<th>Test Data</th>
<th></th>
<th>Number card IC</th>
<th></th>
<th></th>
</tr>
</thead>
<tfoot>
<tr>
<td>Name Agent::</td>
<td>TomHansom </td>
<td></td>
<td>New:</td>
<td>foot5</td>
<td></td>
</tr>
</tfoot>
<tbody>
<tr>
<td>Address:</td>
<td>werwrwrwwwwwwwwwwwwww wwwwwwwwwwwwwwwwwwww</td>
<td></td>
<td>Old:</td>
<td>cell5_1</td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td>No.SAG:</td>
<td>cell5_2</td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td>Number.Siri:</td>
<td>cell5_3</td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
Might be easier to put both in columns as tables in a parent table. Check out my code below to see if this is what you were after.
<table class="unstyledTable">
<tbody>
<tr>
<td valign="top"><table>
<tbody>
<tr class="unstyledTable">
<th>Name:</th>
<th>Test Data</th>
</tr>
<tr class="unstyledTable">
<td>Address:</td>
<td>werwrwrwwwwwwwwwwwwww wwwwwwwwwwwwwwwwwwww</td>
</tr>
<tr class="unstyledTable">
<td></td>
<td></td>
</tr>
<tr class="unstyledTable">
<td></td>
<td></td>
</tr>
<tr class="unstyledTable">
<td></td>
<td></td>
</tr>
<tr class="unstyledTable">
<td>Name Agent::</td>
<td>TomHansom </td>
</tr>
</tbody>
</table></td>
<td valign="top"><table>
<tbody>
<tr class="unstyledTable">
<th>Number card IC</th>
<th></th>
</tr>
<tr class="unstyledTable">
<td>Old:</td>
<td>cell5_1</td>
</tr>
<tr class="unstyledTable">
<td>No.SAG:</td>
<td>cell5_2</td>
</tr>
<tr class="unstyledTable">
<td>Number.Siri:</td>
<td>cell5_3</td>
</tr>
<tr class="unstyledTable">
<td></td>
<td></td>
</tr>
<tr class="unstyledTable">
<td>New:</td>
<td>foot5</td>
</tr>
</tbody>
</table></td>
</tr>
</tbody>
</table>

How to add data into HTML table irrespective of number of elements in array

I am trying to display the array data into HTML table but table design or structure should be untouched irrespective of number of elements in array.
I tried some of logic like looping, but table structure changes.I don't want that.
Here is my code:
<?php
$goods=["ABC","XYZ","PQR","DEF"];
$qty=[12345,25120,14521,12541];
$rate=[12.00,13.02,15.00,14.00];
?>
<table>
<tr>
<th>Sr No.</th>
<th>Goods Description</th>
<th>Total Quantity(pcs)</th>
<th>Rate per unit(Rs.)</th>
<th colspan="3">Amount</th>
</tr>
<?php
for($i=0;$i<count($goods);$i++) {
?>
<tr>
<td><?php echo $goods[$i];?></td>
<td><?php echo $qty[$i];?></td>
<td><?php echo $rate[$i];?></td>
</tr>
<?php } ?>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td>Total Value</td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td>Frieght</td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td>Total Taxable Value</td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td>Tax</td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td>tax</td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td>tax</td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td>Total Tax Amount</td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td>Grand Total Tax Amount</td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td>ABC</td>
<td></td>
<td></td>
</tr>
</table>
Expected Output:
Expected_output
I think this is what you need. You had not included the words in the loop.
It was insanely hard to write this code on the phone so there may be something left out.
$goods=["ABC","XYZ","PQR","DEF"];
$qty=[12345,25120,14521,12541];
$rate=[12.00,13.02,15.00,14.00];
$words=["Total Value", "Frieght", "Total Taxable Value", "Tax", "Tax", "Tax", "Total Tax Amount"];
?>
<table>
<tr>
<th>Sr No.</th>
<th>Goods Description</th>
<th>Total Quantity(pcs)</th>
<th>Rate per unit(Rs.)</th>
<th colspan="3">Amount</th>
</tr>
<?php
for ($i=0;$i<count($words);$i++) {
?>
<tr>
<td><?php echo $i+1 . "."; ?></td>
<td><?php if(isset($goods[$i])) echo $goods[$i];?></td>
<td><?php if(isset($qty[$i])) echo $qty[$i];?></td>
<td><?php if(isset($rate[$i])) echo $rate[$i];?></td>
<td><?php echo $words[$i];?></td>
</tr>
<?php } ?>
</table>
https://3v4l.org/f1BnW.
Edit forgot number on first column.

Tournament table [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to create a tournament table using PHP.
This is the HTML code sample.
<table>
<thead>
<tr>
<th>1</th>
<th>2</th>
<th>final</th>
<th>winner</th>
</tr>
</thead>
<tbody>
<tr>
<td class="team">team1</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td class="team">team1</td>
<td></td>
<td></td>
</tr>
<tr>
<td class="team">team2</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td class="team">team4</td>
<td></td>
</tr>
<tr>
<td class="team">team3</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td class="team">team4</td>
<td></td>
<td></td>
</tr>
<tr>
<td class="team">team4</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td class="team">team4 vinner</td>
</tr>
<tr>
<td class="team">team5</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td class="team">team6</td>
<td></td>
<td></td>
</tr>
<tr>
<td class="team">team6</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td class="team">team8</td>
<td></td>
</tr>
<tr>
<td class="team">team7</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td class="team">team8</td>
<td></td>
<td></td>
</tr>
<tr>
<td class="team">team8</td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
I tried creating that table using for loops, but that didn't work very well.
Maybe you can give some hints? I am out of ideas.
The elimination system you are working with is called "single elimination" (sometimes "head to head") and the output you wish to achieve is often called "brackets".
Have a look at:
Generate a single elimination tournament
and: PHP Single Elimination Tournament Table
This one can be helpful too: Tournament bracket placement algorithm

HTML to PDF conversion rowspan not working

I am using third party code for HTML To PDF conversion is working good.But this cannot accept rowspan why?? Here is my code.
require('WriteHTML.php');
$pdf=new PDF_HTML();
$pdf->AliasNbPages();
$pdf->SetAutoPageBreak(true, 15);
$pdf->AddPage();
$pdf->SetFont('Arial','B',14);
$pdf->WriteHTML('<para><h1>Title</h1>');
$pdf->SetFont('Arial','B',7);
$htmlTable="<TABLE border='1'>
<TR height='40'>
<TD>S.NO</TD>
<TD>Type Of Beneficiary</TD>
<TD>Expected</TD>
<TD>No.Of Beneficiary</TD>
<TD>No.Of Packs Issued</TD>
<TD>Ave.monthly Usage</TD>
<TD>Balance Stock Available</TD>
</TR>
<TR>
<TD>1</TD>
<TD>Adolescents Girls</TD>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD>
<TD ></TD>
</TR>
<TR>
<TD rowspan='4'>2</TD>
<TD>Normal</TD>
<TD>ALLOTMENT NO</TD>
<TD>QUANTITY</TD>
<TD>QUANTITY</TD>
<TD>QUANTITY</TD>
<TD rowspan='4'></TD>
</TR>
<TR>
<TD>LSCS</TD>
<TD>ALLOTMENT NO</TD>
<TD>QUANTITY</TD>
<TD>QUANTITY</TD>
<TD>QUANTITY</TD>
</TR>
<TR>
<TD>Puperial Sterilization</TD>
<TD>ALLOTMENT NO</TD>
<TD>QUANTITY</TD>
<TD>QUANTITY</TD>
<TD>QUANTITY</TD>
</TR>
<TR>
<TD>Total</TD>
<TD>ALLOTMENT NO</TD>
<TD>QUANTITY</TD>
<TD>QUANTITY</TD>
<TD>QUANTITY</TD>
</TR>
<TR>
<TD>3</TD>
<TD>Women Prison Inmates</TD>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD>
</TR>
<TR>
<TD>4</TD>
<TD>Women IMH Inmates</TD>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD>
</TR>
<TR>
<TD>5</TD>
<TD>Total</TD>
<TD> </TD>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD>
</TR>
</TABLE>";
$pdf->WriteHTML2("$htmlTable");
$pdf->Output();
It seems FPDF doesn't support rowspan:
FPDF does not recognize rowspan or colspan. Here is a workaround that
you can try, using empty cells and the border attribute for Cell.
by Sean (emphasis mine) - here is the full answer
Rowspan and Colspan supporting the TCPDF Library.And also easy configration this one. Library path http://www.tcpdf.org/ and coding path http://www.tcpdf.org/examples/example_048.phps. just change the path only. require_once('tcpdf.php');

mdpf can't create pdf with <b> or <s> tags

I'm trying to create a pdf out of an html table which is created through php.
I use php version 5.4.7 and mdpf version 5.7.3.
This code does not work:
<?php
include('../../mpdf/mpdf.php');
$mpdf=new mPDF('utf-8', 'A4-L');
$test2 = "<table class='timon' border='1'>
<thead>
<tr>
<th></th>
<th>Montag</th>
<th>Dienstag</th>
<th>Mittwoch</th>
<th>Donnerstag</th>
<th>Freitag</th>
<th>Avg. Tag</th>
</tr>
</thead>
<tbody>
<tr>
<td>Morgen (0.3) </td>
<td><s>test user 0.36</s><br /></td>
<td></td>
<td><s>test user 0.26</s><br /></td>
<td></td>
<td></td>
<td> </td>
</tr>
<tr>
<td>Mittag (0.5) </td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td> </td>
</tr>
<tr>
<td>Nachmittag (0.5) </td>
<td><s>test user 0.16</s><br /></td>
<td></td>
<td></td>
<td></td>
<td><s>test user 0.46</s><br /></td>
<td> </td>
</tr>
<tr>
<td>Ganzer Tag (1) </td>
<td></td>
<td></td>
<td><s>test user 1.2</s><br /></td>
<td></td>
<td></td>
<td> </td>
</tr>
<tr>
<td>Total Auslastung</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>Total Gruppe</td>
<td>7.5</td>
<td>7.5</td>
<td>7.5</td>
<td>7.5</td>
<td>7.5</td>
<td>7.5</td>
</tr><tr>
<td>Freie Plätze</td>
<td style='background-color:#c6efce;'>7.5</td>
<td style='background-color:#c6efce;'>7.5</td>
<td style='background-color:#c6efce;'>7.5</td>
<td style='background-color:#c6efce;'>7.5</td>
<td style='background-color:#c6efce;'>7.5</td>
<td style='background-color:#c6efce;'>7.5</td>
</tr>
</tbody>
</table>";
$mpdf->WriteHTML($test2,2);
$filename = "test.pdf";
$mpdf->Output($filename,'I');
exit;
?>
If I remove just one of the < s> tags, the PDF will be created fine. Eg.:
<td><s>test user 0.36</s><br /></td>
<td>test user 0.36<br /></td>
it also have problems with the < b> and < u> tag.
Any ideas?
You must close the <s> tag. mPDF is very strict with opening and closing tags. And not all of your opening tags are closed.
f.i.
<td><s>test user 0.36<s><br /></td>
Is not closed properly.
I found the mistake. I had an additional php include and this php had an body and html tag in it. I just removed it and now it works fine. BR

Categories