【问题标题】:How to add a PHP to my html table? [closed]如何将 PHP 添加到我的 html 表中? [关闭]
【发布时间】:2018-03-18 22:16:49
【问题描述】:

我需要在我的 html 文件中实现这个 PHP,这会转到一个目录检查那里的文件并创建一个包含这些选项的组合框......现在我如何才能在我的 html 代码中的特定位置调用它。

<?php
$dir = 'xml/';

$exclude = array('somefile.php', 'somedir');

// Check to see if $dir is a valid directory
if (is_dir($dir)) {
  $contents = scandir($dir);

  echo '<select class="dropdown-toggle" id="combo">';

  foreach($contents as $file) {
  // This will exclude all filenames contained within the $exclude array
  // as well as hidden files that begin with '.'
  if (!in_array($file, $exclude) && substr($file, 0, 1) != '.') {
  echo '<option>'. $file .'</option>';
  }
  }

  echo '</select>';
  }
  else {
  echo "The directory <strong>". $dir ."</strong> doesn't exist.";
  }
?>

【问题讨论】:

  • 问题如何将 PHP 添加到我的 html 表中? 让我相信您应该阅读基础知识
  • “我需要在我的html文件中实现这个php”,你的意思是反过来吗?
  • 请注意 HTML 是由浏览器和服务器上的 PHP 解析和显示的。因此,您不能简单地在 HTML 中放置/调用一些 PHP 代码来执行某些操作...
  • 听起来你没有PHP经验。这是一个正确的假设吗?如果是这样,听起来你需要一个基本的 PHP 教程,因为你所问的没有一个快速、简单的答案,你需要了解一些基础知识。您不只是“将 PHP 添加到 HTML 文件”。也许这是一个不错的起点:php.net/manual/en/tutorial.php
  • 如果我运行这段代码,它就像一个魅力......但现在我需要将它包含在我的 html 代码中......我该怎么做?

标签: php html filesystems


【解决方案1】:

首先,你不要将 PHP 放入 HTML

PHP 在服务器端处理,HTML 在客户端处理。

意味着浏览器将 HTML 拼凑在一起的时间,PHP 已经被处理了。

据我所知,您希望将 [您所回显的内容] 放入 HTML 元素中...

<?php
$dir = 'xml/';
$output;

$exclude = array('somefile.php', 'somedir');

// Check to see if $dir is a valid directory
if (is_dir($dir)) {
  $contents = scandir($dir);

  $output .= '<select class="dropdown-toggle" id="combo">';

  foreach($contents as $file) {
  // This will exclude all filenames contained within the $exclude array
  // as well as hidden files that begin with '.'
  if (!in_array($file, $exclude) && substr($file, 0, 1) != '.') {
  $output .= '<option>'. $file .'</option>';
  }
  }

  $output .= '</select>';
  }
  else {
  $output .= "The directory <strong>". $dir ."</strong> doesn't exist.";
  }
?>

看看我是如何用$output .= 替换回声的? PHP 现在将这些字符串附加到 $output 变量中。然后可以在页面上的任何位置输出该 $variable。即:

<table>
<tr>
<td>
<?php echo $output ?>
</td>
</tr>
</table>

你也应该知道include(),但我不会解释,因为人们已经做出了相应的回答。

【讨论】:

  • 它没有显示在 html 文件中...我不必像在标头中的 javascript 中那样调用 php 吗?
  • 是的,您可以将代码(答案中的第一个块)放在顶部,在 &lt;head&gt;&lt;html&gt; 之前,以便写入 $output 变量。
  • 你能告诉我,如果 php 位于 index.html 的不同文件中,如 selects.php 并使用它,我该如何调用它?
  • 是的,只需将代码块放在selects.php 文件中,然后在您的 index.php 中,您将把 &lt;?php include("selects.php") ?&gt; 放在您的 &lt;head&gt;
  • 你能检查一下我在这里做错了什么吗? 4zdesigner.com/markers 我更改为在标题和 td 中再次调用 echo 并且它不显示:(
【解决方案2】:

您可以创建一个包含在 php 中生成的选择并使用 javascript 加载数据的 div,例如jquery:http://api.jquery.com/load/

【讨论】:

    【解决方案3】:

    你的意思是这样的吗?

    <html>
    ...
    <?php include('codesnippet.php') ?>
    ...
    </html>
    

    或其他方式(在 HTML 文档中显示代码)?

    <pre>
      <code>
        <!-- your snippet goes here, with escaped characters -->
      </code>
    </pre>
    

    【讨论】:

      【解决方案4】:

      您可以将您的 php 直接插入到您的 html 页面中,只要您希望此选择元素出现。假设您的服务器将为 php 标签解析一个 html 文件(这是非常标准的),那么它将像那样工作。如果没有,请尝试将 .html 文件重命名为 .php。

      我建议将您的 php 保存在一个单独的文件中(即 generate_select.php),然后将其包含在您希望的任何位置,就像这样;

      <table>
      <tr>
      <td>
      <?php include('generate_select.php'); ?>
      </td>
      </tr>
      </table>
      

      您可以随时使用 php 开始和结束标记插入和退出 php 代码。

      【讨论】:

        猜你喜欢
        • 2012-11-29
        • 2014-06-16
        • 1970-01-01
        • 1970-01-01
        • 2010-09-28
        • 1970-01-01
        • 1970-01-01
        • 2012-01-19
        相关资源
        最近更新 更多