LoginSignup
0
0

More than 3 years have passed since last update.

【PHP+SQLite3】恐竜登録ツール2

Last updated at Posted at 2020-01-02

前回の記事の内容を実装してみた。

共通処理のプログラム

commom_dino.php
<?php 
//
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_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_dino.php

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

<?php 
require 'common_dino.php';
$select_all = "select count(*) from tbl_dino";
$db_name='dino_collect.db';
$ext=file_exists($db_name);
//恐竜情報テーブル作成
$query_c="CREATE TABLE tbl_dino(id integer primary key autoincrement,dname0 text,type0 text,n_src1 integer,type1 text,n_src2 integer,type2 text)";
$ret0 = 0;
//dbが存在しない時は
if(!$ext){
    $ret0 = 0;
    $db1 = new PDO('sqlite:'.$db_name);
    $result_a=$db1->prepare($query_c);
    $result_a->execute();

}else{
    //データベースを開く
    $db1 = new PDO('sqlite:'.$db_name);
    $result_a=get_AllData($db1,$select_all);
    //$count
    $count = $result_a->fetchColumn();
    $ret0=$count;

}

 ?>
<!—- 開始ボタンと一緒に登録件数を送る ->
<input type="hidden" name="btn1" value="<?=$ret0?>">
<input type="submit" value="開始">
</form>
</html>

恐竜のタイプ(ハイブリッドか非ハイブリッド)を選択する画面

  • 登録件数が3体未満だと、非ハイブリッドの登録画面になる。
  • 3体以上の場合、非ハイブリッドかハイブリッドを選べる。
select_dinotype1.php

<!DOCTYPE html>
<html lang="ja">
<meta charset="utf-8">
<title>恐竜タイプ選択1</title>
<body>
<?php 
if(isset($_POST["btn1"])){
    $ret0=$_POST["btn1"];
    echo "<h1>恐竜タイプ選択1</h1>";
}
 ?>

<?php if($ret0<3){ ?>
<form action="single_dino1.php" method="post">
<input type="text" name="dname0">
カテゴリー:
<select name="dtype0">
<option>normal</option>
<option>rare</option>
<option>epic</option>
</select>
<input type="submit" value="確定" name="btn2">
</form>

<?php }else{ 

require "common_dino.php";
$db_name='dino_collect.db';
$db1 = new PDO('sqlite:'.$db_name);
$query_type0 = "SELECT DISTINCT type0 from tbl_dino";

?>
<form action="select_dinotype2.php" method="post">
カテゴリー:
<select name="l_category">
<option>ハイブリッド</option>
<option>非ハイブリッド</option>
</select><br>
生成元タイプ1:(ハイブリッド型は必須)
<?php $result_a=get_AllData($db1,$query_type0); ?>  
<select name="s_type1">
<?php while($select_data=$result_a->fetch(PDO::FETCH_ASSOC)){ ?>
<option><?php echo $select_data['type0']; ?></option>
<?php } ?>
</select><br>

生成元タイプ2:(ハイブリッド型は必須)
<?php $result_a=get_AllData($db1,$query_type0); ?>  
<select name="s_type2">
<?php while($select_data2=$result_a->fetch(PDO::FETCH_ASSOC)){ ?>
<option><?php echo $select_data2['type0']; ?></option>
<?php } ?>
</select><br>

<input type="submit" value="確定" name="btn2">
</form>

<?php   } ?>

<form action="start_dino.php" method="post">
<input type="submit" value="戻る" name="btn3">
</form>

</body>
</html>

恐竜の名前とタイプの決定画面(3体以上登録時)

ハイブリッド選択時と非ハイブリッド選択時で画面項目の数は
異なるが、最終的に生成する恐竜名とタイプを決定する部分は同じ。

select_dinotype2.php

<!DOCTYPE html>
<html lang="ja">
<meta charset="utf-8">
<title>恐竜タイプ選択2</title>
<h1>恐竜タイプ選択2</h1>

<?php 
$db_name='dino_collect.db';
$db1 = new PDO('sqlite:'.$db_name);

