menu


2017年4月28日金曜日

保存・再読み込み機能を付ける⑦読み込み処理を付ける

csv ファイルを読み込みます。
php の関数を使うと、正しく読み込みませんので全て作ります。
ソースは以下になります。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>新規作者編集</title>
<script>
function setMode(mode){
document.getElementById("mode").value = mode;
}

function addtcount(){
document.getElementById("tcount").value = parseInt(document.getElementById("tcount").value)+1;
}
</script>
</head>
<body>
<?php
function WriteCsv($fname,$tbl){
$file = fopen($fname,"w");
if( $file != FALSE ){
$tbl[0][3]="\"".$tbl[0][3]."\"";
for( $i=1;$i < count($tbl) ; $i++ ){
$tbl[$i][4]="\"".$tbl[$i][4]."\"";
}
for( $i=0;$i < count($tbl) ; $i++ ){
for( $j=0 ; $j < count($tbl[$i]) ; $j++ ){
fwrite( $file,$tbl[$i][$j]) ;
if( $j < (count($tbl[$i])-1) ){
fwrite($file,",");
}
else {
fwrite($file,"\n");
}
}
}
fclose($file);
}
return $file ;
}

function GetParam(){
$tb = array();
$tb[0][0] = $_POST["name"] ;
$tb[0][1] = $_POST["yomi"] ;
$tb[0][2] = $_POST["path_dir"] ;
$tb[0][3] = $_POST["info"] ;
for( $i = 0 ; $i < $tcount ; $i++ ){
$tb[$i+1][0]=$_POST["title_".$i];
$tb[$i+1][1]=$_POST["dir_link_".$i];
$tb[$i+1][2]=$_POST["Publishing_company_".$i];
$tb[$i+1][3]=$_POST["Journal_title_".$i];
$tb[$i+1][4]=$_POST["Comment_".$i];
}
return $tb ;
}

function getkazu($str,$c){
$b=explode($c,$str);
return count($b)-1;
}

function getcsv($handle){
$str="" ;
do{
$str = $str.fgets($handle);
$cun = getkazu($str,"\"") ;
}while( ($cun%2)!=0 );
$str = str_replace("\"", '', $str);
return explode(",",$str) ;
}

