Jump to content

reading .txt files & manipulating its data for output


jetlife76

Recommended Posts

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);
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.