Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBetsabeh Yeganeh
3,793 PointsUndefined Index in php I get an error for second 'if' statement that is "Undefined Index". what else should I add?
<?php
$name=array(
'01' => 'Alabama',
'02' => 'Alaska',
'04' => 'Arizona',
'05' => 'Arkansas',
'06' => 'California',
'08' => 'Colorado',
'09' => 'Connecticut',
'10' => 'Delaware',
'11' => 'District of Columbia',
'12' => 'Florida',
'13' => 'Georgia',
'15' => 'Hawaii',
'16' => 'Idaho',
'17' => 'Illinois',
'18' => 'Indiana',
'19' => 'Iowa',
'20' => 'Kansas',
'21' => 'Kentucky',
'22' => 'Louisiana',
'23' => 'Maine',
'24' => 'Maryland',
'25' => 'Massachusetts',
'26' => 'Michigan',
'27' => 'Minnesota',
'28' => 'Mississippi',
'29' => 'Missouri',
'30' => 'Montana',
'31' => 'Nebraska',
'32' => 'Nevada',
'33' => 'New Hampshire',
'34' => 'New Mexico',
'35' => 'New York',
'36' => 'North Carolina',
'37' => 'North Dakota',
'38' => 'Ohio',
'39' => 'Oklahoma',
'40' => 'Oregon',
'41' => 'Pennsylvania',
'42' => 'Rhode Island',
'44' => 'South Carolina',
'45' => 'South Dakota',
'46' => 'Tennessee',
'47' => 'Texas',
'48' => 'Utah',
'49' => 'Vermont',
'50' => 'Virginia',
'51' => 'Washington',
'53' => 'West Virginia',
'54' => 'Wisconsin',
'55' => 'Wyoming',
'56' => 'American Samoa'
);
$sum=array('n1_4','n5_9','n10_19','n20_49','n50_99',
'n100_249','n250_499','n500_999','n1000',
'n1000_4','n1000_1','n1000_2','n1000_3','n1000_4'
);
$header=false;
$arr=array(array());
$handle = fopen('county.csv', 'r');
while (($current = fgetcsv($handle)) !== FALSE) {
if (! $header) {
$header=$current;
continue;
}
$res= array_combine($header,$current);
$state = $name[$res['fipstate']];
foreach ($sum as $v){
if (array_key_exists($state,$arr)) {
$arr[$state][$v] = $arr[$state][$v] + $res[$v];
}else{
$arr[$state][$v] = $res[$v];
}
}
}
fclose($handle);
print_r($arr);
?>
1 Answer
shadrackndacayisenga
Python Web Development Techdegree Student 14,189 PointsI've got to admit, it is harder to know exactly know what your code is trying to achieve with no single comment description / doc. However I notice that you are proceeding to use the fopen handle , "$handle" even if the fopen itself might have failed.
To correct that make sure to only use the handle if the open stream was successful. You can use an if block to enclose all the the block; to be dependent on the state of the fopen outcome.
<?php
$name=array(
'01' => 'Alabama',
'02' => 'Alaska',
'04' => 'Arizona',
'05' => 'Arkansas',
'06' => 'California',
'08' => 'Colorado',
'09' => 'Connecticut',
'10' => 'Delaware',
'11' => 'District of Columbia',
'12' => 'Florida',
'13' => 'Georgia',
'15' => 'Hawaii',
'16' => 'Idaho',
'17' => 'Illinois',
'18' => 'Indiana',
'19' => 'Iowa',
'20' => 'Kansas',
'21' => 'Kentucky',
'22' => 'Louisiana',
'23' => 'Maine',
'24' => 'Maryland',
'25' => 'Massachusetts',
'26' => 'Michigan',
'27' => 'Minnesota',
'28' => 'Mississippi',
'29' => 'Missouri',
'30' => 'Montana',
'31' => 'Nebraska',
'32' => 'Nevada',
'33' => 'New Hampshire',
'34' => 'New Mexico',
'35' => 'New York',
'36' => 'North Carolina',
'37' => 'North Dakota',
'38' => 'Ohio',
'39' => 'Oklahoma',
'40' => 'Oregon',
'41' => 'Pennsylvania',
'42' => 'Rhode Island',
'44' => 'South Carolina',
'45' => 'South Dakota',
'46' => 'Tennessee',
'47' => 'Texas',
'48' => 'Utah',
'49' => 'Vermont',
'50' => 'Virginia',
'51' => 'Washington',
'53' => 'West Virginia',
'54' => 'Wisconsin',
'55' => 'Wyoming',
'56' => 'American Samoa'
);
$sum = array('n1_4','n5_9','n10_19','n20_49','n50_99',
'n100_249','n250_499','n500_999','n1000',
'n1000_4','n1000_1','n1000_2','n1000_3','n1000_4'
);
$header=false;
$arr=array(array());
if($handle = fopen('county.csv', 'r')){
while (($current = fgetcsv($handle,1000,',')) !== FALSE) {
if (!$header) {
$header=$current;
continue;
}
$res= array_combine($header,$current);
$state = $name[$res['fipstate']];
foreach ($sum as $v){
if (array_key_exists($state,$arr)) {
$arr[$state][$v] = $arr[$state][$v] + $res[$v];
}else{
$arr[$state][$v] = $res[$v];
}
}
}
fclose($handle);
}
print_r($arr);
?>
for better debugging , perhaps you would describe what your script is trying to achieve.