spec36 Posted October 6, 2011 Share Posted October 6, 2011 I'm trying to learn writing php scripts with html-simple-dom. I am scrapping my website with html-simple-dom and the below code, but I am running into an issue with inserting the scrapped data into my database. This code scraps the website and grabs the html data I want. $ret = $html->find('table[class=data] tr'); foreach($ret as $visitor){ $visitor = $visitor->find('td','1') . "<br>"; $insert="INSERT INTO $dbtable (visitor) VALUES ('$visitor')"; mysql_query($insert) OR die(mysql_error()); } But this is being inserted into the database "visitor" field. <td colspan="1" rowspan="1" style="text-align: left;"><a style="border-bottom:1px dotted;" onclick="loadTeamSpotlight(jQuery(this));" rel="HEN" href="javascript:void(0);">HENRY</a></td><br> I only want "HENRY" to be extracted out of the html code above and inserted into the database table. Any help would be greatly appreciated, as I have been pulling my hair out trying to figure this out. Do I need to use explode or something? Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/248531-database-insertion-not-working/ Share on other sites More sharing options...
joel24 Posted October 6, 2011 Share Posted October 6, 2011 if the insert always follows that formatting, $ret = $html->find('table[class=data] tr'); foreach($ret as $visitor){ $visitor = $visitor->find('td','1') . "<br>"; $replaceArray=array('<td colspan="1" rowspan="1" style="text-align: left;"><a style="border-bottom:1px dotted;" onclick="loadTeamSpotlight(jQuery(this));" rel="HEN" href="javascript:void(0);">','</a></td><br>'); $visitor=str_replace($replaceArray,'',$visitor); $insert="INSERT INTO $dbtable (visitor) VALUES ('$visitor')"; mysql_query($insert) OR die(mysql_error()); } ##TEST## $string='<td colspan="1" rowspan="1" style="text-align: left;"><a style="border-bottom:1px dotted;" onclick="loadTeamSpotlight(jQuery(this));" rel="HEN" href="javascript:void(0);">HENRY</a></td><br>'; $replaceArray=array('<td colspan="1" rowspan="1" style="text-align: left;"><a style="border-bottom:1px dotted;" onclick="loadTeamSpotlight(jQuery(this));" rel="HEN" href="javascript:void(0);">','</a></td><br>'); $string=str_replace($replaceArray,'',$string); echo $string; Quote Link to comment https://forums.phpfreaks.com/topic/248531-database-insertion-not-working/#findComment-1276337 Share on other sites More sharing options...
awjudd Posted October 6, 2011 Share Posted October 6, 2011 I would go one layer deeper where you are looking for 'td' and actually look for the 'a' tag. Then you would be able to grab the innertext. /* An extension to your code */ $visitor = $visitor->find('td','1')->find('a','1')->innertext . "<br>"; ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/248531-database-insertion-not-working/#findComment-1276338 Share on other sites More sharing options...
spec36 Posted October 7, 2011 Author Share Posted October 7, 2011 Thank you so much for the help guys/girls. When I run this code: /* An extension to your code */ $visitor = $visitor->find('td','1')->find('a','1')->innertext . "<br>"; I receive this error message when running the script Fatal error: Call to a member function find() on a non-object in C:\Program Files\EasyPHP-5.3.8.0\www\nhl\index.php on line 42 This error seems to be happening , because of the extra ->find and ->innertext that was added to the statement. I didn't know you could extend the statement like that, it is very cool, but must need to be written differently. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/248531-database-insertion-not-working/#findComment-1276666 Share on other sites More sharing options...
spec36 Posted October 7, 2011 Author Share Posted October 7, 2011 Thanks for the info Joel. Your code gave me a clear idea about how to go about this, but turns out the database has two inserts before that html I posted that contains <br>. So two <br>'s are posted to the table and then all the other records follow what I posted. Quote Link to comment https://forums.phpfreaks.com/topic/248531-database-insertion-not-working/#findComment-1276667 Share on other sites More sharing options...
awjudd Posted October 7, 2011 Share Posted October 7, 2011 Try the stuff from their documentation with their use of children? Or better yet: $visitior = $visitor -> find ( 'td a', '1') -> innertext; I have never used this plugin before so it is just a guess. ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/248531-database-insertion-not-working/#findComment-1277088 Share on other sites More sharing options...
awjudd Posted October 7, 2011 Share Posted October 7, 2011 And by plugin I mean open source project / api. ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/248531-database-insertion-not-working/#findComment-1277094 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.