-1

A website that I'm working on already has around 140 HTML files and each file contains a different HTML table. Each table has anywhere between 10 and approx 400 rows with 2 columns. This is old code which doesn't meet standards and at the moment I'm trying to make do with that old code.

Here's an example:

<TABLE BORDER=0>
  <TR><TD><FONT SIZE=1>Row 1 Col 1</TD><TD WIDTH+20><FONT SIZE=1>Row 1 Col 2</TD><TR>
<TR><TD><FONT SIZE=1>Row 2 Col 1</TD><TD WIDTH+20><FONT SIZE=1>Row 2 Col 2</TD><TR>
<TR><TD><FONT SIZE=1>Row 3 Col 1</TD><TD WIDTH+20><FONT SIZE=1>Row 3 Col 2</TD><TR>
...
</TABLE>

I'm trying to find a way in PHP to count how many rows there are in the table, and then split the rows into 4 divs. So if we had a table with 100 rows. The first 25 rows would go into this div:

<div class="span3"><table>{first 25 rows go here}</table></div>

and so on...

<div class="span3"><table>{next 25 rows go here}</table></div>
<div class="span3"><table>{next 25 rows go here}</table></div>
<div class="span3"><table>{next 25 rows go here}</table></div>

until finally we get something resembling:

<div class="row-fluid">
  <div class="span3"><table>{first 25% rows go here}</table></div>
  <div class="span3"><table>{next 25% rows go here}</table></div>
  <div class="span3"><table>{next 25% rows go here}</table></div>
  <div class="span3"><table>{next 25% rows go here}</table></div>
</div>

All of this needs to be done without actually editing the code in the existing tables. Does anyone know how I'd do this with PHP?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Metzed
  • 470
  • 1
  • 8
  • 27
  • what did you did so far other than asking this question? Refer these : http://stackoverflow.com/questions/3553897/what-is-the-fastest-way-to-convert-html-table-to-php-array http://stackoverflow.com/questions/12445962/html-table-to-array-php http://stackoverflow.com/questions/3553897/what-is-the-fastest-way-to-convert-html-table-to-php-array – limovala Jul 08 '13 at 11:45
  • @limovala - What did this guy try: http://stackoverflow.com/questions/840948/stripping-everything-but-alphanumeric-chars-from-a-string-in-php? Look at those votes! – Expedito Jul 08 '13 at 11:53
  • 1
    You should parse it with some PHP tool as already pointed out. I would just gather the stuff you need via jQuery [much easier than dealing with parsers], serialize it as JSON, then do whatever you want with it. – moonwave99 Jul 08 '13 at 11:56
  • @PédeLeão, I found no research effort from Mark21,I already pointed 3 out of several links which he could use to solve his issue. – limovala Jul 09 '13 at 04:17

3 Answers3

1

I would use the DomDocument model, something like this:

$dom = new domDocument;
@$dom->loadHTML($html);
$rows = $dom->getElementsByTagName('tr');
Expedito
  • 7,771
  • 5
  • 30
  • 43
1

Thank you for the tips everyone. This is how I answered the question, going down the DomDocument route.

 <?php

  $dom = new DOMDocument();

  //load the html
  $html = $dom->loadHTMLFile($path.$filename);

  //discard white space 
  $dom->preserveWhiteSpace = false; 

  //the table by its tag name
  $tables = $dom->getElementsByTagName('table'); 

  //get all rows from the table
  $rows = $tables->item(0)->getElementsByTagName('tr'); 

$numberofrows = $tables->item(0)->getElementsByTagName('tr')->length;

$numberincolumn = ceil($numberofrows / 4);

  $counter = 0;
echo '<div class="row-fluid"><div class="span3"><table>';
  // loop over the table rows

  foreach ($rows as $row) 
  { 


    if ($counter > 0 && $counter % $numberincolumn == 0){
    echo '</table></div><div class="span3"><table>';    
    }



   // get each column by tag name
      $cols = $row->getElementsByTagName('td'); 
   // echo the values  
      echo "<tr><td style='padding-left:10px;'>".$cols->item(0)->nodeValue.'</td>'; 
      echo "<td>".$cols->item(1)->nodeValue.'</td></tr>'; 
      $counter++;
    } 
    echo "</table></div></div>";
*/
?>
Metzed
  • 470
  • 1
  • 8
  • 27
  • 1
    Glad you figured it out. I was actually working on it just now, but my experience with the class is limited, so I'm learning as I go. – Expedito Jul 08 '13 at 17:06
0

try something link this

$data = "<TABLE BORDER=0>
  <TR><TD><FONT SIZE=1>Row 1 Col 1</TD><TD WIDTH+20><FONT SIZE=1>Row 1 Col 2</TD><TR>
<TR><TD><FONT SIZE=1>Row 2 Col 1</TD><TD WIDTH+20><FONT SIZE=1>Row 2 Col 2</TD><TR>
<TR><TD><FONT SIZE=1>Row 3 Col 1</TD><TD WIDTH+20><FONT SIZE=1>Row 3 Col 2</TD><TR>
...
</TABLE>";
$data = array_shift($data); // first array (table) 
$rows = explode("<TR>",$data); // you will loose the <TR> so you will need to add the <TR> back on to the begining

$numrows = count($rows);
Liam Sorsby
  • 2,912
  • 3
  • 28
  • 51