0

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);
?>
2
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by diafol
0

The last entry in the .txt file is Screwdriver, sorry for the emoticon, didnt notice it when i was pasting

0
$ptno =

also split() is deprecated - try explode

$id is not initialised.

Bit of a mess :(

Edited by diafol: n/a

0

Ok i have been working on it since then, here is what i have now, but i keep getting Undefined Offset errors, but it prints my table at the bottom of the errors, any clue?

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

You're listing 5 variables for 4 values:

list($ptno,$ptname,$num,$price,$value) = explode(':',$inline);

exploding this : AC1000:Hammers:122:12.50 gives 4 values

0

This partially works for me:

$inf = 'file.txt';
$lines = file($inf) or die ("Cannot open $inf");
$found = 0;
$id = 'AC1000';
foreach($lines as $line){
	list($ptno,$ptname,$num,$price) = explode(':',$line);
	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;
	}
}
if ($found !=1) {
	print "Error: PartNo=$id not found";
}

I took off $value from the list(),but that will give a warning for $value in the table.
So either get rid of $value or include these values in yout text file lines.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.