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;
-------------------------------------------------------------------------
multi_select1
multi_select1
http://localhost/index.html
目的:enq1から複数の項目を選択する
mode:multi_select1;table:enq1;Perl:
multi_select1
-------------------------------------------------------------------------
# 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;
-------------------------------------------------------------------------
multi_select2
multi_select2
http://localhost/index.html
http://localhost/scripts/multi_select1.pl
mode:multi_select2;table:enq1;Perl:
enq1id==>
type1id==>
partsid==>
-------------------------------------------------------------------------
# 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;
-------------------------------------------------------------------------
multi_select3
multi_select3
http://localhost/index.html
http://localhost/scripts/multi_select1.pl
mode:multi_select3;table:enq1;Perl:
enq1id==>
type1id==>
partsid==>
NO1==>
-------------------------------------------------------------------------
# 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;
-------------------------------------------------------------------------
multi_select4
multi_select4
http://localhost/index.html
http://localhost/scripts/multi_select1.pl
mode:multi_select4;table:enq1;Perl:
==>
-------------------------------------------------------------------------
戻る