Jump to content

Breaking Down a table


twittoris

Recommended Posts

I am trying to break down the tables i have on my website in order to place them into my mySQL database. I am new to php and have been having some trouble and any help would be greatly appreciated.

 

So far I have this but it doesnt seem to be working. I am using DOMDocument but I wonder if maybe preg_match woulf be better?

 

 

Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\Hosting\5915198\html\2.php on line 11

 

 

 

 

<?php
// Load html string
$dom = new DOMDocument();
$dom->loadHTML('www.empirebuildingsestate.com/company.html');

// Get tables from html
$tables = $dom->getElementsByTagName('tr');


// Get rows from tables
$rows = $tables->item(0)->getElementsByTagName('td');

// Loop over each row
foreach ($rows as $row)
{
   // Get each column by tag name
   $cols = $row->getElementsByTagName('td');
   
   
   // Echo values (here you can assign them in array for example)
   echo $cols->item(0)->nodeValue.'<br />';
   
   echo '<hr />';
   }
?>

Link to comment
https://forums.phpfreaks.com/topic/213295-breaking-down-a-table/
Share on other sites

First, turn on error reporting so you will see the errors that are being generated:

 

error_reporting(E_ALL);
ini_set('display_errors', '1');

 

Second, you need to use loadHTMLFile().  Third, you'll need to use the full URL (http://).

I made those changes and got the following errors:

 

Warning: DOMDocument::loadHTMLFile() [function.DOMDocument-loadHTMLFile]: URL file-access is disabled in the server configuration in D:\Hosting\5915198\html\2.php on line 6

 

Warning: DOMDocument::loadHTMLFile(http://www.empirebuildingsestate.com/company.html) [function.DOMDocument-loadHTMLFile]: failed to open stream: no suitable wrapper could be found in D:\Hosting\5915198\html\2.php on line 6

 

Warning: DOMDocument::loadHTMLFile() [function.DOMDocument-loadHTMLFile]: I/O warning : failed to load external entity "http://www.empirebuildingsestate.com/company.html" in D:\Hosting\5915198\html\2.php on line 6

 

Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\Hosting\5915198\html\2.php on line 1

 

 

 

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Load html string
$dom = new DOMDocument();
$dom->loadHTMLfile('http://www.empirebuildingsestate.com/company.html');

// Get tables from html
$tables = $dom->getElementsByTagName('tr');


// Get rows from tables
$rows = $tables->item(0)->getElementsByTagName('td');

// Loop over each row
foreach ($rows as $row)
{
   // Get each column by tag name
   $cols = $row->getElementsByTagName('td');
   
   
   // Echo values (here you can assign them in array for example)
   echo $cols->item(0)->nodeValue.'<br />';
   
   echo '<hr />';
   }
?>

 

 

That means that allow_url_fopen = OFF in php.ini.  It needs to be ON in order for you to access files by URL using most functions.  If you don't have access to php.ini then you are probably out of luck.  You might be able to fetch the page via curl or using this (haven't tested):

 

$dom->loadHTML(http_get('http://www.empirebuildingsestate.com/company.html'));

 

Fatal error: Call to undefined function http_get() in D:\Hosting\5915198\html\2.php on line 6

 

Changed to your suggestion and received the above error.

 

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Load html string
$dom = new DOMDocument();
$dom->loadHTML(http_get('http://www.empirebuildingsestate.com/company.html'));

// Get tables from html
$tables = $dom->getElementsByTagName('tr');


// Get rows from tables
$rows = $tables->item(0)->getElementsByTagName('td');

// Loop over each row
foreach ($rows as $row)
{
   // Get each column by tag name
   $cols = $row->getElementsByTagName('td');
   
   
   // Echo values (here you can assign them in array for example)
   echo $cols->item(0)->nodeValue.'<br />';
   
   echo '<hr />';
   }
?>

What if I do it with this code? Could someone help me edit it to match the table on the above website?

<?php
$dom = new DomDocument();
@$dom->loadHTML($allcont);
$xpath = new DomXPath($dom);

$ranks = $xpath->query('/html/body//td[@class="rank"]');
$names = $xpath->query('/html/body//td[@class="name"]');
$killss = $xpath->query('/html/body//td[@class="kills"]');
$moneys = $xpath->query('/html/body//td[@class="money"]');
$poss = $xpath->query('/html/body//td[@class="pos"]');

$rows = 20;

