0
0

More than 3 years have passed since last update.

【PHP+MySQL】改良恐竜登録ツール(PHP7.2.4+MySQL版)

Posted at

1.MySQL+PHPにおける恐竜登録ツール

画面例:開始画面(一定数以上、登録するとハイブリッドか非ハイブリッドを選べる)

start_dino_mysql.jpg

画面例:非ハイブリッド登録画面

select_dino_mysql1.jpg

画面例:ハイブリッド登録画面

SQLite3版との相違点は、生成元タイプを確定すると、そのタイプに属した恐竜名を指定することができる。
前回は生成元タイプ指定画面+生成元恐竜指定画面が別画面だった。

select_dino_mysql2.jpg

なおCSSは適用していない。

2.コード

共通関数

common_dino2.php
<?php 

  function NewPdo($dbname)
  {
    $dsn = 'mysql:dbname='.$dbname.';host=localhost:3308;charset=utf8mb4';
    $user = 'root';
    $password = 'root123';
    $dbh = new PDO($dsn, $user, $password);
    return $dbh;
  }
  function Checkdino($db0,$qry0,$val1,$type1)
  {
      $flg=0;
      $result3=$db0->prepare($qry0);
      //パラメータをセット
      $result3->bindparam(1,$val1);
      $result3->bindparam(2,$type1);
      $result3->execute();
      //検索結果を配列に格納する
      $select_data=$result3->fetch();
      return $select_data;
  }
  //クエリ実行して得られる全データ(パラメータ)
  function get_SelectTypeData($db0,$query0,$val1)
  {
      $result_a=$db0->prepare($query0);
      $result_a->bindparam(1,$val1);
      $result_a->execute();
    return $result_a;
  }
  //クエリ実行して得られる全データ
  function get_AllData($db0,$query0)
  {
      $result_a=$db0->prepare($query0);
      $result_a->execute();
    return $result_a;
  }
  //$str1:検索対象文字列,$str_mark:探したい文字: 戻り値、探したい文字より前にある文字列
  function get_BeforeMarkString($str1,$str_mark)
  {
    $idx0=strpos($str1, $str_mark);
    $retstr=substr($str1,0,$idx0);
    return $retstr;
  }

  //$str1:検索対象文字列,$str_mark:探したい文字、探したい文字より後にある文字列
  function get_AfterMarkString($str1,$str_mark)
  {
    $idx0=strpos($str1, $str_mark);
    $retstr=substr($str1,$idx0+1);
    return $retstr;
  }

 ?>  

開始画面

start_dino2_mysql.php
<!DOCTYPE html>
<html lang="ja">
<meta charset="utf-8">
<title>恐竜登録にようこそ</title>
<h1>開始</h1>

<?php 
  require 'common_dino2.php';
  $select_all = "select count(*) from tbl_dino";

    $dbname="db_dino";
  $dbh= NewPdo($dbname);

  $ret0 = 0;
  $result_a=get_AllData($dbh,$select_all);
     //$count
  $count = $result_a->fetchColumn();
  $ret0=$count;

 ?>

<?php 
if($ret0 > 3){
 ?>    
  <form action="select_dinotype3_mysql.php" method="post">
カテゴリー:
  <select name="l_category">
    <option>ハイブリッド</option>
    <option>非ハイブリッド</option>
  </select><br>
  <input type="submit" value="開始" name="btn2">
  </form>
<?php 
}else{
 ?>

  <form action="single_dino2_mysql.php" method="post">
  <input type="text" name="dname">
カテゴリー:
  <select name="dtype0">
    <option>normal</option>
    <option>rare</option>
    <option>epic</option>
  </select>
  <input type="submit" value="確定" name="btn2">
  </form>

<?php 
} ?>

</html>

恐竜タイプ選択画面

