Jump to content

reading .txt files & manipulating its data for output


jetlife76

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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>";

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.