使用for循环基于用户输入增加数组中的单个值

I'm not entirely sure how to do this and I've been stuck trying to figure this out. I want to be able to increment a single value inside an array every time a user presses a submit button which gets saved into a csv file. However, every time I try the number stays at 1.

Could I get some guidance? Here's what I have:

<?php $name = $_POST["Name"];
$grade = $_POST["Grade"];
$handle = fopen("users.csv", "a+t");

if (!$handle) {
    die("error, can't open file!");
}
if (!flock($handle, LOCK_EX)) {
    die("lock error!");
}
$count = 10;
for ($i = 0; $i < count($count); $i++) {
    $count++; 
    fputcsv($handle, array($i++, $name, $grade)); }
fseek($handle, 0);

while (!feof($handle)) { 

$record = fgetcsv($handle);
?>
<div>
    ID Number: <?php echo $record[0]; ?><br/>
    Name: <?php echo $record[1]; ?><br/>
    Grade: <?php echo $record[2]; ?><br/>
</div> }

Thank you.

The basic strategy is to read the existing records from the CSV file, then use that info to generate a new ID. The new ID could either be based on either:

  1. The total number of records, or
  2. The largest current ID stored.

In your existing code, the for loop seems unnecessary since it is artificially generating a number - which will always be 0 because count($count) always returns 1.

I've cleaned up the code and added some comments:

$name = $_POST["Name"];
$grade = $_POST["Grade"];
$handle = fopen("users.csv", "a+t");

if (!$handle) die("error, can't open file!");
if (!flock($handle, LOCK_EX)) die("lock error!");

$records = array();
while (!feof($handle))
    $records[] = fgetcsv($handle);

// 1. Use this to find the number of elements
$count = count($records);

// 2. Use this to find the largest current ID
$max = $count > 0 ? max(array_column($records, 0)) : 0;

// Our new ID should be based on (1) or (2) above
$newId = $max + 1;

fputcsv($handle, array($newId, $name, $grade));

fseek($handle, 0);
while (!feof($handle)) { 
    $record = fgetcsv($handle);
    // ... print out your <div> here ...
}