How to add a PHP to my html table? [closed]

OK first of all, you don’t put PHP into HTML.

PHP is processed at the server, HTML, at the client.

Meaning by time the browser pieces the HTML together, the PHP has already been processed.

From what I can see you’d like [what you have echo’d] to be placed into a HTML element…

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

See how I’ve replaced the echo’s with $output .=? PHP is now appending those strings to the $output variable. That $variable can then be outputted anywhere on your page. I.E:

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

You should also know about include() but I won’t explain as people have already answered accordingly.

Leave a Comment