markschum Posted September 24, 2012 Share Posted September 24, 2012 (edited) 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 September 24, 2012 by PFMaBiSmAd code in code tags please Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 24, 2012 Share Posted September 24, 2012 You'd want to check if the value is set at that array offset. You can use isset() You might also want to do a print_r() on $fields to see what it contains. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted September 24, 2012 Share Posted September 24, 2012 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. Quote Link to comment Share on other sites More sharing options...
markschum Posted September 24, 2012 Author Share Posted September 24, 2012 ok, thanks I will add some checking to catch those lines that may be incomplete. Quote Link to comment 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.