<?php
$fName = "item.txt"; // 1
$fp = fopen($fName, "r") or die("File not found/opened!"); // 2
$lineCount = 0;
while ($lineIn = fgets($fp) ) { // 3
print("$lineIn <br/>");
$lineCount++;
}
print ("line count = $lineCount<br/><br/>");
fclose($fp);
$fp = fopen($fName, "r"); // 4
$lineIn = fgets($fp);
$id = substr($lineIn, 0, 5);
$name = substr($lineIn, 6, 14);
$cost = substr($lineIn, 21, 6);
$qty = substr($lineIn, 28, 1);
print("<pre>");
print("$id\t$name\t$cost\t$qty\n\n");
fclose($fp);
$fName = "item.csv";
$fp = fopen($fName, "r") or die("File not found/opened!");
$delimiter = ',';
$totalCost = 0.00;
print("<font face = \"Lucida Console\" size = \"3\" >");
printf("%30s", "Item Inventory\n");
printf("%5s \t %-14s \t %6s \t %-3s\n", "id", "name", "cost", "qty");
while ($item = fgetcsv($fp, $delimiter) ) { // 5
$id = $item[0];
$name = $item[1];
$cost = $item[2];
$qty = $item[3];
printf("%5d \t %-14s \t %6.2f \t %1d\n", $id, $name, $cost, $qty);
$totalCost += $cost;
}
printf("%31s %7.2f", "total cost:", $totalCost);
fclose($fp);
?>
preferences:
21.09 ms | 402 KiB | 5 Q