function ReadCsv($fname){
$records = array() ;
$fname = str_replace(array("\r\n", "\r", "\n"), '', $fname);
if (($handle = fopen($fname, "r")) !== false) {
while (!feof($handle)){
$line = getcsv($handle) ;
if( count($line) == 1 && $line[0] == "" ){
}
else {
       $records[] = $line;
}
 } 
 fclose($handle); 

return $records ;
}

$tcount = 1 ;
$tb = array();
$err = 0;
if( isset($_POST["mode"])){
if( $_POST["mode"] == "1" ){
print "<div>作者編集(保存)</div>";
$tb = GetParam();
if( $tb[0][2]!="" ){
if( WriteCsv("../autho/".$tb[0][2],$tb) == FALSE ){
$err = 2 ;
}
}
else {
$err = 1 ;
}
}
else if( $_POST["mode"]=="2"){
print "<div>作者編集(再読み込み)</div>";
if( $tb[0][2]!="" ){
$tb = ReadCsv($tb[0][2]);
if( count($tb) < 2 ){
$err = 4 ;
$tb = GetParam();
}
}
else {
$err = 3 ;
$tb = GetParam();
}
}
else {
print "<div>作者編集(タイトル追加)</div>";
$tcount = $_POST["tcount"] ;
if( $tb[0][2]!="" ){
WriteCsv("../autho/".$tb[0][2],$tb);
}
else {
$tb = GetParam();
}
}
}
else {
print "<div>新規作者編集</div>";
$tb[0]=array("","","","");
$tb[1]=array("","","","","");
}
print "<br>";
print "<input type=\"button\" onclick=\"window.location.href='./index.php';\" value=\"メニューに帰る\" /><br>";
print "<form method=\"POST\" action=\"newautho.php\" name=\"main_form\">";
print "<div>";
print "<div style=\"border: solid 1px #202020;margin: 5px 5px 3px;\">";
print "<div style=\"border: solid 0px #202020;margin: 5px 5px 5px 5px;\">";
print "<input id=\"mode\" type=\"hidden\" name=\"mode\" />";
print "<input id=\"tcount\" type=\"hidden\" name=\"tcount\"value=".$tcount." />";
print "著者名:<input id=\"name\" type=\"text\" name=\"name\" size=\"50\" value=\"".$tb[0][0]."\" /><br>";
print "  読み:<input id=\"yomi\" type=\"text\" name=\"yomi\" size=\"50\" value=\"".$tb[0][1]."\" /><br>";
print "ファイル名:<br>";
print "<input id=\"path_dir\" type=\"text\" name=\"path_dir\" size=\"50\" value=\"".$tb[0][2]."\" />";
if( $err != 0 ){
if( $err==1 ){
print "<font color=\"#ff0000\">ファイル名が指定されてません!</font>";
}
else if( $err == 2 ){
print "<font color=\"#ff0000\">保存ファイルが作成できませんでした!</font>";
}
else if( $err == 3 ){
print "<font color=\"#ff0000\">読み込みファイル名が指定されてません!</font>";
}
else if( $err == 4 ){
print "<font color=\"#ff0000\">読み込みファイルが異常で読み込めません!</font>";
}
}
print "<br>";
print "追記事項:<br>";
print "<textarea id=\"info\" name=\"info\" rows=\"4\" cols=\"50\">".$tb[0][3]."</textarea><br>";
print "</div>";
print "<input id=\"renewal\" type=\"submit\" name=\"renewal\" onclick=\"setMode(1);\" value= \"保存\" />";
print "<input id=\"renewal\" type=\"submit\" name=\"renewal\" onclick=\"setMode(2);\" value= \"際読み込み\" />";
print "</div>";
print "</div>";
print "<div style=\"border: solid 1px #202020;margin: 5px 5px 3px;\">";
print "作品リスト<br>";
for( $i = 0 ; $i < $tcount ; $i++ ){
print "<div style=\"border: solid 1px #202020;margin: 5px 5px 5px 5px;\">";
print "<div style=\"border: solid 0px #202020;margin: 5px 5px 5px 5px;\">";
print "タイトル<input id=\"title_".$i."\" type=\"text\" name=\"title_".$i."\" size=\"50\" value=\"".$tb[$i+1][0]."\" /><br>";
print "リンク<input id=\"dir_link_".$i."\" type=\"text\" name=\"dir_link_".$i."\" size=\"50\" value=\"".$tb[$i+1][1]."\" /><input id=\"Search\" type=\"button\" name=\"Search\" value= \"検索\" /><br>";
print "出版社<input id=\"Publishing_company_".$i."\" type=\"text\" name=\"Publishing_company_".$i."\" size=\"50\" value=\"".$tb[$i+1][2]."\" /><br>";
print "雑誌名<input id=\"Journal_title_".$i."\" type=\"text\" name=\"Journal_title_".$i."\" size=\"50\" value=\"".$tb[$i+1][3]."\" /><br>";
print "コメント<br>";
print "<textarea id=\"Comment_".$i."\" name=\"Comment_".$i."\" rows=\"4\" cols=\"50\">".$tb[$i+1][4]."</textarea><br>";
print "</div>";
print "</div>";
}
print "<input id=\"add\" type=\"submit\" name=\"add\" onclick=\"setMode(31);addtcount();\" value= \"追加\" />";
print "</div>";

print "</div>";
print "</form>";

?>
</body>
</html>
共通処理なので関数にしました。
function GetParam(){
$tb = array();
$tb[0][0] = $_POST["name"] ;
$tb[0][1] = $_POST["yomi"] ;
$tb[0][2] = $_POST["path_dir"] ;
$tb[0][3] = $_POST["info"] ;
for( $i = 0 ; $i < $tcount ; $i++ ){
$tb[$i+1][0]=$_POST["title_".$i];
$tb[$i+1][1]=$_POST["dir_link_".$i];
$tb[$i+1][2]=$_POST["Publishing_company_".$i];
$tb[$i+1][3]=$_POST["Journal_title_".$i];
$tb[$i+1][4]=$_POST["Comment_".$i];
}
return $tb ;
}
csv 読み込み処理
この処理は、文字列中の特定の文字数を返します
csv 処理では、「"」の数を取得するのにつかってます。
function getkazu($str,$c){
$b=explode($c,$str);
return count($b)-1;
}
この処理は、1行分のcsv データを取得します。1行にある「"」の数が奇数の場合は、偶数になるまでファイルを取得します。(改行のある文字列が終わるのを「"」の数で判断します
function getcsv($handle){
$str="" ;
do{
$str = $str.fgets($handle);
$cun = getkazu($str,"\"") ;
}while( ($cun%2)!=0 );
$str = str_replace("\"", '', $str);
return explode(",",$str) ;
}
指定ファイルを開いてcsv データを読み込みます。
function ReadCsv($fname){
$records = array() ;
$fname = str_replace(array("\r\n", "\r", "\n"), '', $fname);
if (($handle = fopen($fname, "r")) !== false) {
while (!feof($handle)){
$line = getcsv($handle) ;
if( count($line) == 1 && $line[0] == "" ){
}
else {
        $records[] = $line;
}
  } 
  fclose($handle); 

return $records ;
}
引数の異常を確認して、引数を配列に設定します
if( $tb[0][2]!="" ){
$tb = ReadCsv($tb[0][2]);
if( count($tb) < 2 ){
$err = 4 ;
$tb = GetParam();
}
}
else {
$err = 3 ;
$tb = GetParam();
}
異常があったら異常に合わせてメッセージを出力します
if( $err != 0 ){
if( $err==1 ){
print "<font color=\"#ff0000\">ファイル名が指定されてません!</font>";
}
else if( $err == 2 ){
print "<font color=\"#ff0000\">保存ファイルが作成できませんでした!</font>";
}
else if( $err == 3 ){
print "<font color=\"#ff0000\">読み込みファイル名が指定されてません!</font>";
}
else if( $err == 4 ){
print "<font color=\"#ff0000\">読み込みファイルが異常で読み込めません!</font>";
}
}
大分形になりました。
今までてを付けないでいた処理が残っています。コミック情報のリンクの入力処理です。
このリンクは、一個上のcsv フォルダーにフォルダーとして作成します。
いちいちフォルダー名なんか覚えて無いですよね!
そこで、一覧を作り、使う人は、選択するだけで良いようにします。
次は、フォルダー名一覧作成と選択処理を作ります。

0 件のコメント:

コメントを投稿