Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

Extracting data from an html table

I am trying to convert my html table to a php array. (Table here: http://goldenjoe.xyz/messages/), but I seem to be having a problem. The rows are generated using a json file and the php script reads directly from the index.html on the server, which shows the tbody is empty (because they are generated).

php:
PHP Code

<?php
    $htmlContent 
= file_get_contents("http://goldenjoe.xyz/messages/");
 
       
    $DOM 
= new DOMDocument();
 
   $DOM->loadHTML($htmlContent);
 
   
    $Header 
= $DOM->getElementsByTagName('th');
 
   $Detail = $DOM->getElementsByTagName('tr');

 
   //#Get header name of the table
 
   foreach($Header as $NodeHeader) 
 
   {
 
       $aDataTableHeaderHTML[] = trim($NodeHeader->textContent);
 
   }
 
   //print_r($aDataTableHeaderHTML); die();

 
   //#Get row data/detail table without header name as key
 
   $i = 0;
 
   $j = 0;
 
   foreach($Detail as $sNodeDetail) 
 
   {
 
       $aDataTableDetailHTML[$j][] = trim($sNodeDetail->textContent);
 
       $i = $i + 1;
 
       $j = $i % count($aDataTableHeaderHTML) == 0 ? $j + 1 : $j;
 
   }
 
   print_r($aDataTableDetailHTML); die();
?>

index.html:
Code
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"> </script>

<script>

   $(function() {


  var messages = [];

  $.getJSON('messages.json', function(data) {
      $.each(data.items, function(i, f) {
         var tblRow = "<tr>" + "<td>" + f.storage.url +
          "<td>" + f.message.headers.to + "<td>" + f.message.headers.subject + "<td>"
          $(tblRow).appendTo("#userdata tbody");
    });

  });

});
</script>
</head>

<body>

<div class="wrapper">
<div class="profile">
  <table id= "userdata" border="2">
 <thead>
           <th>Url</th>
           <th>Target</th>
           <th>Subject</th>
       </thead>
     <tbody>

      </tbody>
  </table>

</div>
</div>

</body>
</html>

When I run the php I get the error
Code
Notice: Undefined variable: aDataTableDetailHTML in /home/gueruex/public_html/messages/test2.php on line 26

Which makes sense because the variable "aDataTableDetailHTML" is declared in the foreach loop, but that loop isn't running because it does not see any <td> tags in the index.html

When you load the actual site up though, the table loads and the <td> tags are present.
Spoiler (Click to View)

Does anyone here know of a workaround I could use. I've been trying to download the complete webpage and generate a separate html file to use, but wget and curl just download the index.html directly off the server.
You just said you own the website. If that's the case, just create an API that outputs a JSON object and read it that way instead of parsing the HTML table.
Thanks for the suggestion. I decided to do that instead of the html table and everything worked out!
PHP Code

<?php
$raw 
= file_get_contents("http://goldenjoe.xyz/messages/messages.json");
if(
$raw !== false){
    
$json = json_decode($raw);
    foreach(
$json->items as $entry){
        
// do something with each $entry in the list
        
var_dump($entry);
    }
} 
(Feb 1, 2019 01:35 PM)Topkek_Is_Life Wrote:  Which makes sense because the variable "aDataTableDetailHTML" is declared in the foreach loop, but that loop isn't running because it does not see any <td> tags in the index.html

Because PHP will see the HTML code you posted in OP, not the one you see in the browser, and in that one there is no <td>.
You see, Doctor, God didn't kill that little girl. Fate didn't butcher her and destiny didn't feed her to those dogs.
If God saw what any of us did that night he didn't seem to mind. From then on I knew... God doesn't make the world this way.
We do.


Possibly Related Threads...
Thread / Author Replies Views Last Post
by .eXeAug 28, 2023 05:07 AM 1 201 Aug 28, 2023 05:07 AM
Last Post: Nachfolger
[Javascript]  by flexing_99Apr 29, 2023 12:02 AM 3 269 Apr 29, 2023 12:02 AM
Last Post: Zgh600
[Node.JS] by bevcOct 26, 2022 11:02 PM 6 262 Oct 26, 2022 11:02 PM
Last Post: Ramsey