select_dinotype3_mysql.php
<!DOCTYPE html>
<html lang="ja">
<meta charset="utf-8">
<title>恐竜タイプ選択1</title>
<body>
<?php 
$HNtype0=isset($_POST['l_category'])?htmlspecialchars($_POST['l_category']):null;;
if(strcmp($HNtype0,"非ハイブリッド")==0){
echo "<h1>非ハイブリッド選択</h1>";

 ?>

  <form action="single_dino2_mysql.php" method="post">
  <input type="text" name="dname">
カテゴリー:
  <select name="dtype0">
    <option>normal</option>
    <option>rare</option>
    <option>epic</option>
  </select>
  <input type="submit" value="確定" name="btn2">
  </form>

<?php 
}else{
  echo "<h1>ハイブリッド選択</h1>";

  require "common_dino2.php";

    $dbname="db_dino";
  $dbh= NewPdo($dbname);

  $query_type0 = "SELECT DISTINCT type0 from tbl_dino";
 ?>
  <form action="select_dinotype3_mysql.php" method="post">
生成元タイプ1:
  <?php $result_a=get_AllData($dbh,$query_type0);
  $result_b=get_AllData($dbh,$query_type0);
  $stype1=isset($_POST['dtype01'])?htmlspecialchars($_POST['dtype01']):null;
  $stype2=isset($_POST['dtype02'])?htmlspecialchars($_POST['dtype02']):null;

   ?> 
  <select name="dtype01" style="width: 100px">
    <?php while($select_data=$result_a->fetch(PDO::FETCH_ASSOC)){
      //$stype1が$select_data['type0']と等しい場合
      if($stype1 == $select_data['type0']){ 
        echo "<option selected>".$select_data['type0']."</option>";
      //$stype1が$select_data['type0']と等しくない場合
      }else{
        echo "<option>".$select_data['type0']."</option>";
      }
    }  
    ?>
  </select><br>

生成元タイプ2:
  <select name="dtype02" style="width: 100px">
    <?php while($select_data2=$result_b->fetch(PDO::FETCH_ASSOC)){
      //$stype2が$select_data2['type0']と等しい場合
      if($stype2 == $select_data2['type0']){ 
        echo "<option selected>".$select_data2['type0']."</option>";
      //$stype2が$select_data2['type0']と等しくない場合
      }else{
        echo "<option>".$select_data2['type0']."</option>";
      }
    }
     ?>
  </select><br>
  <input type="submit" value="タイプ確定">
  </form>

<?php 


    $dbname="db_dino";
  $dbh= NewPdo($dbname);

  $query_select_type0 = "SELECT dname0 from tbl_dino where type0 = ?";
  $result1=get_SelectTypeData($dbh,$query_select_type0,$stype1);
  $result2=get_SelectTypeData($dbh,$query_select_type0,$stype2);  

 ?>

  <form action="hibrid_dino2_mysql.php" method="post">

生成元恐竜1:
  <select name="dname1" style="width: 280px">
    <?php while($select_data=$result1->fetch(PDO::FETCH_ASSOC)){
      echo "<option>".$select_data['dname0']."</option>";
    }
    ?>
  </select><br>

生成元恐竜2:
  <select name="dname2" style="width: 280px">
    <?php while($select_data2=$result2->fetch(PDO::FETCH_ASSOC)){
      echo "<option>".$select_data2['dname0']."</option>";
    }
     ?>
  </select><br>
恐竜名:
  <input type="text" name="hdname0">
製造カテゴリー:
  <select name="hdtype0">
    <option>rare</option>
    <option>epic</option>
    <option>regend</option>
    <option>unique</option>
  </select><br>
  <input type="hidden" name="btn2" value="<?=$HNtype0.":".$stype1.",".$stype2?>">
  <input type="submit" value="確定">
  </form>

<?php 
} ?>
<br>
<form action="start_dino2_mysql.php" method="post">
<input type="submit" value="戻る" name="btn3">
</form>

</body>
</html>

非ハイブリッド登録結果

single_dino2_mysql.php

<!DOCTYPE html>
<html lang="ja">
<meta charset="utf-8">
<title>非ハイブリッド恐竜</title>
<h1>非ハイブリッド恐竜登録結果</h1>

<?php 
  require 'common_dino2.php';

    $dbname="db_dino";
  $dbh= NewPdo($dbname);

  $dtype1=isset($_POST['dtype0'])?htmlspecialchars($_POST['dtype0']):null;
  $dname1=isset($_POST['dname'])?htmlspecialchars($_POST['dname']):null;
  $query_ins1="INSERT INTO tbl_dino(dname0,type0,n_src1,n_src2) VALUES(?,?,?,?)";
  $select_all = "select count(*) from tbl_dino";
  $result_a=get_AllData($dbh,$select_all);
    //登録件数
  $count = $result_a->fetchColumn();

  $total_dino=$count;


  //echo $dname1.":".$dtype1;
  $query_select1="SELECT * FROM tbl_dino WHERE dname0 = ? AND type0 = ?";
  //検索文字列、置換文字列,検索対象文字列の順番
  $dname2=trim(str_replace(" "," ",$dname1));
  if(isset($dname1) && empty($dname2) == false){
    //すでに登録済みの恐竜か否かチェックする
    $select_data=Checkdino($dbh,$query_select1,trim($dname2),$dtype1);
    if($select_data){
      echo "登録済みです";
    }else{
        $result1=$dbh->prepare($query_ins1);
        //パラメータをセット
        $result1->bindparam(1,$dname2);
        $result1->bindparam(2,$dtype1);
      //合成なしの場合、合成元恐竜がいないため
        $num0=0;
        $result1->bindparam(3,$num0);
        $result1->bindparam(4,$num0);
        $result1->execute();
        echo trim($dname2).":".$dtype1."の登録が完了しました";
    }
  }else{
    echo "値が不正です";
  }
 ?>
