0

我正在尝试使用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>

先感谢您

4

2 回答 2

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>";
    }
于 2013-10-15T16:01:46.547 回答
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>";
    }
}
于 2013-10-16T16:58:54.563 回答