基礎から解る!PHP入門講座 プログラム入門に最適な言語、PHPを学ぼう

基礎から解る!PHP入門講座

プログラム入門に最適な言語、PHPを学ぼう      

 このサイトはPHPを独習する際に作成したスクリプトを基に、プログラム言語初心者のための入門講座として作成しました。PHPは初心者に親しみやすく、解りやすいプログラム言語です。これからプログラムを学ぶ人の参考になればと思います。


    投票アンケートシステム2 投票画面

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html lang="ja"><head>
    <meta http-equiv="Content-type" content="text/html; charset=EUC-JP">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <link rel="stylesheet" href="../style.css">
    <title>アンケート</title></head>
    <body><div class="file2">
    <h2>アンケート</h2>
    <?php
    error_reporting(1);
    /*---------------------------------------------------データを読出し配列に格納*/
    $updir = "enq/enquete";
    if(file_exists("./$updir")){
      $enq = file($updir);
    /*---------------------------------------------------BAND名をアルファベット順に並べる*/
      $array_cnt = count($enq);
      $stnum = 0;
      while($array_cnt > $stnum){
        $text = split("#",$enq[$stnum]);
        $select[$stnum] = $text[1];
      $stnum++;
      }
    sort($select);} else {
      echo "1件の投票もありません";
    }
    /*---------------------------------------------------入力画面表示*/
    echo "このアンケートは「ファイル操作」のアンケートのスクリプトをfunctionを使って書き換えたもので、<br>";
    echo "動作はまったく同じものです。スクリプト解説は「ファイル操作」を見てください。";
    echo "<form action='01_02_enq_b.php' method='post'>";
    echo "<table border='0' width='400'><tr>";
    echo "<td>あなたの好きなROCK BANDは?</td></tr>";
    echo "<tr><td><select name='band_1'>";
    echo "<option></option>";
    $stnum = 0;
    while($array_cnt > $stnum){
        $select[$stnum] = str_replace("¥n", '', $select[$stnum]);
        echo "<option value='$select[$stnum]'>$select[$stnum]</option>";
    $stnum++;
    }
    echo "</select></td></tr>";
    echo "<tr><td><input type='text' name='band_2' size='31'></td></tr>";
    echo "<tr><td><input type='submit' name='enter' value='投票!'></table></form>";
    echo "表示形式が崩れないよう、全角15文字、半角30文字以内の追加制限があります。<br>";
    echo "連続投票を防ぐため、一度投票すると30秒間次の投票ができません。<br>";
    echo "記入せずに投票をクリックすると結果を見ることができます。";
    ?>

    </div></body></html>

    投票アンケートシステム2 結果画面

    <?php
    error_reporting(1);
    header("Content-Type: text/html; charset=EUC-JP");
    header("Expires: Thu, 01 Dec 1994 16:00:00 GMT");
    header("Last-Modified: ". gmdate("D, d M Y H:i:s"). " GMT");
    header("Cache-Control: no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
    /*---------------------------------------------------データの受け取り等*/
    $band_1 = $_POST['band_1'];
    $band_2 = $_POST['band_2'];
    $band_2 = strip_tags($band_2);    /*HTMLタグを取去る*/
    $word = split("amp;",$band_2);      /*&が&amp;となるためamp;を削除する*/
    $band_2 = $word[0].$word[1];
    $updir = "enq/enquete";
    $band_2 = str_replace('#', "#", $band_2);
    $band_2 = str_replace("¥'", "’", $band_2);
    $band_2 = str_replace("¥¥", '', $band_2);
    /*---------------------------------------------------クッキーで投票時刻を記憶*/
    if($band_1 != "" or $band_2 != ""){
      $time=$_COOKIE['enq_time'];
      if($time==null){
        $time = time();
        setcookie("enq_time", $time, time()+30);
      }
    }
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html lang="ja"><head>
    <meta http-equiv="Content-type" content="text/html; charset=EUC-JP">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <link rel="stylesheet" href="../style.css">
    <title>アンケート</title></head>
    <body><div class="file2">
    <h2>アンケート</h2>
    <?php
    /*---------------------------------------------------ユーザー定義関数の設定*/
    function MAKE_FILE(){
      global $updir;
      global $band_2;
      $filepointer=fopen("./$updir", "a+");
        flock($filepointer, LOCK_EX);
          fputs($filepointer, "1"."#".$band_2."¥n");
        flock($filepointer, LOCK_UN);
      fclose($filepointer);
      echo "新規にファイルを作成して1件目の登録を記録しました";
    }
    /*---------------------------------------------------ユーザー定義関数の設定*/
    function CHECK_ADD(){
      global $enq;
      global $updir;
      global $band_2;
      global $array_cnt;
      $stnum = 0;
      while($array_cnt > $stnum){
        $text = split("#",$enq[$stnum]);
          if($text[1] == $band_2."¥n"){
            echo "$band_2 は既に登録済です。プルダウンから選択してください。";
            $check = "no";
          }
        $stnum++;
      }
      if($check != "no"){
        $length =mb_strwidth($band_2);      /*英語と日本語を区別して文字数を数える*/
          if($length > 30){
            echo "全角15文字、半角30文字以内で記入してください。";
          } else {
        $filepointer=fopen("./$updir", "a");
          flock($filepointer, LOCK_EX);
            fputs($filepointer, "1"."#".$band_2."¥n");
              flock($filepointer, LOCK_UN);
            fclose($filepointer);
          echo "投票受け付け、新規データを追加しました。";
          }
      }
    }
    /*---------------------------------------------------ユーザー定義関数の設定*/
    function SELECT(){
      global $enq;
      global $updir;
      global $band_1;
      global $array_cnt;
      $filepointer=fopen("./$updir", "w");
        flock($filepointer, LOCK_EX);
        $band_1 = $band_1."¥n";
        $stnum = 0;
        while($array_cnt > $stnum){
          $text = split("#",$enq[$stnum]);
            if($text[1] == $band_1){
              $text[0] ++;
            }
          fputs($filepointer, $text[0]."#".$text[1]);
        $stnum++;
        }
        flock($filepointer, LOCK_UN);
      fclose($filepointer);
      echo "投票を受付け、得票数を追加しました。";
    }
    /*---------------------------------------------------書込みの制御構造*/
    $nowstamp = time();
    $timediff = $nowstamp-$time;
    if($timediff < 30 and $timediff > 0){
      $diff = 30-$timediff;          /*クッキーのタイムスタンプと今のタイムスタンプを比較*/
      echo "<br>$diff 秒後に投票できます<br>";
    } else if ($band_2 != "" or $band_1 != "") {
      if(!file_exists("./$updir")){
        MAKE_FILE();            /*ファイルがない場合の作成*/
      } else {
        $enq = file($updir);        /*データを読込み配列に格納する*/
        $array_cnt = count($enq);
        if($band_1 == "" and $band_2 <> ""){
          CHECK_ADD();          /*重複チェックと追加*/
        } else if ($band_1 <> "" and $band_2 == ""){
          SELECT();            /*データ選択の場合*/
        }
      }
    }
    /*---------------------------------------------------結果表示のため改めてデータ読み出し*/
    if(file_exists("./$updir")){
      $filename = "./$updir";
      $enq = file($filename);
      rsort($enq,SORT_NUMERIC);
      $array_cnt = count($enq);
      $top = split("#",$enq[0]);
    /*---------------------------------------------------全投票数を計算*/
      $stnum = 0;
      while($array_cnt > $stnum){
        $text = split("#",$enq[$stnum]);
        $cnt_all += $text[0];
      $stnum++;
      }
    /*---------------------------------------------------結果を表示*/
      if($top[0] > 0 or $band_2 != ""){
      echo "<table border='1'>";
      echo "<tr><td align='center'>順位</td><td align='center'>得票</td>";
      echo "<td width='150'>BAND NAME</td><td width='320'>得票率グラフ</td></tr>";
      $stnum = 0;
        while($array_cnt > $stnum){
          $text = split("#",$enq[$stnum]);
          $stnum_2 = $stnum +1;
            if($last_cnt == $text[0]){    /*得票数が同じ場合順位を調整*/
              $stnum_2 = $last_num;
            }
          $percent = $text[0]/$top[0] *280;  /*得票数を1位の得票数で割り、280(横幅)をかけてグラフの長さを得る*/
          $per_all = (int)($text[0]/$cnt_all*10000)/100;  /*得票数を総数で割り得票率を得る*/
          $last_cnt = $text[0];
          $last_num = $stnum_2;  
          echo "<tr><td align='center'>$stnum_2</td>";
          echo "<td align='center'>$text[0]</td><td>$text[1]</td>";
          echo "<td><img src='bar.gif' width = $percent height='10'> $per_all"."%</td></tr>";
        $stnum++;
        }
      echo "</table>";
      }
    } else {
      echo "1件の投票もありません";
    }

    echo "<br><a href='01_02_enq_a.php'>投票画面に戻る</a><br>";

    ?>

    </div></body></html>
    2023/12
    SUN MON TUE WED THU FRI SAT
              1 2
    3 4 5 6 7 8 9
    10 11 12 13 14 15 16
    17 18 19 20 21 22 23
    24 25 26 27 28 29 30
    31       


     フォクすけ©2006 Mozilla Japan



    制作:基礎から解るPHP入門講座 代表:FREEZE

    Copyright © 2005/2/28 flzPHP.All Rights Riserved