jetlife76 Posted December 3, 2011 Share Posted December 3, 2011 trying to create a simpleprogram that will read a text file and output information and calculations using the data in the text file. I have 4 radio buttons which represent an item Number. when one is selected the output form should print to a table the ID,Part,Count, Price, and the inventory Value= ($count * $price), can anyone tell me what i am doing wrong? This is what the .txt file looks like: AC1000:Hammers:122:12.50 AC1001:Wrenches:5:5.00 AC1002:Handsaws:10:10.00 AC1003:Screwdrivers:222:3.00 Here's what i have so far: <?php $inf = 'infile.txt'; $FILEH = fopen($inf, 'r') or die ("Cannot open $inf"); $inline = fgets($FILEH,4096); $found = 0; //$ptno = //if (isset($_POST['AC1000']) || isset($_POST['AC1000']) || isset($_POST['AC1000']) || isset($_POST['AC1000'])) { while (!feof($FILEH) && !($found)){ list($ptno,$ptname,$num,$price) = split (':', $inline); if ($ptno == $id) { print '<table border=1>'; print '<th> ID <th> Part <th> Count <th> Price'; print "<tr><td> $ptno </td><td>$ptname</td>"; print "<td> $num </td><td> \$price</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/ Share on other sites More sharing options...
ngreenwood6 Posted December 3, 2011 Share Posted December 3, 2011 Maybe I am missing something but I dont see where you have given $id a value before this line: if ($ptno == $id) { Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/#findComment-1293779 Share on other sites More sharing options...
jetlife76 Posted December 3, 2011 Author Share Posted December 3, 2011 Ok , that's what i don't understand, i copied the code out of the book as it was written to see what it actually looks like, i don't understand what value to give to '$id','$ptno',$ptname, or $price as well. i had it running once where it only printed the file contents, but i need to be able to click a radio button and have the corresponding information display for that button. ie. ID- Part- Count - Price and Inventory Value. Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/#findComment-1293781 Share on other sites More sharing options...
ngreenwood6 Posted December 3, 2011 Share Posted December 3, 2011 It seems to me that you would have a radio button that would look like this: <input type="radio" name="part_number" value="5" /> When you submit the form that this part number is on you will get a value either in post or get depending on how you are submitting the form. This will then be the id that you are searching for in the file. So if you are using get it would look like this: $id = $_GET['part_number']; That would need to be put before the while statement allowing you to search for the value in the text file. Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/#findComment-1293784 Share on other sites More sharing options...
jetlife76 Posted December 3, 2011 Author Share Posted December 3, 2011 Not really understanding what you are doing. what is the 5 in the value of the radio button for? My radio buttons look like this: <input type ="radio" name = "id" value = "AC1000">AC1000 <input type ="radio" name = "id" value = "AC1001">AC1001 <input type ="radio" name = "id" value = "AC1002">AC1002 <input type ="radio" name = "id" value = "AC1003">AC1003 and i am using the Post method Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/#findComment-1293791 Share on other sites More sharing options...
ngreenwood6 Posted December 3, 2011 Share Posted December 3, 2011 Sorry about that the 5 was the value of the $num variable. Looking at what you posted if you change $id to $_POST['id'] your code might work fine. Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/#findComment-1293802 Share on other sites More sharing options...
jetlife76 Posted December 3, 2011 Author Share Posted December 3, 2011 ok that got rid of alot of the error messages i was getting, but now it's saying that the split funcstion is DEPRECATED, is it not supposed to be since it has already been seperated by ":"? Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/#findComment-1293924 Share on other sites More sharing options...
ngreenwood6 Posted December 3, 2011 Share Posted December 3, 2011 No that is a warning because your error reporting is turned on. The split function is deprecated in php (see here http://http://us2.php.net/split. You should use explode() instead of split(). Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/#findComment-1293927 Share on other sites More sharing options...
jetlife76 Posted December 3, 2011 Author Share Posted December 3, 2011 I've already tried explode before and it gives my a whole page of errors for the line with (SPLIT) i did try "preg_split" and it gave me part of my table with 2 errors, the 1st was no ending delimiter, (is my sntax wrong), the 2nd one was undefined index 'id' in the line beneath the (preg_split) line. Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/#findComment-1293937 Share on other sites More sharing options...
ngreenwood6 Posted December 3, 2011 Share Posted December 3, 2011 I am not sure what you mean by it is giving you a whole page of errors. This should work exactly the same but remove the warning about split: list($ptno,$ptname,$num,$price) = explode(':', $inline); Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/#findComment-1293953 Share on other sites More sharing options...
jetlife76 Posted December 3, 2011 Author Share Posted December 3, 2011 Error messages i get when i do the explode: Undefined offset: 3 in C:\wamp\www Undefined offset: 2 in C:\wamp\www Undefined offset: 1 in C:\wamp\www *all reference line 15 which is the explode line when i set $id = $_POST it got rid of the undefined index error Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/#findComment-1293958 Share on other sites More sharing options...
jetlife76 Posted December 3, 2011 Author Share Posted December 3, 2011 ok i figured out that you cannot use "list" with explode, but now i am getting an error that says, unexpected "," in the explode line, any clue why? Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/#findComment-1293996 Share on other sites More sharing options...
xyph Posted December 3, 2011 Share Posted December 3, 2011 You can use list with explode. What makes you think you can't? You can test things like this quite easily. <?php $str = 'one,two,three'; list( $v1, $v2, $v3 ) = explode( ',', $str ); echo "$v1 - $v2 - $v3"; ?> The error means you've used incorrect syntax. Since we can't see your syntax, we can't say exactly what you've done wrong. Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/#findComment-1294002 Share on other sites More sharing options...
jetlife76 Posted December 3, 2011 Author Share Posted December 3, 2011 This is what i have and everytime i try to run the program it gives me those errors i posted before <?php $inf = 'infile.txt'; $FILEH = fopen($inf, 'r') or die ("Cannot open $inf"); $inline = fgets($FILEH,4096); $found = 0; while (!feof($FILEH) && !($found)){ list($ptno,$ptname,$num,$price) = explode(':',$inline); if ($ptno == $id) { print '<table border=1>'; print '<th> ID <th> Part <th> Count <th> Price'; print "<tr><td> $ptno </td><td>$ptname</td>"; print "<td> $num </td><td> \$price</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/#findComment-1294015 Share on other sites More sharing options...
xyph Posted December 3, 2011 Share Posted December 3, 2011 The first step to debugging is verifying your inputs contain what you expect them to. You still haven't defined $id anywhere. Try echo'ing out the raw value of $inline. Does it contain what you expect? Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/#findComment-1294017 Share on other sites More sharing options...
jetlife76 Posted December 3, 2011 Author Share Posted December 3, 2011 Ok i defined $id and it gives me the output i want, but i'm still getting those same 3 errors Undefined offset:1 -2- 3 Here's the new code: <?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) = explode(':',$inline); if ($ptno == $id) { print '<table border=1>'; print '<th> ID <th> Part <th> Count <th> Price'; print "<tr><td> $ptno </td><td>$ptname</td>"; print "<td> $num </td><td>$price</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/#findComment-1294059 Share on other sites More sharing options...
xyph Posted December 3, 2011 Share Posted December 3, 2011 Nothing in your code will return that error. You need to pay attention to the line numbers being given with the errors. I copy and pasted your code exactly, changed $id = 'AC1002';, and everything worked as expected. Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/#findComment-1294069 Share on other sites More sharing options...
jetlife76 Posted December 3, 2011 Author Share Posted December 3, 2011 ok in line 16 of my code is where it said the problem was. Line 16 in my code is the "explode" line, you forget i have HTML tags and headers on my file, so it won't be the same as when you copy and paste the code i post. i also tried to change the variable to what you changed it to and i get the same errors Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/#findComment-1294073 Share on other sites More sharing options...
xyph Posted December 3, 2011 Share Posted December 3, 2011 HTML and PHP are unrelated. You'd get an error like that if explode returned less values than list requires. Again, check and make sure $inline contains what you think it does before you explode it. <?php $string = 'thisstringhasnocolons'; list( $a, $b, $c ) = explode( ':', $string ); ?> outputs Notice: Undefined offset: 2 in C:\Apps\wamp\www\examples\temp.php on line 5 Notice: Undefined offset: 1 in C:\Apps\wamp\www\examples\temp.php on line 5 If you want to show your appreciation for the help you're getting, try your best to avoid us posting the same advice twice. Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/#findComment-1294075 Share on other sites More sharing options...
jetlife76 Posted December 3, 2011 Author Share Posted December 3, 2011 Ok i think there is a misunderstanding here i was saying that it won't be the same line Number for us since i am not posting my html, the code i posted starts on line 8 for me which would be line 1 for you correct?, what do you mean by checking $inline, i have looked at my txt file and everything is there, i have set the Variable for it correctly, and i even did a print $inline without the table, what else should i do? Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/#findComment-1294083 Share on other sites More sharing options...
xyph Posted December 3, 2011 Share Posted December 3, 2011 I see where I misunderstood. I mean echo it and BE SURE it contains a single line. You're only getting that error because when this line is called list($ptno,$ptname,$num,$price) = explode(':',$inline); $inline contains less than 3 colons. Keep in mind that explode will be called on EACH LINE. So if any line contains less than 3 colons, you will get at least 1 undefined offset error Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/#findComment-1294085 Share on other sites More sharing options...
jetlife76 Posted December 3, 2011 Author Share Posted December 3, 2011 ok are you asking if the lines in the txt file have 3 colons? If so i have counted and there are 4 colons in each line. Where else should i look if that's not the right place. Not clear on what your last post wants me to do, sorry! Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/#findComment-1294091 Share on other sites More sharing options...
xyph Posted December 3, 2011 Share Posted December 3, 2011 The example you posted has 3 colons in each line. You need to echo $inline and ensure it contains a value that can be exploded into 4 different parts. If it can't, you will get those errors. Other than doing it for you, it can't get more simple than this. Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/#findComment-1294093 Share on other sites More sharing options...
jetlife76 Posted December 3, 2011 Author Share Posted December 3, 2011 ok that's what i thought you meant, when i did it the first time you asked me to earlier in the posts it returned the 3 seperate parts with colons, I just did it again and got the same thing, it gives me the same error codes and at the bottom it gives me the output in seperate parts as before. that's why i don't understand why im not getting the same resultd as you are. Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/#findComment-1294095 Share on other sites More sharing options...
xyph Posted December 3, 2011 Share Posted December 3, 2011 You should copy and paste the output of your script here, and your actual script with the debugging included. Quote Link to comment https://forums.phpfreaks.com/topic/252363-reading-txt-files-manipulating-its-data-for-output/#findComment-1294097 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.