gpt4 book ai didi

php - 尝试在php中获取非对象简单html dom的属性

转载 作者:行者123 更新时间:2023-11-30 01:12:06 24 4
gpt4 key购买 nike

我试图用 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>

提前谢谢

最佳答案

试试这个

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>";
}

关于php - 尝试在php中获取非对象简单html dom的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19385491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com