- 2008 : 01 02 03 04 05 06 07 08 09 10 11 12
- 2007 : 01 02 03 04 05 06 07 08 09 10 11 12
- 2006 : 01 02 03 04 05 06 07 08 09 10 11 12
- 2005 : 01 02 03 04 05 06 07 08 09 10 11 12
- 2004 : 01 02 03 04 05 06 07 08 09 10 11 12
- 2003 : 01 02 03 04 05 06 07 08 09 10 11 12
- 2002 : 01 02 03 04 05 06 07 08 09 10 11 12
2006-04-18
PHP で DOM を使って HTML を出力
- 準備
- output (インデント付与)
- 応用例
都道府県の一覧の<select>タグを出力
- usage
- Reference
PHP: DomDocument->html_dump_mem - Manual
http://jp.php.net/manual/ja/function.domdocument-html-dump-mem.php
# aptitude install php4-domxml
- PHPをソースから入れている場合
php.iniを編集
extension_dir = "/usr/lib/php4/20020429"
- sample
// 文章を生成する $doc = domxml_new_doc("1.0"); $root = $doc->create_element("html"); // ルートとなる要素生成 (<html>タグ) $root->set_attribute('lang','ja'); // <html>タグに lang 属性を追加 $root = $doc->append_child($root); // <head>タグ生成 $head = $doc->create_element("head"); $head = $root->append_child($head); // <title>タグ生成 $title = $doc->create_element("title"); $title = $head->append_child($title); // <title>タグの中にテキスト挿入 $text = $doc->create_text_node("This is the title"); $text = $title->append_child($text); // HTMLを出力 echo $doc->html_dump_mem();
- output (インデント付与)
- 応用例
都道府県の一覧の<select>タグを出力
function generateSelectHtml($items,$id,$default){ $doc = domxml_new_doc("1.0"); $root = $doc->create_element('select'); $root->set_attribute('name',$id); $root->set_attribute('id',$id); $root= $doc->append_child($root); // 初期表示のテキストが設定されていたとき if (!empty($default)){ $option = $doc->create_element('option'); $option->set_attribute('value',''); if(empty($_SESSION[$id])){ $option->set_attribute('selected','selected'); } /* Add text node */ $text = $doc->create_text_node($default); $option->append_child($text); $root->append_child($option); } foreach($items as $i){ $option = $doc->create_element('option'); $option->set_attribute('value',$i); if ($_SESSION[$id] == $i){ $option->set_attribute('selected','selected'); } /* Add text node */ $text = $doc->create_text_node($i); $option->append_child($text); $root->append_child($option); } return $doc->html_dump_mem(); } function generatePrefSelectHtml(){ $default = '都道府県を選択してください。'; $items = array( '北海道', '青森県', '岩手県', '宮城県', '秋田県', '山形県', '福島県', '茨城県', '栃木県', '群馬県', '埼玉県', '千葉県', '東京都', '神奈川県', '新潟県', '富山県', '石川県', '福井県', '山梨県', '長野県', '岐阜県', '静岡県', '愛知県', '三重県', '滋賀県', '京都府', '大阪府', '兵庫県', '奈良県', '和歌山県', '鳥取県', '島根県', '岡山県', '広島県', '山口県', '徳島県', '香川県', '愛媛県', '高知県', '福岡県', '佐賀県', '長崎県', '熊本県', '大分県', '宮崎県', '鹿児島県', '沖縄県', ); return generateSelectHtml($items,'pref',$default); }
- usage
<?= generatePrefSelectHtml() ?>
- Reference
PHP: DomDocument->html_dump_mem - Manual
http://jp.php.net/manual/ja/function.domdocument-html-dump-mem.php
- 2008 : 01 02 03 04 05 06 07 08 09 10 11 12
- 2007 : 01 02 03 04 05 06 07 08 09 10 11 12
- 2006 : 01 02 03 04 05 06 07 08 09 10 11 12
- 2005 : 01 02 03 04 05 06 07 08 09 10 11 12
- 2004 : 01 02 03 04 05 06 07 08 09 10 11 12
- 2003 : 01 02 03 04 05 06 07 08 09 10 11 12
- 2002 : 01 02 03 04 05 06 07 08 09 10 11 12
2006-04 /