内部PHP-OOP while循环仅运行一次(在外部PHP-OOP while循环的第一次迭代中)

时间:2019-07-22 15:33:14

标签: php mysql mysqli

所以我有一个mysqli_result,我用while循环(使用mysqli_fetch_assoc())进行了迭代。

在内部,我试图遍历结果中的字段,该字段仅在外部循环的第一次迭代中起作用。

while ($r = mysqli_fetch_assoc($result)) {
    //column for deletion
    echo "<tr>
            <td>                            
                <form method='post' action='$_SERVER[PHP_SELF]' class='delete'>
                    <button type='submit' name='btnDelete' class='save' value='$r[id]' ><i class='glyphicon glyphicon-trash'></i></button>
                </form>
            </td>";

    //all fields in the result
    while ($f = $result->fetch_field()) { //old: foreach($fields as $f); $fieldID = $f['COLUMN_NAME'];
        $fieldID = $f->name;
        if (strpos($fieldID, 'id') !== false) {
            echo "<td>
                    <div class='name'>$r[$fieldID]</div>
                </td>";
        } else {
            echo "<td class='editable'>
                    <div class='name'><i class='glyphicon glyphicon-edit'></i>$r[$fieldID]</div>
                    <form method='post' action='$_SERVER[PHP_SELF]' class='edit_name'>
                        <input type='text' name='txtName' value=$r[$fieldID] size='15'>
                        <button type='submit' name='btnSave' class='save' value='$r[id]'><i class='glyphicon glyphicon-floppy-save'></i></button>
                        <button type='button' class='cancel'><i class='glyphicon glyphicon-remove'></i></button>
                    </form>
                </td>";
        }
    }
    echo    "</tr>";
}

在此之前,我的解决方案是通过SQL查询来获取表中的字段,该方法虽然有效,但似乎有点多余,因为我可以从结果中获取字段。

0 个答案:

没有答案