2012年6月2日土曜日

開発環境

『初めてのPHP5』 (David Sklar 著、 桑村 潤 翻訳、 廣川 類 翻訳、 オライリー・ジャパン、2005年、ISBN978-4-87311-257-2)の8章(クッキーとセッションでユーザを記憶)8.8(演習問題)4を解いてみる。

3.

コード(TextWrangler)

sample29.php (注文フォーム)

<?php
  session_start();
  $main_dishes = array('cuke' => 'Braised Sea Cucumber',
                       'stomach' => "Sauteed Pig's Stomach",
                       'tripe' => 'Sauteed Tripe with Wine Sauce',
                       'taro' => 'Stewed Pork with Taro',
                       'giblets' => 'Baked Giblets with Salt',
                       'abalone' => 'Abalone with Marrow and Duck Feet');
  if(array_key_exists('_submit_check',$_POST)){
    if($form_errors = validate_form()){
      show_form($form_errors);
    } else  {
      process_form();
    }
  } else {
    show_form();
  }
  
  function process_form(){
    global $main_dishes;
    $_SESSION['order'] = 1;
    foreach($main_dishes as $dish => $description){
      if(strlen($_POST[$dish])){
        $_SESSION[$dish] = $_POST[$dish];
      }
    }
    print "Thank you for your order.<br />";
    print "<a href='./sample30.php'>your order</a>";
  }
  
  function show_form($errors = ""){
    print<<<_HTML_
<form method="POST" action="$_SERVER[PHP_SELF]">
Braised Sea Cucumbe: <input type="text" name="cuke" value="$_SESSION[cuke]"/><br />
Sauteed Pig's Stomach: <input type="text" name="stomach" value="$_SESSION[stomach]"/><br />
Sauteed Tripe with Wine Sauce: <input type="text" name="tripe" value="$_SESSION[tripe]"/><br />
Stewed Pork with Taro: <input type="text" name="taro" value="$_SESSION[taro]"/><br />
Baked Giblets with Salt: <input type="text" name="giblets" value="$_SESSION[giblets]"/><br />
Abalone with Marrow and Duck Feet: <input type="text" name="abalone" value="$_SESSION[abalone]"/><br />
<input type="submit" value="Order" />
<input type="hidden" name="_submit_check" value="1" />
</form>
_HTML_;
    if($errors){
      print "<p>error</p><ul><li>";
      print implode("</li><li>",$errors);
      print "</li></ul>";
    }
  }
  
  function validate_form(){
    global $main_dishes;
    $errors = array();
    foreach($main_dishes as $dish => $description){
      if(strval($_POST[$dish]) &&
         ($_POST[$dish] != strval(intval($_POST[$dish])) ||
         (intval($_POST[$dish]) < 0))){
          $errors[] = "$description: Please enter a quantity";
      }
    }
    return $errors;
  }
?>

コード(TextWrangler)

sample30.php (決済)

<?php
  session_start();
  $main_dishes = array('cuke' => 'Braised Sea Cucumber',
                       'stomach' => "Sauteed Pig's Stomach",
                       'tripe' => 'Sauteed Tripe with Wine Sauce',
                       'taro' => 'Stewed Pork with Taro',
                       'giblets' => 'Baked Giblets with Salt',
                       'abalone' => 'Abalone with Marrow and Duck Feet');
  if(array_key_exists('_submit_check',$_POST)){
    process_form();
  } else {
    show_form();
  }
  
  function process_form(){
    global $main_dishes;
    unset($_SESSION['order']);
    foreach($main_dishes as $dish => $description){
      unset($_SESSION[$dish]);
    }
    print "注文完了<br />";
    print "<a href='./sample29.php'>order</a>(最初から)";
  }
  
  function show_form($errors = ""){
    global $main_dishes;
    if($_SESSION['order']){
      print "<ul>";
      foreach($main_dishes as $dish => $description){
        print "<li>$description: ".$_SESSION[$dish]."</li>";
      }
      print "</ul>";
    }
    print<<<_HTML_
<form method="POST" action="$_SERVER[PHP_SELF]">
<input type="submit" value="check out" />
<input type="hidden" name="_submit_check" value="1"/>
</form>
<a href="./sample29.php">order</a>
_HTML_;
  }
?>

HTMLソース(注文フォーム)

<form method="POST" action="/kamimura_blog/learning_php/sample29.php">
Braised Sea Cucumbe: <input type="text" name="cuke" value=""/><br />
Sauteed Pig's Stomach: <input type="text" name="stomach" value=""/><br />
Sauteed Tripe with Wine Sauce: <input type="text" name="tripe" value=""/><br />
Stewed Pork with Taro: <input type="text" name="taro" value=""/><br />
Baked Giblets with Salt: <input type="text" name="giblets" value=""/><br />
Abalone with Marrow and Duck Feet: <input type="text" name="abalone" value=""/><br />
<input type="submit" value="Order" />
<input type="hidden" name="_submit_check" value="1" />
</form>

HTMLソース(決済)

<form method="POST" action="/kamimura_blog/learning_php/sample30.php">
<input type="submit" value="check out" />
<input type="hidden" name="_submit_check" value="1"/>
</form>

0 コメント:

コメントを投稿