<form action="select_dinotype3_mysql.php" method="post">
<input type="hidden" name="l_category" value="非ハイブリッド">
<input type="submit" value="戻る">
<br><br>

</form>
</html>

ハイブリッド登録結果

hibrid_dino2_mysql.php

<!DOCTYPE html>
<html lang="ja">
<meta charset="utf-8">
<title>ハイブリッド恐竜登録結果</title>
<h1>ハイブリッド恐竜登録結果</h1>

<?php 
  require 'common_dino2.php';

    $dbname="db_dino";
  $dbh= NewPdo($dbname);

  $HNsrctype_str=isset($_POST['btn2'])?htmlspecialchars($_POST['btn2']):null;

  //生成恐竜と生成タイプ
  $hdname1=isset($_POST['hdname0'])?htmlspecialchars($_POST['hdname0']):null;
  $hdtype1=isset($_POST['hdtype0'])?htmlspecialchars($_POST['hdtype0']):null;
  //生成元1と生成元2
  $src_dname1=isset($_POST['dname1'])?htmlspecialchars($_POST['dname1']):null;
  $src_dname2=isset($_POST['dname2'])?htmlspecialchars($_POST['dname2']):null;
  $query_ins2="INSERT INTO tbl_dino(dname0,type0,n_src1,type1,n_src2,type2) VALUES(?,?,?,?,?,?)";

  if(isset($HNsrctype_str) && empty($HNsrctype_str) == false){
    $HNtype0=get_BeforeMarkString($HNsrctype_str,":");
    $src_type=get_AfterMarkString($HNsrctype_str,":");
    $stype1=get_BeforeMarkString($src_type,",");
    $stype2=get_AfterMarkString($src_type,",");
  }

  //登録件数
  $select_all = "select count(*) from tbl_dino";
  $result_a=get_AllData($dbh,$select_all);
    //登録件数
  $count = $result_a->fetchColumn();
  $total_dino=$count;

  $query_select1="SELECT * FROM tbl_dino WHERE dname0 = ? and type0 = ?";
  //全角の空白を半角スペースに変換 
  $hdname2=trim(str_replace(" "," ",$hdname1));

  if(isset($hdname1) && empty($hdname2) == false){
    if(strcmp($src_dname1,$src_dname2)==0 && strcmp($stype1,$stype2)==0){
      echo "生成元1と生成元2は違う種類を選んでください。";
    }else{
      //すでに登録済みの恐竜か否かチェックする
      $select_data0=Checkdino($dbh,$query_select1,trim($hdname2),$hdtype1);
      if($select_data0){
        echo "登録済みです";
      }else{

        $select_data1=Checkdino($dbh,$query_select1,$src_dname1,$stype1);
        //生成元1の恐竜番号を取得
        $num_src1=$select_data1['id'];
        $select_data2=Checkdino($dbh,$query_select1,$src_dname2,$stype2);
        //生成元2の恐竜番号を取得
        $num_src2=$select_data2['id'];
        //登録クエリ        
          $result1=$dbh->prepare($query_ins2);
          //パラメータをセット
          $result1->bindparam(1,$hdname2);
          $result1->bindparam(2,$hdtype1);
          $result1->bindparam(3,$num_src1);
          $result1->bindparam(4,$stype1);
          $result1->bindparam(5,$num_src2);
          $result1->bindparam(6,$stype2);
          $result1->execute();
        echo $hdname2.":".$hdtype1."を登録しました";
      }
    }
  }else{
    echo "名前が不正です";
  }
 ?>
<form action="select_dinotype3_mysql.php" method="post">
<input type="hidden" name="l_category" value="<?=$HNtype0?>">
<input type="submit" value="戻る">
</form>

<br>
<form action="start_dino2_mysql.php" method="post">
<input type="hidden" name="btn1" value="<?=$total_dino?>">
<input type="submit" value="開始画面に戻る">

</html>

3.今後の目標

やりたいことを挙げてみた。
業務で関わらないプログラミング言語にトライするには、
興味のある素材があることが一番。
(todoや掲示板、日記も魅力的だと思うけどコピペマンで終わりそう)

  • 恐竜といえば、画像!!!画像登録プログラム。
  • 恐竜登録ページング
  • 恐竜登録CRD
  • 別のプログラミング言語での恐竜登録 --->ここは結構気になっている。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0