14

--------------EDIT------------------------

So i am going with the DOM approach. Here is what I have so far:

  <?php function getdata(){
    $contents = file_get_contents('internatdata.htm');
    //create a DOM based off of the string from the html table
     $DOM = new DOMDocument;
   $DOM->loadHTML($contents);

   //get all tr and td
   $items = $DOM->getElementsByTagName('tr');
   $tds = $DOM->getElementsByTagName('td');

   function tdrows($elements){
       $str = "";
       for ($ii =0; $ii < $elements->length; $ii++){
            $str .= $elements->item($ii)->nodeValue . ",";


           }
          return $str;
       }

   for ($i = 0; $i < $items->length; $i++){


       echo tdrows($tds) . "; <br />";

       }

    } 
?>

The issue I am having is that I only want to select the td's from each table row. I am trying to achieve this with a nested loop. unfortunately It is printing the text of every tag on the page how ever many times as there are tags. how can i get it so its only printing the td of each tr and not every td on the dom?


I need to use an html table as the source of my data because I don't have access to the database. I figure to be able to query data from the html table I need create a function to convert the table into an array, or a multidimensional array.

I have the basic Idea I think but I need some help finishing the code to return an array based off the html table.

Also If you have a better way of doing this other than converting the table to an array then please let me know

Here is the idea I had so far:

 <?php
 function getdata(){

    $contents = file_get_contents('data.htm');
    //add delimiters (semicolon for a row and comma for a cell) ???

    $stripped = strip_tags($contents);

    //explode into an array based off the delimiters above ???


    } 
    ?>
hakre
  • 193,403
  • 52
  • 435
  • 836
JDV590
  • 651
  • 3
  • 15
  • 33
  • 4
    The best way would be to use a DOM parser, for example http://php.net/manual/en/book.dom.php – jli Dec 09 '11 at 17:13
  • 1
    http://stackoverflow.com/questions/3627489/php-parse-html-code – Marijn van Vliet Dec 09 '11 at 17:14
  • Do 'data.htm' only contains a table and its contents, or it is a full html page with many more elements? Anyway, your code is still lacking all the part related to the parsing of the table elements. IMO, I would look for an alternative way: I do not like the idea of reading an html table. – jap1968 Dec 09 '11 at 17:15
  • @jap1968 the data.htm file contains only the html table I need to use – JDV590 Dec 09 '11 at 17:17
  • @jli can you see my edits above and offer any advice...thanks – JDV590 Dec 09 '11 at 18:14
  • @mumis2012 I submitted an answer that fixes your edit. – jli Dec 09 '11 at 19:00
  • @mumis2012 You still around? Did you get this fixed? – jli Dec 12 '11 at 04:13

3 Answers3

29

I've updated your edit to fix it.

function tdrows($elements)
{
    $str = "";
    foreach ($elements as $element) {
        $str .= $element->nodeValue . ", ";
    }

    return $str;
}

function getdata()
{
    $contents = "<table><tr><td>Row 1 Column 1</td><td>Row 1 Column 2</td></tr><tr><td>Row 2 Column 1</td><td>Row 2 Column 2</td></tr></table>";
    $DOM = new DOMDocument;
    $DOM->loadHTML($contents);

    $items = $DOM->getElementsByTagName('tr');

    foreach ($items as $node) {
        echo tdrows($node->childNodes) . "<br />";
    }
}

getdata();
Freddy789
  • 506
  • 4
  • 15
jli
  • 6,523
  • 2
  • 29
  • 37
1

One way to make this easier is to use a dom parser http://simplehtmldom.sourceforge.net/ .

You are still going to have to extract the information into an array but this will make it easier to iterate through the elements one by one.

jakx
  • 748
  • 5
  • 8
-2

You should consider using XML.

It is much easier than HTML table and much more sufficient.

Example: http://www.php.net/manual/en/simplexml.examples-basic.php

I'll-Be-Back
  • 10,530
  • 37
  • 110
  • 213