I have a database table which consists the following format of data in one column.
<table cellspacing="1" cellpadding="0" border="0" width="395">
<tbody>
<tr>
<td valign="top" width="135">
<p>Calories (kcal)<br>Energy (kj)<br>Fats<br>Carbohydrates<br>Protein<br></p>
</td>
<td valign="top">
<p>178<br>748<br>0 g<br>9.6 g<br>0.1 g<br></p>
</td>
<td valign="top" width="135">
<p>Fiber<br>Sugars<br>Cholesterol<br>Sodium<br>Alcohol<br></p>
</td>
<td valign="top">
<p>0 g<br>-<br>0 mg<br>-<br>26.2 g<br></p>
</td>
</tr>
</tbody>
</table>
I want to make another database which has separate columns for Calories, Fats, Carbohydrates and Protein.
To separate this data, I need to fetch data from the old database and parse it like this.
$qry = "SELECT * FROM table";
$res = $mysqli->query($qry);
// new dom object
$dom = new DOMDocument();
while ($row = $res->fetch_assoc()) {
$html = @$dom->loadHTML($row['columndata']);
//the table by its tag name
$tables = $dom->getElementsByTagName('table');
$rows = $tables->item(0)->getElementsByTagName('tr');
foreach ($rows as $row)
{
$cols = $row->getElementsByTagName('td');
echo $cols->item(0)->nodeValue.'<br />';
echo $cols->item(1)->nodeValue.'<br />';
}
}
This outputs the following:
Calories (kcal)Energy (kj)FatsCarbohydratesProtein
1787480 g9.6 g0.1 g
I am unable to separate the output string to have correct column values in the new database.
For example, I want to have value 178 in the Calories column, 0 g in the Fats column, etc.
parse the data based on the HTML tag??nodeValueremoves all the tags, you want something like Javascript'sinnerHTML. The DOMDocument API doesn't have this, you need to write it. There's a simpleget_inner_htmlfunction in the comments at php.net/manual/en/class.domelement.php