echo "<table style='width:500px;padding:2px;background-color:#000000;color:#ffffff;font-size:14px;'>\n";
for ($i = 0; $i < $rows; $i++) {
echo "<tr>\n";
echo "<td style='text-align:right'>\n";
	$pos = trim($poss->item($i)->nodeValue);
	if ($pos == "Server Rank") {
		echo "<div style='color:#4ECAFF'><b>$pos</b></div>\n";
	}
	else {
		echo "<div style='color:#ffffff'>$pos</div>\n";
	}
echo "<td style='text-align:left'>\n";
	$rank = trim($ranks->item($i)->nodeValue);
	if ($rank == "Rank") {
		echo "<div style='color:#4ECAFF'><b>$rank</b></div>\n";
	}
	else {
		echo "<div style='color:#ffffff'>$rank</div>\n";
	}
echo "</td>\n";
echo "<td style='text-align:left'>\n";
	$name = trim($names->item($i)->nodeValue);
	if ($name == "Name") {
		echo "<div style='color:#4ECAFF'><b>$name</b></div>\n";
	}
	else {
		echo "<div style='color:#ffffff'>$name</div>\n";
	}
echo "</td>\n";
echo "<td style='text-align:right'>\n";
	$kills = trim($killss->item($i)->nodeValue);
	if ($kills == "Kills") {
		echo "<div style='color:#4ECAFF'><b>$kills</b></div>\n";
	}
	else {
		echo "<div style='color:#ffffff'>$kills</div>\n";
	}
echo "</td>\n";
echo "<td style='text-align:right'>\n";
	$money = trim($moneys->item($i)->nodeValue);
	if ($money == "Money") {
		echo "<div style='color:#4ECAFF'><b>$money</b></div>\n";
	}
	else {
		echo "<div style='color:#ffffff'>$money</div>\n";
	}
echo "</td>\n";
echo "</td>\n";
echo "</tr>";
}
echo "</table>\n";
?>

 

The problem isnt displaying the info that was grabbed from the cURL. the problem I am having is extracting the table and assigning variables to each table row. So that at a different point I can add each variable into a databse.

 

OK, that's a different problem than the first post showed, but there are other problems.  Reread your code and the comments, they don't match:

 

// Load html string
$dom = new DOMDocument();
$dom->loadHTML($ch2);

// YOU ARE GETTING TR NOT TABLE!
// Get tables from html
//$tables = $dom->getElementsByTagName('tr');
$tables = $dom->getElementsByTagName('table');

// YOU ARE GETTING TD NOT TR!
// Get rows from tables
//$rows = $tables->item(0)->getElementsByTagName('td');
$rows = $tables->item(0)->getElementsByTagName('tr');

// Loop over each row
foreach ($rows as $row)
{
   // Get each column by tag name
   $cols = $row->getElementsByTagName('td');
      
   // Echo values (here you can assign them in array for example)
   echo $cols->item(0)->nodeValue.'<br />';
   
   echo '<hr />';
}

Now it is giving me a parsing error:

 

// make the cURL request to $target_url
$html2 = curl_exec($ch2);

// Create DOMDocument
$dom = new DOMDocument();

// Load html string
$dom->loadHTML($html2);

// Get tables from html
$tables = $dom->getElementsByTagName('table');


// Get rows from tables
$rows = $tables->item(0)->getElementsByTagName('tr');

// Loop over each row
foreach ($rows as $row);
{

// Get each column by tag name
 echo $cols = $row->getElementsByTagName('td');


// Echo values (here you can assign them in array for example)
echo $cols->item(0)->nodeValue.'<br />';

echo '<hr />';
}
?>

 

Parse error: syntax error, unexpected $end

I appreciate the syntax corrections. They are mostly due to my re-copying and pasting. I am sorry for the messy code. However, still have the same issue. What is the best method to extract the points in the table I want into $variables?

At one point i got the nodelist to display the top table but not the bottom table with the address. Does having two loops and two posts in the same script prohibit me at all from pulling data?

// make the cURL request to $target_url
$html2 = curl_exec($ch2);


// Create DOMDocument
$dom = new DOMDocument();

// Load html string
$dom->loadHTML($html2);

// Get tables from html
$tables = $dom->getElementsByTagName('th');


// Get rows from tables
$rows = $tables->item(0)->getElementsByTagName('tr');

// Loop over each row
foreach ($rows as $row)
{

// Get each column by tag name
$cols = $row->getElementsByTagName('td');


// Echo values (here you can assign them in array for example)
echo $cols->item(0)->nodeValue.'<br />';


echo '<hr />';
}

}
?>

 

That would be awesome. I just want to set the table values into variables (maybe an array?) so that as i go through the company pages I can enter these values into my mySQL table.

 

Current Company Name: $CCN

Initial Filing Date: $Date

County: $County

Jurisdiction: $JUR

Entity Type: $ET

Current Company Status: $CCS

 

Address: $addr

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.