Jump to content

Str_Getcsv Notice On Reading Array


markschum

Recommended Posts

Hi all,

I am an old programmer with Cobol and RPG, some Visual Basic 6 and Python.

I am starting PHP for a web site to generate parts of pages.

I am running apache, php and mysql on a local machine as a test system.

 

I am reading a file , comma delimited which contains either text , price, or a filename for a long description.

 

<?php
$fil = "./data/catalog.txt";
if (!file_exists($fil))
	echo "Catalog not found - Ending <br>";
$lines = file($fil);
foreach($lines as $l) {
	$fields = str_getcsv($l,",");
	echo "Item: ".$fields[0]."<br>";
	echo "Price: ".$fields[1]."<br>";
	if (file_exists($fields[2])) {
			echo $f." Decription Found<br>";
	} else {
				echo "No Description<br>";
			}

}
?>

 

I get a Notice on the lines like echo "Price; ".$Fields[1].<br>;

 

 

Notice: Undefined offset: 1 in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\file_read.php on line 15 Price:

 

Is there a better way of getting an array element or is it acceptable to just ignore the Notice.

 

 

I dont mind at all if you laugh , point and make fun of this code. I am sticking bits together from the manual.

 

regards

Edited by PFMaBiSmAd
code in code tags please
Link to comment
Share on other sites

I've taken the liberty of cleaning up your code a bit, and it into a function:

<?php

/**
* Description of function here...
* 
* @param string $file The file to read the catalog from.
* @return string
*/
function read_catalog ($file) {
if (!file_exists ($file)) {
	return "Catalog not found - Ending <br>\n";
}

$output = '';

$lines = file ($file);

foreach ($lines as $l) {
	$fields = str_getcsv ($l, ",");

	if (!is_array ($fields) || !isset ($fields[2])) {
		// Not a valid CSV line, skip
		continue;
	}

	$output .= "Item: {$fields[0]}<br>\n";
	$output .= "Price: {$fields[1]}<br>\n";

	if (file_exists ($fields[2])) {
		// TODO: You're missing something here, as $f is undefined at this point.
		$output .= $f . " Description Found<br>\n";
		continue;
	}

	$output .= "No Description<br>\n";

}

return $output;
}

$fil = "./data/catalog.txt";
$catalog = read_catalog ($fil);

// Put this wherever you want to have the catalog listed.
echo $catalog;
?>

 

I think you main problem was that you didn't verify that str_getcsv () did indeed contain some data, and as such you weren't handling empty lines in the source file properly. That's why I've added the check for isset ($fields[2]), but that doesn't quite guarantee that you're getting what you expect. Only that there are at least 2 commas in the current line.

 

PS: Please use the [code][/code] tags around your code, as it helps make both your post and your code a lot easier to read.

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.