我试图用SIMPLE HTML DOM显示每一行表格。另外,在第2行链接的末尾添加
<?php
include('simple_html_dom.php');
//Connection
$link = mysqli_connect("localhost", "root", "", "webapp");
mysqli_set_charset($link, "utf8");
//Checks connection
if (mysqli_connect_errno($link)) { exit("Failed to connect to MySQL: " . mysqli_connect_error()); }
//Selects content of DB
$postContent = mysqli_query($link, "SELECT post_content FROM wp_posts WHERE ID=1");
//Creates array from content
$result = mysqli_fetch_array($postContent);
//Loads content as first element of array
$html = str_get_html($result[0]);
foreach($html->find('tr') as $element) {
$temphtml = str_get_html($element->innertext);
$tempico = $temphtml->find('a', 0)->innertext; //line #42
echo "<tr>" . $temphtml . "<td><a href=\"delete.php?ico=" . $tempico . "\">Delete</a> // <a href=\"change.php?ico=" . $tempico . "\">Change</a></td></tr>";
}
?>
$tempico
是后来用作url参数的数字。在第42行,我试图从行中的第一个<a>
标记之间获取它。
我的问题出现在第42行,我收到错误Notice: Trying to get property of non-object in C:\wamp\www\control.php on line 42
我有什么方法可以做得更好或者没有得到这个错误?
这是数据库中的html
<table class="tg-table-orange">
<tbody id="SK">
<tr id="47 387 131">
<td><a href="link" class="ico">47 387 131</a></td>
<td class="47 387 131">BARDOSA</td>
<td>s.r.o.</td>
<td>06.09.2013</td>
<td class="47 387 131"><a class="button1" href="link">Zarezervuj</a></td>
</tr>
</tbody>
</table>
提前谢谢
答案 0 :(得分:0)
试试这个
HTML
<table class="tg-table-orange">
<tbody id="SK">
<tr id="47 387 131">
<td><a href="link" class="ico">47 387 131</a></td>
<td class="47 387 131">BARDOSA</td>
<td>s.r.o.</td>
<td>06.09.2013</td>
<td class="47 387 131"><a class="button1" href="link">Zarezervuj</a></td>
</tr>
</tbody>
</table>
PHP
foreach($html->find('tr') as $element) {
foreach($element->find('td') as $td)
{
$temphtml = str_get_html($td->innertext);
$tempico = $temphtml->plaintext;
break;
}
echo "<tr>" . $temphtml . "<td><a href=\"delete.php?ico=" . $tempico . "\">Delete</a> // <a href=\"change.php?ico=" . $tempico . "\">Change</a></td></tr>";
}
答案 1 :(得分:0)
问题是,如果没有<a>
标记,则返回错误,因此我在循环中创建了IF。
foreach($htmlSK->find('tr') as $element) {
$temphtml = str_get_html($element->innertext);
if(gettype($temphtml->find('a', 0)) == "NULL") {
echo "<tr>" . $temphtml . "</tr>";
} elseif(gettype($temphtml->find('a', 0)->innertext) == "string") {
$tempico = $temphtml->find('a', 0)->innertext;
echo "<tr>" . $temphtml . "<td><a href=\"delete.php?ico=" . $tempico . "\">Delete</a> // <a href=\"change.php?ico=" . $tempico . "\">Change</a></td></tr>";
}
}