- foreach $index (keys %membership_data)
- {
- $hashref = $membership_data{$index};
- foreach $key (keys %$hashref) {
- if($key =~ /HH Position/i && $membership_data{$index}{$key} =~ /Head of Household/i) {
- $family_name = $membership_data{$index}{'Preferred Name'};
- $family_name =~ s/\'/\\'/g; #'
- $id = $membership_data{$index}{'HofH ID'};
- $name_id = uc($family_name);
-
- # Find out how many families match this family's name
- $sth = $dbh->prepare("select * from tc_family where name_id='$name_id'");
- $sth->execute or die "-E- DB error: $DBI::errstr\n";
- my @data = ();
- while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
- my $rows = scalar @data;
-
- if($rows == 0) {
- # No existing records found for this family, make a new entry
- print " Adding new Family: $family_name\n";
- $sth = $dbh->prepare("insert into tc_family values (NULL,$id,'$family_name','$name_id','0','0',NULL,1)");
- $sth->execute or die "-E- DB error: $DBI::errstr\n";
- } elsif($rows == 1) {
- # An existing record was found for this family, update it
- print " Updating existing family: $family_name\n";
- $sth = $dbh->prepare("update tc_family set hofh_id=$id where name_id='$name_id'");
- $sth->execute or die "-E- DB error: $DBI::errstr\n";
- $sth = $dbh->prepare("update tc_family set valid=1 where name_id='$name_id'");
- $sth->execute or die "-E- DB error: $DBI::errstr\n";
- } else {
- # More than one record was found. Error! This shouldn't happen.
- print " -E- More than one record found ($rows) for family name: $family_name\n";
- }
+ # find head of households in tc_individual
+ $sth = $dbh->prepare("SELECT * FROM tc_individual WHERE hh_position='Head of Household' and valid=1");
+ $sth->execute or die "-E- DB error: $DBI::errstr\n";
+ while ($sqlhashref = $sth->fetchrow_hashref) {
+ $individual = $sqlhashref{individual};
+ $name = $sqlhashref{name};
+
+ $sth2 = $dbh->prepare("SELECT * FROM tc_family WHERE individual='$individual'");
+ $sth2->execute or die "-E- DB error: $DBI::errstr\n";
+
+ my @data = ();
+ while($sqlhashref2 = $sth2->fetchrow_hashref) { push(@data, $sqlhashref2); }
+ my $rows = scalar @data;