2013年1月5日土曜日

開発環境

『初めてのPHP5』 (David Sklar 著、 桑村 潤 翻訳、 廣川 類 翻訳、 オライリー・ジャパン、2005年、ISBN978-4-87311-257-2)の11章(XMLのパースと生成)11.4(演習問題)4を解いてみる。

4.

HTML、PHPのソースコード(BBEdit)

sample111.php

<?php
  require 'formhelpers.php';
  if(array_key_exists('_submit_check', $_POST)){
    if($errors = validate_form()){
      show_form($errors);
    } else {
      process_form();
    }
  } else {
    show_form();
  }
  function show_form($errors=''){
    if($errors){
      print "エラー<ul><li>";
      print implode("</li><li>", $errors);
      print '</li></ul>';
    }
    $defaults = array();
    if(array_key_exists('_submit_check', $_POST)){
      $defaults = $_POST;
    }
    print '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '">';
    print '検索用語: ';
    input_text("search_q", $defaults);
    input_submit("submit", "submit");
    print '<input type="hidden" name="_submit_check" value="1" />';
    print '</form>';
  }
  function validate_form(){
    $errors = array();
    if(!strlen($_POST['search_q'])){
      $errors[] = "検索語を入力してください";
    }
    return $errors;
  }
  function process_form(){
    $xml = simplexml_load_file('http://news.yahoo.com/rss/');
    print '検索結果<ul>';
    foreach($xml->channel->item as $item){
      if(stristr($item->title, $_POST['search_q'])){
        print '<li><a href="' . $item->link . '" >' .
          htmlentities($item->title) . '</a></li>';
      }
    }
    print '</ul>';
    print '<a href="' . $_SERVER['PHP_SELF'] . '">戻る</a>';
  }
?>

ちなみにJavaScriptの場合。

コード(BBEdit)

<script>
function clicked(){
  var search_q = $('#t0').val();
  $.ajax(option);
  var option = {
                  type:"GET",
                  url: "http://news.yahoo.com/rss/",
                  dataType:"html",
                  success: function(xhr, text_status){
                    var search_q = $('#t0').val();
                    var ol = $(document.createElement('ol'));
                    var links = [];
                    var titles = [];
                    $(xhr).find('channel > item > link').each(function(){
                      links.push($(this).text());
                    });
                    $(xhr).find('channel > item > title').each(function(){
                      titles.push($(this).text());
                    });
                    for(var i = 0; i < links.length; i++){
                      if(titles[i].indexOf(search_q) >= 0){
                        var li = $(document.createElement('li'));
                        var a = $(document.createElement('a'));
                        a.attr('href', links[i]);
                        a.append(titles[i]);
                        li.append(a);
                        ol.append(li);
                      };
                    }
                    $('#d0').html("");
                    $('#d0').append(ol);
                  },
                  error: function(exhr, text_status, error){
                    $('#d0').html('');
                    $('#d0').append('<p>エラー!</p>')
                  },
                  complete: function(xhr, text_status){
                    $('#d0').append("<p>終了</p>");
                  }
               }
  $.ajax(option);
}
function f(e){
  var e = e ? e : window.event;
  if(e.keyCode === 13) clicked();
}
</script>
<label>検索用語: <input id="t0" type="text" value="" onkeydown="f()" /></label>
<input id="btn0" type="button" value="検索" onclick="clicked()" />
<div id="d0">
</div>
ここに表示

0 コメント:

コメントを投稿