twittoris Posted September 13, 2010 Share Posted September 13, 2010 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 />'; } ?> Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted September 13, 2010 Share Posted September 13, 2010 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://). Quote Link to comment Share on other sites More sharing options...
twittoris Posted September 13, 2010 Author Share Posted September 13, 2010 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 />'; } ?> Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted September 13, 2010 Share Posted September 13, 2010 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')); Quote Link to comment Share on other sites More sharing options...
twittoris Posted September 13, 2010 Author Share Posted September 13, 2010 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 />'; } ?> Quote Link to comment Share on other sites More sharing options...
twittoris Posted September 13, 2010 Author Share Posted September 13, 2010 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"; ?> Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted September 13, 2010 Share Posted September 13, 2010 Ummm... do you have the HTML in the $allcont variable? If so, how did you get it there? Quote Link to comment Share on other sites More sharing options...
twittoris Posted September 13, 2010 Author Share Posted September 13, 2010 yes the html is the same as www.empirebuildingsestate.com/company.html I keep it in a variable called $ch2 Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted September 13, 2010 Share Posted September 13, 2010 Well, it doesn't matter which method you use, the original problem was loading the HTML from a URL. If you already have it then use either method and use: $dom->loadHTML($ch2); Quote Link to comment Share on other sites More sharing options...
twittoris Posted September 13, 2010 Author Share Posted September 13, 2010 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. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted September 13, 2010 Share Posted September 13, 2010 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 />'; } Quote Link to comment Share on other sites More sharing options...
twittoris Posted September 13, 2010 Author Share Posted September 13, 2010 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 Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted September 13, 2010 Share Posted September 13, 2010 You need to get an editor with syntax highlighting, though it may not catch this. Look at the end of your foreach() line. Quote Link to comment Share on other sites More sharing options...
twittoris Posted September 13, 2010 Author Share Posted September 13, 2010 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 />'; } } ?> Quote Link to comment Share on other sites More sharing options...
twittoris Posted September 13, 2010 Author Share Posted September 13, 2010 Can I use Preg-Match in a DOMDocument? Quote Link to comment Share on other sites More sharing options...
twittoris Posted September 13, 2010 Author Share Posted September 13, 2010 Ill Pay $20 via paypal if someone can break those tables down into variables ready to be placed in a mysql database. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted September 13, 2010 Share Posted September 13, 2010 I can give it to you here for free, but need to know the exact result you want. Quote Link to comment Share on other sites More sharing options...
twittoris Posted September 13, 2010 Author Share Posted September 13, 2010 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.