Undefined offset:

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wasabi
Forum Newbie
Posts: 17
Joined: Sat Sep 04, 2004 9:38 am
Location: Adelaide

Undefined offset:

Post by wasabi »

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hi People 

The title says it all.

Undefined offset happens when my .txt or csv file has empty spaces.

Can someone look through my code and help me. I'm sure there is another way of writting the code but I'm very limited with my PHP knowledge.

Code: Select all

<?php 

$textfiles = "stn-numbers2.csv"; 

if (!($stn = fopen($textfiles,"r"))) exit ("Unable to Open $textfile");
    
echo "<table border="1" cellpadding="10" cellspacing="0"><tr><td>File Name</td><td>Title</td><td>Date</td><td>Modified</td></tr>\n";
	
while (!feof($stn)) {
	 $line = fgets($stn, 2048);
	 $out = array($line);
	  list($file, $title, $date, $modified) = split ('[,]', $line);
   
     echo "<tr><td>$file</td><td><a href="http://www.artc.com.au/docs/Operations/notices/special-train-notice/stn-data/$file.pdf">$title</a></td><td>$date</td><td>$modified</td></tr>\n";
		}
		echo "<table>";
		$stn++;
fclose ($stn);

?>
Prints to page:

Code: Select all

Notice: Undefined offset: 3 in C:\ARTC\site\elements\array.php on line 12

Notice: Undefined offset: 2 in C:\ARTC\site\elements\array.php on line 12

Notice: Undefined offset: 1 in C:\ARTC\site\elements\array.php on line 12
and the rest of information is currectly printed to the page.

Sample

File Name Title Date Modified
1687 Vide Vide Lidcombe-Auburn-Clyde September 7-8 3/09/2004 12:10
1687 Lidcombe-Auburn-Clyde September 7-8 3/09/2004 12:09
1303 Works Programme Operation Join Forces September 18-19 3/09/2004 12:09



feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Last edited by wasabi on Sat Sep 04, 2004 9:57 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

post a sample line or three.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

[php_man]split[/php_man]()
wasabi
Forum Newbie
Posts: 17
Joined: Sat Sep 04, 2004 9:38 am
Location: Adelaide

spit()

Post by wasabi »

Code: Select all

<?php

// Delimiters may be slash, dot, or hyphen
$date = "04/30/1973";
list($month, $day, $year) = split('[/.-]', $date);
echo "Month: $month; Day: $day; Year: $year<br />\n";

?>
what about the split? Is there something I'm missing? I can't see anything wrong.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your sample lines do not have any commas in them..
wasabi
Forum Newbie
Posts: 17
Joined: Sat Sep 04, 2004 9:38 am
Location: Adelaide

Post by wasabi »

yeah I've shown the results. I'm using csv file instead text. Is there anyway i can included commas in the text file without having to manual enter them in. There is over 1600 entries. At the moment I have the entries in excel and saved as csv but still get undefined offset errors. Any help would be greatly appreciated.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you could "manually" check if the indices exist using isset in combination with setting the variable and a ternary...
wasabi
Forum Newbie
Posts: 17
Joined: Sat Sep 04, 2004 9:38 am
Location: Adelaide

Problem SOLVED

Post by wasabi »

Thx for the input. I ended up doing comma delimited cvs file. All works sweet as now.

Although I still want to print 100 records at a time. Does anyone know if this is possible using a text file?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

just add the lines to an arary, and when count array >= 100 print them and reset the array.
wasabi
Forum Newbie
Posts: 17
Joined: Sat Sep 04, 2004 9:38 am
Location: Adelaide

Array

Post by wasabi »

Is there any example code I could look at or read about?
wasabi
Forum Newbie
Posts: 17
Joined: Sat Sep 04, 2004 9:38 am
Location: Adelaide

Anyone else

Post by wasabi »

Any help would be greatly appreciated.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Well, the right place for you: http://www.php.net/manual

First you read the section on variables -> this way you have something that change value from 0 to 100.

Then you read about control structures -> this way you can write a loops and test if the counter == 100 or not.

Then you read about arrays -> this way you can add stuff to the array, or get stuff out of the array.
wasabi
Forum Newbie
Posts: 17
Joined: Sat Sep 04, 2004 9:38 am
Location: Adelaide

Post by wasabi »

Gee whats with the attitude?
Post Reply