Jump to content

twittoris

Members
  • Posts

    87
  • Joined

  • Last visited

    Never

Everything posted by twittoris

  1. I have a table I am trying to import to mySQL I am having trouble assigning the right variable which represents a column header to the correct column header in the mySQL table. <?php /********************************/ /* Code at http://legend.ws/blog/tips-tricks/csv-php-mysql-import/ /* Edit the entries below to reflect the appropriate values /********************************/ $databasehost = "xxx"; $databasename = "xxx"; $databasetable = "names"; $databaseusername ="xxx"; $databasepassword = "xxx"; $fieldseparator = "\t"; $lineseparator = "\n"; $csvfile = "AddEnt2.txt"; $con = @mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error()); @mysql_select_db($databasename) or die(mysql_error()); $fcontents = file ('AddEnt2.txt'); # expects the csv file to be in the same dir as this script for($i=0; $i<sizeof($fcontents); $i++) { $line = trim($fcontents[$i],'\t'); // '\t' for tab delimeted $arr = explode('\t', $line); // '\t' for tab delimeted $name=implode("','",$arr); $PA=implode("','",$line); # if your data is comma separated # instead of tab separated # change the '\t' above to ',' $sql = "insert into $databasetable values ('$PA','$name')"; $sql = str_replace("''", mysql_escape_string("NULL"), $sql); $sql = str_replace("' '", mysql_escape_string("NULL"), $sql); mysql_query($sql); echo $sql ."<br>\n"; if(mysql_error()) { echo mysql_error() ."<br><b><font color=red>\n</b>"; } } ?>
  2. Also, Is it possible to store each table row in an array?
  3. i found some code that is helping me extract a table. Currently it works and extracts every single table row however it duplicates some of the rows. I am new to XPATH and would appreciate if someone could comment the following code so that I can alter it to meet my needs. Thanks. $newDom->appendChild($newDom->importNode($item,true)); $xpath = new DOMXPath( $newDom ); foreach ($item->attributes as $attribute) { for ($node = $item->firstChild; $node !== NULL; $node = $node->nextSibling) { if (($attribute->nodeName =='valign') && ($attribute->nodeValue=='top')) { print($node->nodeValue); } else { print("<br>".$node->nodeValue); } } print("<br>");
  4. yeah I have worked through all the other issues. This is my last one. I dont even know if its possible to put the output of a dom xpath into an array. I was hoping a veteran php-er would see what im trying to do and explain how to fix it if at all possible.
  5. The output is a table with 6 rows. I am trying to assign each row to a variable.
  6. I am trying to assign the output getElementsBtTagName to an array. I am new to arrays and so far i have this: foreach ($rows as $row) { /*** get each column by tag name ***/ $cols = $row->getElementsByTagName('td'); //Declare array $address = array(); if($row->childNodes->length) { foreach($row->childNodes as $ii) { $address[$ii->nodeName] = $ii ->nodeValue; } } $adresses[] = $address; /*** echo the values ***/ echo $cols->item(0)->nodeValue.'<br />'; echo '<hr />'; echo $address; ;
  7. I would like to place the output of these node values into an array: foreach ($rows as $row) { /*** get each column by tag name ***/ $cols = $row->getElementsByTagName('td'); /*** echo the values ***/ echo $cols->item(0)->nodeValue.'<br />'; echo $cols->item(1)->nodeValue.'<br />'; echo '<hr />'; } If the echo $cols->item(0)->nodeValue.'<br />'; always returns 6 values how can I call the values of each node later?
  8. I want it do escape the loop if it is not found like below: However this is not working. If ($i = 0; $i < $hrefs->length; $i++) { $href = $hrefs->item($i); $url = $href->getAttribute('href'); $purl = substr($url, 30,300); echo '<p>Found:<br />' . $purl. '<br />Retrieving Additional Information... '; } else { echo '<p>No Match Found For:<br />'.$row['name'].'<br />'; } It never gets to the else statement even if no link is found.
  9. I am running a search for a specific link on my site in order to make a big change but some of my entries are a little malformed. How do I make this script go to the next record if no match is found. Here are the two pieces of code: $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_assoc($result)){ echo $row['name']; echo "<br />"; //After the page is loaded search for specified links for ($i = 0; $i < $hrefs->length; $i++) { $href = $hrefs->item($i); $url = $href->getAttribute('href'); $purl = substr($url, 30,300); //Here I would like to continue the while loop //Maybe if i could find the value "No entities were found" it would escape it just as well? At the end of the code I would like to continue the while loop if i could find the value "No entities were found" on the page that is missing the link I need. Thanks for the help.
  10. I am having trouble obtaining the second table in this HTML code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><html> <head> <meta http-equiv="Pragma" content="no-cache"> <title>Entity Information</title> <link rel="stylesheet" href="documents/CORP/default.css" media="screen" type="text/css"> <link rel="stylesheet" href="documents/CORP/infotable.css" media="screen" type="text/css"> </head> <body> <div class="headings"> <h1 class="dos_heading1">Company Information </h1> <BR> <h2 class="dos_heading2"> </h2> <h3 class="page_heading">Company Information</h3> <p class="DtMsg">The information contained in this database is current through September 10, 2010.</p> </div> <hr noshade width="100%"> <center> <div class="highlight">Selected Company Name: 1 PARK ROW, LLC</div> <table summary="This table contains status information for the selected entity."> <caption> Selected Company Status Information </caption> <tr> <th>Current Company Name:</th> <td>1 PARK ROW, LLC</td> </tr> <tr> <th>Initial Filing Date:</th> <td>JANUARY 22, 1997</td> </tr> <tr> <th>County:</th> <td>NASSAU</td> </tr> <tr> <th>Jurisdiction:</th> <td>NEW JERSEY </td> </tr> <tr> <th>Entity Type:</th> <td>DOMESTIC LIMITED LIABILITY COMPANY</td> </tr> <tr> <th>Current Company Status:</th> <td>ACTIVE </td> </tr> </table> <BR> <table id="tblAddr" summary="This table contains address information for the selected entity."> <caption>Selected Entity Address Information</caption> <tr> <th scope="col" id="c1" class="leftalign"><span class="rmvbold">Address</span></th> </tr> <tr> <td headers="c1"> C/O ELECTRONICS INC.<br> 2355 PARK ROW<br> NEW BRUNSWICK, NEW YORK, 15538 </td> </tr> <tr> <th scope="col" id="c4" class="leftalign">Registered Agent</th> </tr> <tr> <td headers="c1"> NONE </td> </tr> </table> <br> <p style="width:50%"> </p> <div id="divStockContainer"> <h4 id="capStock">*Stock Information</h4> <div id="divStock"> <table id="tblStock" cellpadding="0" cellspacing="6"> <tr> <th># of Shares</th> <th>Type of Stock</th> <th>$ Value per Share</th> </tr> <tr> <td> </td> <td>No Information Available</td> <td> </td> </tr> </table> </div> <p id="pStockBlurb">*Stock information is applicable to domestic business corporations.</p> </div> <div id="divHistNmContainer"> <h4 id="capNmHist">Name History</h4> <div id="divNmHist"> <table id="tblNameHist" cellpadding="0" cellspacing="6"> <tr> <th class="FileDt">Filing Date</th> <th class="NameType">Name Type</th> <th class="CorpName">Company Name</th> </tr> <tr> <td class="FileDt">JAN 22, 1997</td> <td class="NameType">Actual</td> <td class="CorpName">1 PARK ROW, LLC</td> </tr> </table> </div> <p id="pFictName"> </p> </div> </center> </body> </html> <script language='javascript' src='https://a12.alphagodaddy.com/hosting_ads/gd01.js'></script></script> I can get the top part with this: /*** a new dom object ***/ $dom = new domDocument; /*** load the html into the object ***/ $dom->loadHTML($html2); /*** 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'); /*** loop over the table rows ***/ foreach ($rows as $row) { /*** get each column by tag name ***/ $cols = $row->getElementsByTagName('td'); /*** echo the values ***/ echo $cols->item(0)->nodeValue.'<br />'; echo $cols->item(1)->nodeValue.'<br />'; echo $cols->item(2)->nodeValue; echo '<hr />'; } } Any help would be appreciated I dont even know where to start to echo the 2nd table in the html. Thanks.
  11. I think you can use xpath with domdocument or get_file_contents
  12. 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
  13. Ill Pay $20 via paypal if someone can break those tables down into variables ready to be placed in a mysql database.
  14. Can I use Preg-Match in a DOMDocument?
  15. 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 />'; } } ?>
  16. 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
  17. 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.
  18. yes the html is the same as www.empirebuildingsestate.com/company.html I keep it in a variable called $ch2
  19. 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"; ?>
  20. 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 />'; } ?>
  21. 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 />'; } ?>
  22. 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 />'; } ?>
  23. so that puts the node in the arrary? How do I access each piece of it? Im new at this sorry for the novice questions.
×
×
  • 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.