sniesler08 Posted April 29, 2011 Share Posted April 29, 2011 this is my code im getting an error message Notice: Undefined offset: 20 in C:\wamp\www\examples\ideas\new2.php on line 33 <?php echo "<table>\n"; //Move through a CSV file, and output an associative array for each line ini_set("auto_detect_line_endings", 1); $current_row = 1; $d=0; $handle = fopen("widget.csv", "r"); while ( ($data = fgetcsv($handle, 10000, ",") ) !== FALSE ) { $number_of_fields = count($data); echo "<thead>\n<tr>"; if ($current_row == 1) { //Header line /* for ($c=0; $c < $number_of_fields; $c++) */ { $header_array[$d] = $data[$d]; echo "<th>" . $data[$d] . "</th>"; } echo "</tr>\n</thead>\n\n<tbody>"; } else { //Data line for ($c=1; $c < $number_of_fields; $c++) echo "<tr>"; { $data_array[$c] = $data[$c]; /* echo "<td>" . $data[$c] . "</td>"; */ } echo "</tr>\n"; } $current_row++; } fclose($handle); echo "</tbody>\n</table>"; echo $number_of_fields; echo "<br />"; echo $current_row; ?> this is my csv file A,Airlie Beach,Andergrove,Alexandra,Armstrong Beach,Alligator Creek,,,,,,,,,,,,,, B,Bucasia,Blacks Beach,Beaconsfield,Bakers Creek,Balberra,Bloomsbury,Breadalbane,Ball Bay,Belmunda,,,,,,,,,, C,Cannonvale,Calen,Crystal Brook,Cremorne,Chelona,Campwin Beach,Cape Hillsborough,Conway,,,,,,,,,,, D,Dows Creek,Dumbleton,Dolphin Heads,,,,,,,,,,,,,,,, E,Eimeo,Eton,Erakala,,,,,,,,,,,,,,,, F,Foulden,Foxdale,Flametree,Farleigh,Freshwater Point,,,,,,,,,,,,,, G,Glen Isla,Glenella,,,,,,,,,,,,,,, ,, H,Homebush,Hampden,,,,,,,,,,,,, ,,,, I,,,,,,,,,,,,,,, ,,,, J,Jubilee Pocket,,,,,,,,,,,,,, ,,,, K,Kinchant Dam,Kolijo,Koumala,Kuttabul,,,,,,,,,,, ,,,, L,Laguna Quays,,,,,,,,,,,,,, ,,,, M,McEwens Beach,Mackay,Mackay Harbour,Mandalay,Marian,Mia Mia,Middlemount,Midge Point,Mirani,Moranbah,Mount Charlton,Mount Jukes,Mount Julian,Mount Marlow,Mount Martin,Mount Ossa,Mount Pelion,Mount Pleasant,Mount Rooper N,Narpi,Nebo,Nindaroo,North Eton,,,,,,,,,,,,,,, O,Oakenden,Ooralea,,,,,,,,,,,,,,,,, P,Palmyra,Paget,Pindi Pindi,Pinevale,Pioneer Valley,Pleystowe,Preston,Proserpine,,,,,,,,,,, Q,,,,,,,,,,,,,,,,,,, R,Racecourse,Richmond,Riordanvale,Rosella,Rural View,,,,,,,,,,,,,, S,St Helens Beach,Sandiford,Sarina,Seaforth,Slade Point,Shoal Point,Shute Harbour,Shutehaven,Strathdickie,Sugarloaf,Sunnyside,,,,,,,, T,Te Kowai,The Leap,,,,,,,,,,,,,,,,, U,,,,,,,,,,,,,,,,,,, V,Victoria Plains,,,,,,,,,,,,,,,,,, W,Wagoora,Walkerston,Woodwark,,,,,,,,,,,,,,,, X,,,,,,,,,,,,,,,,,,, Y,Yalboroo,,,,,,,,,,,,,,,,,, Z,,,,,,,,,,,,,,,,,,, Could anyone please help little stuck on why its causing this error. Quote Link to comment https://forums.phpfreaks.com/topic/235135-notice-undefined-offset/ Share on other sites More sharing options...
fugix Posted April 29, 2011 Share Posted April 29, 2011 try changing this while (($data = fgetcsv($handle, 10000, ",") ) !== FALSE ) to this while ( ($data = fgetcsv($handle) ) !== FALSE ) Quote Link to comment https://forums.phpfreaks.com/topic/235135-notice-undefined-offset/#findComment-1208434 Share on other sites More sharing options...
sniesler08 Posted April 29, 2011 Author Share Posted April 29, 2011 Unfortunately that seems to have no effect its still saying Notice: Undefined offset: 20 in C:\wamp\www\examples\ideas\new2.php on line 36 which is the $data_array[$c] = $data[$c]; hmm... any ideas Quote Link to comment https://forums.phpfreaks.com/topic/235135-notice-undefined-offset/#findComment-1208441 Share on other sites More sharing options...
fugix Posted April 29, 2011 Share Posted April 29, 2011 i believe that it is in you "M" row...you do not have any commas ending this row Quote Link to comment https://forums.phpfreaks.com/topic/235135-notice-undefined-offset/#findComment-1208443 Share on other sites More sharing options...
sniesler08 Posted April 29, 2011 Author Share Posted April 29, 2011 each row has 21 commas well at the moment till i add more data what happens if i add more data to it how would that effect it Quote Link to comment https://forums.phpfreaks.com/topic/235135-notice-undefined-offset/#findComment-1208445 Share on other sites More sharing options...
PFMaBiSmAd Posted April 29, 2011 Share Posted April 29, 2011 The error is because your for(){} loop consists entirely of the following two lines of code - for ($c=1; $c < $number_of_fields; $c++) echo "<tr>"; Then you execute the $data_array[$c] = $data[$c]; statement after that for loop ends, but $c is equal to $number_of_fields at that point in time, which doesn't exist. You need to move that echo statement, either before the start of the for() loop or you need to move it inside the {} that you intended to be part of that for(){} loop. Quote Link to comment https://forums.phpfreaks.com/topic/235135-notice-undefined-offset/#findComment-1208448 Share on other sites More sharing options...
sniesler08 Posted April 30, 2011 Author Share Posted April 30, 2011 i am having another problem thou i am trying to have it as the first column of the array output into the table as A Airlie Beach, Alderly B Bucasia, Blacks Beach C Cannonvale and so on i thought i was on the right track but having difficulties getting this to happen any help would be appreciated This is the output of how it stands so far. A Bucasia Blacks Beach Beaconsfield Bakers Creek Balberra Bloomsbury Breadalbane Ball Bay Belmunda Cannonvale Calen Crystal Brook Cremorne Chelona Campwin Beach Cape Hillsborough Conway Dows Creek Dumbleton Dolphin Heads Eimeo Eton Erakala Foulden Foxdale Flametree Farleigh Freshwater Point Glen Isla Glenella Homebush Hampden Jubilee Pocket Kinchant Dam Kolijo Koumala Kuttabul Laguna Quays McEwens Beach Mackay Mackay Harbour Mandalay Marian Mia Mia Middlemount Midge Point Mirani Moranbah Mount Charlton Mount Jukes Mount Julian Mount Marlow Mount Martin Mount Ossa Mount Pelion Mount Pleasant Mount Rooper Narpi Nebo Nindaroo North Eton Oakenden Ooralea Palmyra Paget Pindi Pindi Pinevale Pioneer Valley Pleystowe Preston Proserpine Racecourse Richmond Riordanvale Rosella Rural View St Helens Beach Sandiford Sarina Seaforth Slade Point Shoal Point Shute Harbour Shutehaven Strathdickie Sugarloaf Sunnyside Te Kowai The Leap Victoria Plains Wagoora Walkerston Woodwark Yalboroo Quote Link to comment https://forums.phpfreaks.com/topic/235135-notice-undefined-offset/#findComment-1208507 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.