jetlife76 Posted December 3, 2011 Author Share Posted December 3, 2011 Output: ( ! ) Notice: Undefined offset: 4 in C:\wamp\www\Fall2011etoyo\data\Pro12Out.php on line 16 Call Stack # Time Memory Function Location 1 0.0004 683760 {main}( ) ..\Pro12Out.php:0 ( ! ) Notice: Undefined offset: 3 in C:\wamp\www\Fall2011etoyo\data\Pro12Out.php on line 16 Call Stack # Time Memory Function Location 1 0.0004 683760 {main}( ) ..\Pro12Out.php:0 ( ! ) Notice: Undefined offset: 2 in C:\wamp\www\Fall2011etoyo\data\Pro12Out.php on line 16 Call Stack # Time Memory Function Location 1 0.0004 683760 {main}( ) ..\Pro12Out.php:0 ( ! ) Notice: Undefined offset: 1 in C:\wamp\www\Fall2011etoyo\data\Pro12Out.php on line 16 Call Stack # Time Memory Function Location 1 0.0004 683760 {main}( ) ..\Pro12Out.php:0 Error: PartNo=AC1003 not found Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/page/2/#findComment-1294100 Share on other sites More sharing options...
jetlife76 Posted December 3, 2011 Author Share Posted December 3, 2011 As you can see at the bottom it gives me the error code i put in if the item isn't found, it only gives me the Item not found for AC1003, for the other items it gives me these same codes but at the bottom it displays the table with the correct outputs in seperate parts: ID Part Count Price Value AC1002 Handsaws 10 10.00 100.00 but it is in a table and are lined up correctly: <?php $inf = 'infile.txt'; $FILEH = fopen($inf, 'r') or die ("Cannot open $inf"); $inline = fgets($FILEH,4096); $found = 0; $id = $_POST['id']; //$array = explode(':', $inline); while (!feof($FILEH) && !($found)){ list($ptno,$ptname,$num,$price,$value) = explode(':',$inline); if ($ptno == $id) { print '<table border=1>'; print '<th> ID <th> Part <th> Count <th> Price <th> Value'; print "<tr><td> $ptno </td><td>$ptname</td>"; print "<td> $num </td><td>$price</td><td>$value</td><tr>"; print '</table>'; $found = 1; } $inline = fgets($FILEH,4096); } if ($found !=1) { print "Error: PartNo=$id not found"; } fclose ($FILEH); ?> Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/page/2/#findComment-1294103 Share on other sites More sharing options...
xyph Posted December 3, 2011 Share Posted December 3, 2011 Why don't you just view the source of the page and copy and paste? As you can see at the bottom it gives me the error code i put in if the item isn't found, it only gives me the Item not found for AC1003, for the other items it gives me these same codes but at the bottom it displays the table with the correct outputs in seperate parts: You'll have to rewrite this, as I'm not entirely sure what you are saying. You are mixing terminology, and I can't tell if you're talking about PHP errors, or the errors your code is outputting. Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/page/2/#findComment-1294107 Share on other sites More sharing options...
jetlife76 Posted December 3, 2011 Author Share Posted December 3, 2011 I copied and pasted the errors that my output page are showing, it gives me all those Undefined Offset errors, then at the bottom of those errors it shows the error message i created in php, which comes up as AC1003 not found. (Meaning AC1003 could'nt be found) If i select AC1002 or any other except AC1003, it gives me the same Underfined Offset errors on my output form but prints the table with all the item information for that item that i selected. Hope that clears it up a bit for ya. Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/page/2/#findComment-1294115 Share on other sites More sharing options...
xyph Posted December 4, 2011 Share Posted December 4, 2011 I know why you aren't getting AC1003. That's related to the way you've looped through the values. If you check out the manual on fgets, it will show you a better way to loop through all the lines - one that keeps proper track of the file pointer. Alternately, you could simply use file to get the contents of the file into a line-by-line array and loop through that. Why you're getting the offset errors is beyond the scope of what you've provided. I copied and pasted your sample data into a text file, and copied your code provided. I did not get these errors. You could have an empty line in your text file that's being read. Any suggestion I provide is simply guessing. Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/page/2/#findComment-1294119 Share on other sites More sharing options...
jetlife76 Posted December 4, 2011 Author Share Posted December 4, 2011 well thanks for what you did help me with, guess i'll sit down and read some and try to figure out what it is i'm doing wrong Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/page/2/#findComment-1294123 Share on other sites More sharing options...
meltingpoint Posted December 4, 2011 Share Posted December 4, 2011 <?php $inf = 'infile.txt'; $id = 'AC1000';//--------this would be the $id variable passed by the form ($_POST) or by a link ($_GET) if(!file_exists($inf)) { echo "Sorry- could not find file"; } // $record_count =0; // $openedfile =fopen($inf, "r"); if(!$openedfile) { echo "File could not be OPENED. Please notify the Administrator"; echo "</tr>"; exit; } // flock($openedfile, LOCK_EX) or die("Error!- Could not obtain exclusive lock on the file to edit. Please try again"); while(!feof($openedfile)) { $record_count++; $hold[$record_count] = explode(":", trim(fgets($openedfile))); if($hold[$record_count][0] == $id) $X = $record_count;//-----sets $X to array that contains your match } flock($openedfile, LOCK_UN); fclose($openedfile); // //---Now all your data is in an array $hold where each line of you file is represented as so // $hold[0] which would look like $hold[0][0] = AC1000, $hold[0][1] = Hammers, $hold[0][2] = 122, $hold[0][3] = 12.50 // $hold[1] $hold[1][0] = AC1001, $hold[1][1] = Wrenches, $hold[1][2] = 5, $hold[1][3] = 5.00 //......etc // if(empty($X)) { echo "Error: Part number not found."; exit; } else { echo "<table border=1>"; echo "<th> ID <th> Part <th> Count <th> Price"; echo "<tr><td>".$hold[$X][0]."</td><td>".$hold[$X][1]."</td>"; echo "<td>".$hold[$X][2]."</td><td>".$hold[$X][3]."</td></tr>"; echo "</table>"; } ?> Run and tested. Worked for me. Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/page/2/#findComment-1294176 Share on other sites More sharing options...
meltingpoint Posted December 4, 2011 Share Posted December 4, 2011 And actually...........the table html is a little mixed up. It should look like; echo "<table border=1>"; echo "<tr>"; echo "<th> ID </th> <th>Part </th><th> Count </th><th> Price</th>"; echo "</tr>"; echo "<tr>"; echo "<td>".$hold[$X][0]."</td><td>".$hold[$X][1]."</td><td>".$hold[$X][2]."</td><td>".$hold[$X][3]."</td>"; echo "</tr>"; echo "</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/page/2/#findComment-1294180 Share on other sites More sharing options...
jetlife76 Posted December 4, 2011 Author Share Posted December 4, 2011 Ok yea this looks like what i was reading on the link XYPH recommended, thanks alot guys!!! Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/page/2/#findComment-1294238 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.