$type0=isset($_POST['l_category'])?htmlspecialchars($_POST['l_category']):null;;
$type1=isset($_POST['s_type1'])?htmlspecialchars($_POST['s_type1']):null;;
$type2=isset($_POST['s_type2'])?htmlspecialchars($_POST['s_type2']):null;;
if(strcmp($type0,"非ハイブリッド")==0){ ?>
<form action="single_dino1.php" method="post">
<input type="text" name="dname0">
カテゴリー:
<select name="dtype0">
<option>normal</option>
<option>rare</option>
<option>epic</option>
</select>

<input type="submit" value="確定" name="btn2">
</form>

<?php 
}else{

    $query_all="SELECT dname0,type0 FROM tbl_dino WHERE  type0 = ?";

function get_typedata($db0,$query0,$val1)
{
    $result_a=$db0->prepare($query0);
    $result_a->bindparam(1,$val1);
    $result_a->execute();
    return $result_a;
}   
 ?>
<form action="hibrid_dino1.php" method="post">
<input type="text" name="hdname0">

カテゴリー:
<select name="dtype0">
<option>rare</option>
<option>epic</option>
<option>regend</option>
<option>unique</option>
</select><br>
<?php $result_a=get_typedata($db1,$query_all,$type1); ?>
生成元1:
<select name="src1">
<?php while($select_data=$result_a->fetch(PDO::FETCH_ASSOC)){ ?>
<option><?php echo $select_data['dname0']; ?>:<?php echo $select_data['type0']; ?></option>
<?php } ?>
</select><br>

<?php $result_a=get_typedata($db1,$query_all,$type2); ?>
生成元2:
<select name="src2">
<?php while($select_data2=$result_a->fetch(PDO::FETCH_ASSOC)){ ?>
<option><?php echo $select_data2['dname0']; ?>:<?php echo $select_data2['type0']; ?></option>
<?php } ?>
</select>

<input type="submit" value="確定" name="btn2">
</form>

<?php }
    require 'common_dino.php';
    $select_all = "select count(*) from tbl_dino";
    $result_a=get_AllData($db1,$select_all);
    //$count
    $count = $result_a->fetchColumn();
    $ret0=$count;

 ?>
<form action="select_dinotype1.php" method="post">
<input type="hidden" name="btn1" value="<?=$ret0?>">
<input type="submit" value="戻る">
</form>

</html>

非ハイブリッド登録結果

single_dino1.php

<!DOCTYPE html>
<html lang="ja">
<meta charset="utf-8">
<title>恐竜シングル</title>
<h1>恐竜シングル結果</h1>

<?php 
require 'common_dino.php';
$db_name='dino_collect.db';
$db1 = new PDO('sqlite:'.$db_name);

$dtype1=isset($_POST['dtype0'])?htmlspecialchars($_POST['dtype0']):null;
$dname1=isset($_POST['dname0'])?htmlspecialchars($_POST['dname0']):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($db1,$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($db1,$query_select1,trim($dname2),$dtype1);
    if($select_data){
        echo "登録済みです";

    }else{
        $result1=$db1->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_dinotype1.php" method="post">
<input type="hidden" name="btn1" value="<?=$total_dino?>">
<input type="submit" value="戻る">
</form>
</html>

ハイブリッド恐竜の登録結果

hibrid_dino1.php

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

<?php 
require 'common_dino.php';
$db_name='dino_collect.db';
$db1 = new PDO('sqlite:'.$db_name);
$hdname1=isset($_POST['hdname0'])?htmlspecialchars($_POST['hdname0']):null;
$dtype1=isset($_POST['dtype0'])?htmlspecialchars($_POST['dtype0']):null;
$srctype1=isset($_POST['src1'])?htmlspecialchars($_POST['src1']):null;
$srctype2=isset($_POST['src2'])?htmlspecialchars($_POST['src2']):null;
$query_ins2="INSERT INTO tbl_dino(dname0,type0,n_src1,type1,n_src2,type2) VALUES(?,?,?,?,?,?)";

//登録件数
$select_all = "select count(*) from tbl_dino";
$result_a=get_AllData($db1,$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($srctype1,$srctype2)==0){
        echo "生成元1と生成元2は違う種類を選んでください。";
    }else{
    //すでに登録済みの恐竜か否かチェックする
        $select_data0=Checkdino($db1,$query_select1,trim($hdname2),$dtype1);

        if($select_data0){
            echo "登録済みです";
        }else{
            //生成元タイプ1と生成元恐竜1に分ける。
            $src1=get_BeforeMarkString($srctype1,":");
            $stype1=get_AfterMarkString($srctype1,":");
            //生成元タイプ2と生成元恐竜2に分ける。
            $src2=get_BeforeMarkString($srctype2,":");
            $stype2=get_AfterMarkString($srctype2,":");  
            $select_data1=Checkdino($db1,$query_select1,$src1,$stype1);
            //生成元1の恐竜番号を取得
            $num_src1=$select_data1['id'];
            $select_data2=Checkdino($db1,$query_select1,$src2,$stype2);
            //生成元2の恐竜番号を取得
            $num_src2=$select_data2['id'];
            //登録クエリ   
            $result1=$db1->prepare($query_ins2);

            //パラメータをセット
            $result1->bindparam(1,$hdname2);
            $result1->bindparam(2,$dtype1);
            $result1->bindparam(3,$num_src1);
            $result1->bindparam(4,$stype1);
            $result1->bindparam(5,$num_src2);
            $result1->bindparam(6,$stype2);
            $result1->execute();
            echo $hdname2.":".$dtype1."を登録しました";
        }
    }
}else{
    echo "名前が不正です";
}

 ?>
<form action="select_dinotype1.php" method="post">
<input type="hidden" name="btn1" value="<?=$total_dino?>">
<input type="submit" value="戻る">
</form>
</html>

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