I'm scraping the following html table:
<table>
<tr>
<td class="Name">A</td>
<td class="S1">5</td>
<td class="S2">6</td>
</tr>
</table>
My goal is to use Html_simple_dom in order to parse the data and input the values into a MySQL database. Here's what I have so far:
<?php
include('../simple_html_dom.php');
include('dbconnect.php');
$html = file_get_html('url');
$table = $html->find('table');
foreach ($table->find('tr') as $row) {
foreach ($row->find('td[class=Name]') as $cell) {
$name = $cell->plaintext;
}
}
The issue I'm running into is that my $name variable is actually an array. I'm getting stuck with duplicates if I do this instead:
foreach ($table->find('tr') as $row) {
foreach ($row->find('td[class=Name]') as $cell) {
}
$name = $cell->plaintext;
}
My ultimate goal would be a MySQL query such as this:
$sql = Insert into ScoreTable (Score1, Score2)
Values ($S1, $S2)
Where PName = $Name
However I can't separate the array values I'm getting when I "find" and I can't even isolate the html elements into variables. Where am I going wrong?
edit: Fixed what my goal is.
$nameis not an array, and it is being reassigned every time through the nested foreach loops.foreach $table->find('tr') as $row) { $data = array('name' => $row>find('td[class=Name]')->plaintext, 'S1' => $row>find('td[class=S1]')->plaintext, 'S2 => $row>find('td[class=Name]')->plaintext); }assuming that your syntax for finding the classname is correct. I haven't used html_simple_dom, but I would expect it works this way.class=Nametoclass=S2. Too close to bedtime...find()method didn't find what it was looking for, so when we asked for theplaintextproperty, there was no object (what it should have found) for it to get the property from. This would happen if you havetrs without the classes you're looking for. My bad, I shouldn't have chained it together. You'll have to test to see if thefind()method actually is successful before trying to get theplaintext.