Jump to content

Notice: Undefined offset


sniesler08

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.