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

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

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

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


    テーブル・メーカー

    <?php
    error_reporting(1);
    $enter = $_POST['enter'];
    $select = $_POST['select'];
    $tate = $_POST['tate'];  /*02*/
    $yoko = $_POST['yoko'];  /*03*/
    $updir = "./table/";
    /*-------------------------------------------------*/
      if($handle = opendir($updir)){
        $cnt=0;
        while (false !== ($file = readdir($handle))) {
          if(ereg("tabledata",$file)){  
              $filenames[$cnt] = $file;
          }
        $cnt++;
        }
      closedir($handle);
      }
      sort ($filenames);
      $f_num = count($filenames);
    /*-------------------------------------------------データ保存*/
    if($enter == "保存"){
      $table_id = $_POST['table_id'];  /*0*/
      $t_name = $_POST['t_name'];  /*1*/
      $table_num = $_POST['table_num'];  /*4*/
      $t_width = $_POST['t_width'];  /*5*/
      $left_mrgn = $_POST['left_mrgn'];  /*6*/
      $line = $_POST['line'];  /*7*/
      $line_color = $_POST['line_color'];  /*8*/
      $bg_color1 = $_POST['bg_color1'];  /*9*/
      $bg_color2 = $_POST['bg_color2'];  /*10*/
      $bg_color_top = $_POST['bg_color_top'];  /*11*/
      $bg_color_left = $_POST['bg_color_left'];  /*12*/
      $top_size = $_POST['top_size'];  /*13*/
      $top_color = $_POST['top_color'];  /*14*/
      $top_bold = $_POST['top_bold'];  /*15*/
      $left_size = $_POST['left_size'];  /*16*/
      $left_color = $_POST['left_color'];  /*17*/
      $left_bold = $_POST['left_bold'];  /*18*/
      $other_size = $_POST['other_size'];  /*19*/
      $other_color = $_POST['other_color'];  /*20*/
      $other_bold = $_POST['other_bold'];  /*21*/
    /*-------------------------------------------------*/
      $line_color = strip_tags($line_color);
      $line_color = str_replace("&#&", '', $line_color);
      $line_color = str_replace("%%%", '', $line_color);
      $line_color = str_replace("¥¥", "¥", $line_color);
    /*-------------------------------------------------*/
      $bg_color1 = strip_tags($bg_color1);
      $bg_color1 = str_replace("&#&", '', $bg_color1);
      $bg_color1 = str_replace("%%%", '', $bg_color1);
      $bg_color1 = str_replace("¥¥", "¥", $bg_color1);
    /*-------------------------------------------------*/
      $bg_color2 = strip_tags($bg_color2);
      $bg_color2 = str_replace("&#&", '', $bg_color2);
      $bg_color2 = str_replace("%%%", '', $bg_color2);
      $bg_color2 = str_replace("¥¥", "¥", $bg_color2);
    /*-------------------------------------------------*/
      $top_color = strip_tags($top_color);
      $top_color = str_replace("&#&", '', $top_color);
      $top_color = str_replace("%%%", '', $top_color);
      $top_color = str_replace("¥¥", "¥", $top_color);
    /*-------------------------------------------------*/
      $left_color = strip_tags($left_color);
      $left_color = str_replace("&#&", '', $left_color);
      $left_color = str_replace("%%%", '', $left_color);
      $left_color = str_replace("¥¥", "¥", $left_color);
    /*--------------------------------------フォームに入力されたデータをwhileを使って受け取る*/
      $cnt = 1;
      while($cnt < $table_num){
        $table= "table".$cnt;
        $t_data[$cnt] = $_POST[$table];
        $cnt++;
      }
    /*-------------------------------------------------データから、使用禁止語を抜く*/
      $ch_cnt = 1;
      while($ch_cnt < $table_num){
        $t_data[$ch_cnt] = strip_tags($t_data[$ch_cnt]);
        $t_data[$ch_cnt] = str_replace("&#&", '', $t_data[$ch_cnt]);
        $t_data[$ch_cnt] = str_replace("%%%", '', $t_data[$ch_cnt]);
        $t_data[$ch_cnt] = str_replace("¥¥", "¥", $t_data[$ch_cnt]);  /*-¥を入力されたら¥に変える*/
        $t_data[$ch_cnt] = str_replace("¥¥", "¥", $t_data[$ch_cnt]);  /*-なぜか¥¥と2つになる!?原因不明*/
        $ch_cnt++;
      }
    /*-------------------------------------------------表の列ごとの場所を受け取る、*/
      $algn_num = $_POST['algn_num'];
      $algn_cnt = 1;
      while($algn_cnt <= $algn_num){
        $align2 = "align".$algn_cnt;
        $algn_data[$algn_cnt] = $_POST[$align2];
        $algn_cnt++;
      }
    /*-------------------------------------------------保存するデータ1*/
      $save = $table_id."&#&".$t_name."&#&".$tate."&#&".$yoko."&#&".$table_num."&#&".$t_width."&#&".$left_mrgn;
      $save .= "&#&".$line."&#&".$line_color."&#&".$bg_color1."&#&".$bg_color2."&#&".$bg_color_top."&#&".$bg_color_left;
      $save .= "&#&".$top_size."&#&".$top_color."&#&".$top_bold."&#&".$left_size."&#&".$left_color."&#&".$left_bold;
      $save .= "&#&".$other_size."&#&".$other_color."&#&".$other_bold;
    /*-------------------------------------------------保存するデータ2*/
      $save .= "%%%";
      $s_cnt= 1;
      while($s_cnt < $table_num){
        $save .= "&#&".$t_data[$s_cnt];
      $s_cnt++;
      }
    /*-------------------------------------------------保存するデータ3*/
      $save .= "%%%";
      $al_cnt= 1;
      while($al_cnt <= $algn_num){
        $save .= "&#&".$algn_data[$al_cnt];
      $al_cnt++;
      }
    /*-------------------------------------------------データ書込み*/
      $handle = fopen("./table/tabledata_$table_id","w") ;
        fwrite($handle, $save);
      fclose($handle);
      $now_data = file_get_contents("./table/tabledata_$table_id");
    /*-------------------------------------------------新規に保存するファイル名を決める*/
    }elseif($enter == "作成/編集" and $select == "新規"){
      $new_name = $_POST['new_name'];
      $f_num++;
      $cnt = 1;
      while($cnt <= $f_num){
        if(!in_array("tabledata_$cnt", $filenames)) {
          break;
        }
      $cnt++;
      }
      $f_num--;
    /*---------------------------5個以上ファイルを作らないように5になったら2〜4に変えて上書*/
      if("tabledata_$cnt" == "tabledata_5"){
        $rand = rand(2,4);    
        $cnt = $rand;
      }
    /*-------------------------------------------------データの書込み*/
      if($new_name != "" and $cnt != ""){
        $save = $cnt."&#&".$new_name."&#&".$tate."&#&".$yoko;
        $pointer=fopen("./table/tabledata_$cnt", "w");
          flock($pointer, LOCK_EX);
            fputs($pointer, $save);
          flock($pointer, LOCK_UN);
        fclose($pointer);
      }
      $now_data = file_get_contents("./table/tabledata_$cnt");
    /*-------------------------------------------------保存したファイルを読む*/
    }elseif($enter == "作成/編集" and $select != ""){
      $now_data = file_get_contents("./table/tabledata_$select");}
    /*-------------------------------------読み込んだデータを%%%でまず3つにわける、その後&#&でわける*/
      $r_data = split("%%%",$now_data);
      $text = split("&#&",$r_data[0]);
      $sel_text = split("&#&",$r_data[1]);
      $algn_text = split("&#&",$r_data[2]);
    ?>

    <html lang="ja"><head>
    <title>テーブル・メーカー</title>
    <meta name="description" content=" ">
    <meta name="keywords" content="">
    <meta http-equiv="Content-Type" content="text/html; charset=EUC-JP" />
    <meta http-equiv="Content-Language" content="ja-jp" />
    <link rel="stylesheet" href="../style.css"></head>
    <body><div class="file2" style="width:760">
    <center>
    <?php
    echo "<table width='700' border='1'><tr><td><br>";
    /*-------------------------------------------------作成した表の名前を配列に入れる*/
    $l_cnt = 0;
    while($l_cnt < $f_num){
      $f_data = file_get_contents("./table/$filenames[$l_cnt]");
      $l_text = split("&#&",$f_data);
      $list_a[$l_cnt] = $l_text[0];        /*番号*/
      $list_b[$l_cnt] = $l_text[1];        /*表の名前*/
    $l_cnt++;
    }
    /*-------------------------------------------------配列をセレクトに入れる*/
    echo "<form action='table_maker.php' method='post'>";
    if($text[1] != ""){
      echo " <b>作成/編集する表の名前:$text[0]-$text[1]</b>";
    }else{
      echo " 表:<select name='select'>";
      echo "<option value='新規'>新規</option>";
      $t_cnt = 0;
      while($t_cnt < $f_num){
        echo "<option value='$list_a[$t_cnt]'>$list_b[$t_cnt]</option>";
      $t_cnt++;
      }
      echo "</select>";
      echo " 表名前:<input type='text' name='new_name' size='12' value=''>";
    }
    /*-------------------------------------------------表のタテ・ヨコの数を表示*/
    echo " タテ:<select name='tate'>";
    if($text[2] != ""){
      echo "<option value='$text[2]'>$text[2]</option>";
    }
      $t_cnt = 1;
      while($t_cnt < 21){
        echo "<option value='$t_cnt'>$t_cnt</option>";
      $t_cnt++;
      }
    echo "</select>";

    echo " ヨコ:<select name='yoko'>";
    if($text[3] != ""){
      echo "<option value='$text[3]'>$text[3]</option>";
    }
      $y_cnt = 1;
      while($y_cnt <= 10){
        echo "<option value='$y_cnt'>$y_cnt</option>";
      $y_cnt++;
      }
    echo "</select>¥n";
    /*-------------------------------------------------「作成/編集」ボタンを編集中は表示しない*/
    if($text[1] == ""){
      echo "  <input type='submit' name='enter' value='作成/編集'>";
    }
    /*-------------------------------------------------データがない時は表示しない*/
    if($text[1] != ""){
    /*-------------------------------------------------記入用フォームの設置*/
      echo "<center><hr width= '680'>¥n¥n";
      echo "<table style='font-size:12px' width='680' border='0' cellspacing='0' cellpadding='0'>¥n";
    /*-------------------------------------------------入力フォームのサイズを決める*/
      $size = (138-5*$text[3])/$text[3];
    /*-------------------------------------------------テーブルに入力欄を入れていく*/
      $c = 1;    /*セルの数*/
      $a = 1;    /*タテの数*/
      while($a <= $text[2]){
        echo "<tr>¥n";
        $b = 1;    /*ヨコの数*/
        while($b <= $text[3]){
          echo "<td align='center'><input type='text' name='table$c' size='$size' value='$sel_text[$c]'></td>¥n";
        $b++;
        $c++;
        }
        echo "</tr>¥n";
      $a++;
      }
      echo "</table><br><br>¥n";
    /*-------------------------------------------------テーブルの横幅をピクセルで選ぶ*/
      echo " ヨコ幅:<select name='t_width'>";
      if($text[5] != ""){
        echo "<option value='$text[5]'>$text[5]</option>";
      }
      $w_cnt = 680;
      while($w_cnt >= 200){
        echo "<option value='$w_cnt'>$w_cnt</option>";
      $w_cnt = $w_cnt-20;
      }
      echo "</select>¥n";
    /*-------------------------------------------------中央を0として左右に移動する位置を指定する*/
      echo " 位置:<select name='left_mrgn'>";
      if($text[6] != ""){
        echo "<option value='$text[6]'>$text[6]</option>";
      }else{
        echo "<option value='0'>0</option>";
      }
      $mrgn = 680-$text[5];  /*680が最大横幅なので、指定した横幅から引いて余白部分(マージン)の長さを出す*/
      $r_cnt = 0-$mrgn;
      while($mrgn >= $r_cnt){
        echo "<option value='$mrgn'>$mrgn</option>";
      $mrgn = $mrgn-20;    /*マージンから20ずつ引いてマイナスまで作成*/
      }
      echo "</select>¥n";
    /*-------------------------------------------------枠線や色の指示*/
      echo " 枠線:<select name='line'>";
      if($text[7] != ""){
        echo "<option value='$text[7]'>$text[7]</option>";
      }
      echo "<option value='0'>0</option><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option>";
      echo "</select>¥n";
      echo " 枠線色:<input type='text' name='line_color' size='9' value='$text[8]'>";
      echo " 背景色:<input type='text' name='bg_color1' size='9' value='$text[9]'>";
    /*-------------------------------------------------*/
      echo "<hr width='680'>";
      echo "<table border='0' ><tr><td align='right'>";
      echo " 一行目背景色:<input type='text' name='bg_color_top' size='9' value='$text[11]'>";
      echo " 文字サイズ:<select name='top_size'>";
      if($text[13] != ""){
        echo "<option value='$text[13]'>$text[13]</option>";
      }
      echo "<option value='12'>12</option>";
      echo "<option value='13'>13</option>";
      echo "<option value='14'>14</option>";
      echo "<option value='15'>15</option>";
      echo "<option value='16'>16</option>";
      echo "<option value='17'>17</option>";
      echo "</select>¥n";
    /*-------------------------------------------------*/
      echo " 文字色:<input type='text' name='top_color' size='9' value='$text[14]'>";
      echo " 太字<input type='checkbox' name='top_bold' value='bold'";
      if($text[15] == "bold"){
        echo "checked";
      }
      echo "><br>";
    /*-------------------------------------------------*/
      echo " 一列目背景色:<input type='text' name='bg_color_left' size='9' value='$text[12]'>";
      echo " 文字サイズ:<select name='left_size'>";
      if($text[16] != ""){
        echo "<option value='$text[16]'>$text[16]</option>";
      }
      echo "<option value='12'>12</option>";
      echo "<option value='13'>13</option>";
      echo "<option value='14'>14</option>";
      echo "<option value='15'>15</option>";
      echo "<option value='16'>16</option>";
      echo "<option value='17'>17</option>";
      echo "</select>¥n";
      echo " 文字色:<input type='text' name='left_color' size='9' value='$text[17]'>";
      echo " 太字<input type='checkbox' name='left_bold' value='bold'";
    if($text[18] == "bold"){
        echo "checked";
      }
      echo "><br>";
    /*-------------------------------------------------*/
      echo " その他背景色:<input type='text' name='bg_color2' size='9' value='$text[10]'>";
      echo " 文字サイズ:<select name='other_size'>";
      if($text[19] != ""){
        echo "<option value='$text[19]'>$text[19]</option>";
      }
      echo "<option value='12'>12</option>";
      echo "<option value='13'>13</option>";
      echo "<option value='14'>14</option>";
      echo "<option value='15'>15</option>";
      echo "<option value='16'>16</option>";
      echo "<option value='17'>17</option>";
      echo "</select>¥n";
      echo " 文字色:<input type='text' name='other_color' size='9' value='$text[20]'>";
      echo " 太字<input type='checkbox' name='other_bold' value='bold'";
      if($text[21] == "bold"){
        echo "checked";
      }
      echo "></td><td valign='bottom'>";
      echo "<input type='hidden' name='t_name' value='$text[1]'>";
      echo "<input type='hidden' name='table_id' value='$text[0]'>";
      echo "<input type='hidden' name='table_num' value='$c'>";
    /*-------------------------------------------------*/
      echo "   <input type='submit' name='enter' value='保存'>";
      echo "       <input type='submit' name='enter' value='終了'></td></tr></table>";
    /*-------------------------------------------------列ごとの配置を選択する*/
      echo "</td></tr></table><br>";
      echo "<table width='700' border='1'><tr><td><center><br>";
      echo "<table width='$text[5]'><tr>";
    /*-------------------------------------------------*/
      $b = 1;
      while($b <= $text[3]){
        echo "<td align='center'><select name='align$b'>";
          if($algn_text[$b] != ""){
            echo "<option value='$algn_text[$b]'>$algn_text[$b]</option>";
          }
        echo "<option value='left'>left</option><option value='center'>center</option><option value='right'>right</option></td>¥n";
      $b++;
      }
      echo "<input type='hidden' name='algn_num' value='$text[3]'>";
      echo "</form>¥n¥n";
      echo "</tr></table><br>";
    /*-------------------------------------------------テーブルを表示*/
      echo "<table width='$text[5]' border='$text[7]' cellspacing='1' cellpadding='5' bgcolor='$text[9]' bordercolor='$text[8]' style='margin-left:$text[6];font-size:12px'>¥n";

      $c = 1;
      $a = 1;
      while($a <= $text[2]){
          if($a == 1){
            echo "<tr bgcolor='$text[11]' style='font-size:$text[13];color:$text[14];font-weight:$text[15];'>¥n";
          }else{
            echo "<tr bgcolor='$text[10]' style='font-size:$text[19];color:$text[20];font-weight:$text[21];'>¥n";
          }
        $b = 1;                        /*-最初のセルは1列1行目なので設定が複雑*/
        while($b <= $text[3]){
          if($a == 1 and $b == 1){

            echo "<td ";
            if($text[11] != ""){
              echo "bgcolor='$text[11]' ";
            }elseif($text[12] != ""){
              echo "bgcolor='$text[12]' ";
            }else{
              echo "bgcolor='$text[10]' ";
            }

            if($text[13] != ""){
              echo "style='font-size:$text[13];";
            }elseif($text[16] != ""){
              echo "style='font-size:$text[16];";
            }else{
              echo "style='font-size:$text[19];";
            }

            if($text[14] != ""){
              echo "color:$text[14];";
            }elseif($text[17] != ""){
              echo "color:$text[17];";
            }else{
              echo "color:$text[20];";
            }

            if($text[15] != ""){
              echo "font-weight:$text[15];'";
            }elseif($text[18] != ""){
              echo "font-weight:$text[18];'";
            }else{
              echo "font-weight:$text[21];'";
            }

            echo " align='$algn_text[$b]'>$sel_text[$c]</td>¥n";
    /*-------------------------------------------------1行目*/
          }elseif($a == 1){
            echo "<td ";
            if($text[11] != ""){
              echo "bgcolor='$text[11]' ";
            }else{
              echo "bgcolor='$text[10]' ";
            }

            if($text[13] != ""){
              echo "style='font-size:$text[13];";
            }else{
              echo "style='font-size:$text[19];";
            }

            if($text[14] != ""){
              echo "color:$text[14];";
            }else{
              echo "color:$text[20];";
            }

            if($text[15] != ""){
              echo "font-weight:$text[15];'";
            }else{
              echo "font-weight:$text[21];'";
            }
            echo " align='$algn_text[$b]'>$sel_text[$c]</td>¥n";
    /*-------------------------------------------------1列目*/
          }elseif($b == 1){
            echo "<td bgcolor='$text[12]' style='font-size:$text[16];color:$text[17];font-weight:$text[18];' align='$algn_text[$b]'>$sel_text[$c]</td>¥n";
          }else{
            echo "<td bgcolor='$text[10]' style='font-size:$text[19];color:$text[20];font-weight:$text[21];' align='$algn_text[$b]'>$sel_text[$c]</td>¥n";
          }
        $b++;
        $c++;
        }
        echo "</tr>¥n";
      $a++;
      }
      echo "</table></center>¥n¥n¥n";
    /*----------------------------------------テキストエリアにタグを表示、上の表示とまったく同じもの*/
    echo "<br><br><center><textarea name='data' cols='90' rows='15'>";
      echo "<center>";
      echo "<table width='$text[5]' border='$text[7]' cellspacing='1' cellpadding='5' bgcolor='$text[9]' bordercolor='$text[8]' style='margin-left:$text[6];font-size:12px'>¥n";

      $c = 1;
      $a = 1;
      while($a <= $text[2]){
          if($a == 1){
            echo "<tr bgcolor='$text[11]' style='font-size:$text[13];color:$text[14];font-weight:$text[15];'>¥n";
          }else{
            echo "<tr bgcolor='$text[10]' style='font-size:$text[19];color:$text[20];font-weight:$text[21];'>¥n";
          }
        $b = 1;
        while($b <= $text[3]){
          if($a == 1 and $b == 1){
            echo "<td ";
              if($text[11] != ""){
                echo "bgcolor='$text[11]' ";
              }elseif($text[12] != ""){
                echo "bgcolor='$text[12]' ";
              }else{
                echo "bgcolor='$text[10]' ";
              }
              if($text[13] != ""){
                echo "style='font-size:$text[13];";
              }elseif($text[16] != ""){
                echo "style='font-size:$text[16];";
              }else{
                echo "style='font-size:$text[19];";
              }
              if($text[14] != ""){
                echo "color:$text[14];";
              }elseif($text[17] != ""){
                echo "color:$text[17];";
              }else{
                echo "color:$text[20];";
              }
              if($text[15] != ""){
                echo "font-weight:$text[15];'";
              }elseif($text[18] != ""){
                echo "font-weight:$text[18];'";
              }else{
                echo "font-weight:$text[21];'";
              }
            echo " align='$algn_text[$b]'>$sel_text[$c]</td>¥n";
          }elseif($a == 1){
            echo "<td ";
              if($text[11] != ""){
                echo "bgcolor='$text[11]' ";
              }else{
                echo "bgcolor='$text[10]' ";
              }
              if($text[13] != ""){
                echo "style='font-size:$text[13];";
              }else{
                echo "style='font-size:$text[19];";
              }
              if($text[14] != ""){
                echo "color:$text[14];";
              }else{
                echo "color:$text[20];";
              }
              if($text[15] != ""){
                echo "font-weight:$text[15];'";
              }else{
                echo "font-weight:$text[21];'";
              }
            echo " align='$algn_text[$b]'>$sel_text[$c]</td>¥n";
          }elseif($b == 1){
            echo "<td bgcolor='$text[12]' style='font-size:$text[16];color:$text[17];font-weight:$text[18];' align='$algn_text[$b]'>$sel_text[$c]</td>¥n";
          }else{
            echo "<td bgcolor='$text[10]' style='font-size:$text[19];color:$text[20];font-weight:$text[21];' align='$algn_text[$b]'>$sel_text[$c]</td>¥n";
          }
        $b++;
        $c++;
        }
        echo "</tr>¥n";
      $a++;
      }
      echo "</table></center>¥n¥n¥n";
      echo "</textarea>";
    }
    echo "<br><br></td></tr></table></center>";


    ?>

    </div></body></html>
    2020/3
    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