猿问

如何使用 PHP 显示表格列?

我试图仅在 php/html 中 $edit = 1 时显示两列。所以做了以下事情:


<?php

   if ($edit == 1)

        {

    <td align="center"><a href="editor/editor.php?id=<?php echo $row["id"]; ?>" onclick="handleLinkClick(event);">Edit</a></td>

    <td align="center"><a href="removedoc.php?id=<?php echo $row["id"]; ?>" onclick="swalremove(event);">Remove</a></td>

         }

?>

但我收到一个错误:


Parse error: syntax error, unexpected '< on line 177

我该如何解决这个问题?


30秒到达战场
浏览 224回答 3
3回答

慕沐林林

更新:<?php&nbsp; &nbsp;if ($edit == 1)&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp;<td align="center"><a href="editor/editor.php?id=<?php echo $row['id']; ?>" onclick="handleLinkClick(event);">Edit</a></td>&nbsp; &nbsp; &nbsp;<td align="center"><a href="removedoc.php?id=<?php echo $row['id']; ?>" onclick="swalremove(event);">Remove</a></td>&nbsp; &nbsp; &nbsp; }?>至:<?php&nbsp; &nbsp;if ($edit == 1)&nbsp; &nbsp;{&nbsp; &nbsp; echo '<td align="center"><a href="editor/editor.php?id='.$row['id'].'" onclick="handleLinkClick(event);">Edit</a></td>';&nbsp; &nbsp; echo '<td align="center"><a href="removedoc.php?id='.$row["id"].'" onclick="swalremove(event);">Remove</a></td>';&nbsp;}?>或者:<?php if ($edit == 1) { ?>&nbsp; &nbsp; &nbsp; &nbsp;<td align="center"><a href="editor/editor.php?id=<?php echo $row['id']; ?>" onclick="handleLinkClick(event);">Edit</a></td>&nbsp; &nbsp; &nbsp; &nbsp; <td align="center"><a href="removedoc.php?id=<?php echo $row['id']; ?>" onclick="swalremove(event);">Remove</a></td>&nbsp; &nbsp; &nbsp;<?php } ?>

温温酱

像这样做 :<?phpif ($edit == 1) { ?>&nbsp; &nbsp; <td align="center"><a href="editor/editor.php?id=<?php echo $row["id"]; ?>" onclick="handleLinkClick(event);">Edit</a></td>&nbsp; &nbsp; <td align="center"><a href="removedoc.php?id=<?php echo $row["id"]; ?>" onclick="swalremove(event);">Remove</a></td><?php } ?>您需要使用正确的 php 标记约定。不要将 HTML 与 PHP 混合使用。在编写原始 HTML 之前始终关闭 PHP 标记。您也可以仅在 PHP 中使用它,如下所示:<?phpif ($edit == 1) {&nbsp;&nbsp; &nbsp; echo&nbsp;&nbsp; &nbsp; '<td align="center"><a href="editor/editor.php?id='.$row["id"].'" onclick="handleLinkClick(event);">Edit</a></td>&nbsp; &nbsp; <td align="center"><a href="removedoc.php?id='.$row["id"].'" onclick="swalremove(event);">Remove</a></td>';} ?>

拉丁的传说

您可以定义一个 HTML 字符串并将您可能拥有的任何元素添加到其中,然后在最后回显它。它要简单得多:$html = '';if ($edit == 1) {&nbsp; &nbsp; $html .= '<td align="center"><a href="editor/editor.php?id=' . $row["id"] . '" onclick="handleLinkClick(event);">Edit</a></td>';&nbsp; &nbsp; $html .= '<td align="center"><a href="removedoc.php?id=' . $row["id"] . '" onclick="swalremove(event);">Remove</a></td>';}echo $html;
随时随地看视频慕课网APP
我要回答