select multipleの扱い

戻る

# multi_select1.pl use strict; use DBI; use CGI qw/:standard/; use HTML::Template; my(%t,@rec,@loop); # データベースに連結 $t{dsn} = "DBI:mysql:host=localhost;database=cookbook"; $t{dbh} = DBI->connect($t{dsn}, "cbuser", "cbpass") or die "Cannot connect to server\n"; $t{dbh}->do("SET NAMES utf8"); if(!$t{dbh}){ print "SQL read ERROR!\n"; exit; } # enq1テーブルのidを読む $t{sth} = $t{dbh}->prepare("select id from enq1"); $t{sth}->execute; @loop = (); while (@rec = $t{sth}->fetchrow_array) { my %row = ( id => $rec[0] ); # put this row into the loop by reference push(@loop, \%row); } $t{template} = HTML::Template->new(filename => 'multi_select1.htm'); $t{template}->param(pro => "multi_select1.pl"); $t{template}->param(LOOP => \@loop); $t{dbh}->disconnect; print $t{template}->output; 1; ------------------------------------------------------------------------- <HTML> <HEAD> <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> <TITLE>multi_select1</TITLE> <style type="text/css"> body { background: lightcyan; color:black; margin-left:2em;margin-right:2em;} </style> </HEAD> <BODY bgcolor="#FFFFFF"> <center><h2>multi_select1</h2></center> <a href="http://localhost/index.html">http://localhost/index.html</a><br> <hr color="#003366"> 目的:enq1から複数の項目を選択する<br> mode:multi_select1;table:enq1;Perl:<TMPL_VAR NAME="pro"> <hr color="#003366"> <h3>multi_select1</h3> <hr color="#003366"> <form method="POST" action="multi_select2.pl"> enq1id <SELECT NAME="enq1id"> <TMPL_LOOP NAME="LOOP"> <OPTION><TMPL_VAR NAME="id"> </TMPL_LOOP> </SELECT> <input type="submit" value="選択"> </form> </BODY> </HTML> ------------------------------------------------------------------------- # multi_select2.pl use strict; use DBI; use CGI qw/:standard/; use HTML::Template; my(%t,@rec,@loop,$n); $t{q} = new CGI; $t{enq1id} = $t{q}->param("enq1id"); # データベースに連結 $t{dsn} = "DBI:mysql:host=localhost;database=cookbook"; $t{dbh} = DBI->connect($t{dsn}, "cbuser", "cbpass") or die "Cannot connect to server\n"; $t{dbh}->do("SET NAMES utf8"); if(!$t{dbh}){ print "SQL read ERROR!\n"; exit; } # enq1テーブルを読む ($t{type1id},$t{partsid}) = $t{dbh}->selectrow_array("select type1id, partsid from enq1 where id = $t{enq1id}"); # データ @{ $t{type1id_list} } = split(/==/,$t{type1id}); $t{NO1} = 0; for $n ( 0 .. $#{ $t{type1id_list} } ) { $t{type1id1} = $t{type1id_list}[$n]; $t{NO1}++; my %row = ( NO1 => $t{NO1}, type1id => $t{type1id1} ); # put this row into the loop by reference push(@loop, \%row); } $t{template} = HTML::Template->new(filename => 'multi_select2.htm'); $t{template}->param(pro => "multi_select2.pl"); $t{template}->param(enq1id => $t{enq1id}); $t{template}->param(type1id => $t{type1id}); $t{template}->param(partsid => $t{partsid}); $t{template}->param(LOOP => \@loop); $t{dbh}->disconnect; print $t{template}->output; 1; ------------------------------------------------------------------------- <HTML> <HEAD> <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> <TITLE>multi_select2</TITLE> <style type="text/css"> body { background: lightcyan; color:black; margin-left:2em;margin-right:2em;} </style> </HEAD> <BODY bgcolor="#FFFFFF"> <center><h2>multi_select2</h2></center> <a href="http://localhost/index.html">http://localhost/index.html</a><br> <a href="http://localhost/scripts/multi_select1.pl">http://localhost/scripts/multi_select1.pl</a><br> <hr color="#003366"> mode:multi_select2;table:enq1;Perl:<TMPL_VAR NAME="pro"> <hr color="#003366"> enq1id==><TMPL_VAR NAME="enq1id"><br> type1id==><TMPL_VAR NAME="type1id"><br> partsid==><TMPL_VAR NAME="partsid"><br> <form method="POST" action="multi_select3.pl"> <input type="hidden" name="enq1id" value="<TMPL_VAR NAME="enq1id">"> <input type="hidden" name="type1id" value="<TMPL_VAR NAME="type1id">"> <input type="hidden" name="partsid" value="<TMPL_VAR NAME="partsid">"> enq1id <SELECT NAME="NO1"> <TMPL_LOOP NAME="LOOP"> <OPTION VALUE="<TMPL_VAR NAME="NO1">"><TMPL_VAR NAME="NO1">==><TMPL_VAR NAME="type1id"></OPTION> </TMPL_LOOP> </SELECT> <input type="submit" value="選択"> </form> </BODY> </HTML> ------------------------------------------------------------------------- # multi_select3.pl use strict; use CGI qw/:standard/; use HTML::Template; my(%t,@rec,@loop,$n,$n1); $t{q} = new CGI; $t{enq1id} = $t{q}->param("enq1id"); $t{type1id} = $t{q}->param("type1id"); $t{partsid} = $t{q}->param("partsid"); $t{NO1} = $t{q}->param("NO1"); # パーツデータ処理 @{ $t{partsid_list} } = split(/==/,$t{partsid}); for $n ( 0 .. $#{ $t{partsid_list} } ) { $t{partsid1} = $t{partsid_list}[$n]; $t{N1} = $n+1; if ( $t{N1} == $t{NO1} ) { @{ $t{partsid1_list} } = split(/=/,$t{partsid1}); $t{N} = 0; for $n1 ( 0 .. $#{ $t{partsid1_list} } ) { $t{partsid11} = $t{partsid1_list}[$n1]; $t{N}++; my %row = ( NO => $t{N}, partsid => $t{partsid11} ); push(@loop, \%row); } } } $t{template} = HTML::Template->new(filename => 'multi_select3.htm'); $t{template}->param(pro => "multi_select3.pl"); $t{template}->param(enq1id => $t{enq1id}); $t{template}->param(type1id => $t{type1id}); $t{template}->param(partsid => $t{partsid}); $t{template}->param(NO1 => $t{NO1}); $t{template}->param(LOOP => \@loop); print $t{template}->output; 1; ------------------------------------------------------------------------- <HTML> <HEAD> <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> <TITLE>multi_select3</TITLE> <style type="text/css"> body { background: lightcyan; color:black; margin-left:2em;margin-right:2em;} </style> </HEAD> <BODY bgcolor="#FFFFFF"> <center><h2>multi_select3</h2></center> <a href="http://localhost/index.html">http://localhost/index.html</a><br> <a href="http://localhost/scripts/multi_select1.pl">http://localhost/scripts/multi_select1.pl</a><br> <hr color="#003366"> mode:multi_select3;table:enq1;Perl:<TMPL_VAR NAME="pro"> <hr color="#003366"> enq1id==><TMPL_VAR NAME="enq1id"><br> type1id==><TMPL_VAR NAME="type1id"><br> partsid==><TMPL_VAR NAME="partsid"><br> NO1==><TMPL_VAR NAME="NO1"><br> <form method="POST" action="multi_select4.pl"> <SELECT SIZE="3" MULTIPLE NAME="parts"> <TMPL_LOOP NAME="LOOP"> <OPTION VALUE="<TMPL_VAR NAME="partsid">"><TMPL_VAR NAME="NO">==><TMPL_VAR NAME="partsid"></OPTION> </TMPL_LOOP> </SELECT> <input type="submit" value="選択"> </form> </BODY> </HTML> ------------------------------------------------------------------------- # multi_select4.pl use strict; use CGI qw/:standard/; use HTML::Template; my(%t,@rec,@loop,$n,$n1); $t{q} = new CGI; @{ $t{parts} } = $t{q}->param("parts"); # 複数選択した場合,自動に配列に入る $t{NO}=0; for $n ( 0 .. $#{ $t{parts} } ) { $t{parts1} = $t{parts}[$n]; $t{NO}++; my %row = ( NO => $t{NO}, parts => $t{parts1} ); push(@loop, \%row); } $t{template} = HTML::Template->new(filename => 'multi_select4.htm'); $t{template}->param(pro => "multi_select4.pl"); $t{template}->param(LOOP => \@loop); print $t{template}->output; 1; ------------------------------------------------------------------------- <HTML> <HEAD> <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> <TITLE>multi_select4</TITLE> <style type="text/css"> body { background: lightcyan; color:black; margin-left:2em;margin-right:2em;} </style> </HEAD> <BODY bgcolor="#FFFFFF"> <center><h2>multi_select4</h2></center> <a href="http://localhost/index.html">http://localhost/index.html</a><br> <a href="http://localhost/scripts/multi_select1.pl">http://localhost/scripts/multi_select1.pl</a><br> <hr color="#003366"> mode:multi_select4;table:enq1;Perl:<TMPL_VAR NAME="pro"> <hr color="#003366"> <TMPL_LOOP NAME="LOOP"> <TMPL_VAR NAME="NO">==><TMPL_VAR NAME="parts"><br> </TMPL_LOOP> </BODY> </HTML> -------------------------------------------------------------------------
戻る