Jump to content

Database Insertion Not Working


spec36

Recommended Posts

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,

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.