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

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

Member Avatar for diafol
$ptno =

also split() is deprecated - try explode

$id is not initialised.

Bit of a mess :(

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);
?>
Member Avatar for diafol

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

Member Avatar for diafol

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.