Posts Tagged HTML

Turn a text list into an HTML Select Option list with PHP

Easy solution to this basic HTML coding need posted over at Stack Overflow by a random clever bloke. Thanks RCB.

<?php

  $fc = file_get_contents('./test.txt');
  $fc = utf8_encode ( $fc );
	$lines = explode("\n", $fc);

  $html = '<select>';
  foreach($lines as $line)
     $html .= '<option value="' . $line . '">' . $line . '</option>';

  $html .= '</select>';
  echo $html;
?>

My only issue was with UTF-8 characters coming through garbled. Bodged a workaround using this encode entities plug-in for Dreamweaver.

,

No Comments

Include an HTML file in a PHP variable

I couldn’t get PHP to accept an HTML file include as the definition of a variable without throwing my templated page into disarray.  An answer came from the venerable desilva then checked with the php folk; use output  buffering (see example six in that last link):

<?php
$string = get_include_contents('somefile.php');

function get_include_contents($filename) {
 if (is_file($filename)) {
 ob_start();
 include $filename;
 $contents = ob_get_contents();
 ob_end_clean();
 return $contents;
 }
 return false;
}

?>

Also worth a look: file_get_contents

Why? Because Coda, my favourite Macintosh text editor+,  doesn’t do syntax highlighting of HTML for a PHP string. The site I’ve just finished for a high-end luxury villa in Marbella I had a big block of HTML to include (and work on) for the images page…

,

No Comments

How to group items in HTML select menus

Here’s something I learnt last week, just as the title says: how to group items in an (x)HTML select drop down list. This trick, which I’d assumed was done with magic (or rather DHTML or JavaScript trickery), is actually very easy to achieve being straight forward HTML markup.

[Found in a referenced post linked to by Laura Carlson's excellent [webdev] reference pages (subscribe to the list server here)…]

…the secret, as explained by “Web Teacher” here, is simply to wrap the items you want to group  in the select list with

<optgroup label="group_title"> and </optgroup>

Another good point made there is that to make a select list multiple choice you just need to add the attribute multiple="multiple" into the opening select tag (and make it clear to the users that this is the case as well as how to actually select mulitiple items…)

,

No Comments