将数据添加到 csv 并增加其中一个值
Add data to a csv and increase one of the values
你好,我会试着解释清楚这个,有点混乱
我有一个 .csv 文件,名称 users.csv 格式如下:
id name surname email
并使用此数据:
1 paul harrison test@test.com
2 robin martinez test@test.com
3 alma halford test@test.com
问题是我需要将表单中的数据引入到csv中,但是当我们从表单中获取数据时没有引入id元素,我该如何增加一个id并添加同时id后面的数据在同一行?
<form style="text-align: center;" method="post">
name: <input type="text" name="name">
<br><br>
surname: <input type="text" name="surname">
<br><br>
Email: <input type="email" name="mail">
<br><br>
Password: <input type="password" name="pwd">
<br><br>
smartphone: <input type="tel" name="smart">
<br><br>
city: <input type="text" name="city">
<br><br>
C.P: <input type="number" name="cp">
<br><br>
<input type="submit" name="send">
</form>
要添加更多信息,想法是每次将数据添加到 users.csv 时,csv 中的 id 列都会增加,每次我们在 csv 中写入数据时,id 文件都会增加一个。
如果你想实现像SQL那样的自增行为,只需要找到一个最大的ID,然后增加一个。结果是新行的新标识符。
因此求最大值:
$csv = ''; // here you csv
$max = 0;
foreach (explode("\n", $csv) as $row) {
foreach (explode("\t", $row) as $index => $col) {
if ($index == 0 && (int)$col > $max)
$max = (int)$col;
}
}
因此,您在 $max
变量中获得最大 ID。正如我之前所说,只要将它增加一个,你就会有新行的新标识符。
你好,我会试着解释清楚这个,有点混乱
我有一个 .csv 文件,名称 users.csv 格式如下:
id name surname email
并使用此数据:
1 paul harrison test@test.com
2 robin martinez test@test.com
3 alma halford test@test.com
问题是我需要将表单中的数据引入到csv中,但是当我们从表单中获取数据时没有引入id元素,我该如何增加一个id并添加同时id后面的数据在同一行?
<form style="text-align: center;" method="post">
name: <input type="text" name="name">
<br><br>
surname: <input type="text" name="surname">
<br><br>
Email: <input type="email" name="mail">
<br><br>
Password: <input type="password" name="pwd">
<br><br>
smartphone: <input type="tel" name="smart">
<br><br>
city: <input type="text" name="city">
<br><br>
C.P: <input type="number" name="cp">
<br><br>
<input type="submit" name="send">
</form>
要添加更多信息,想法是每次将数据添加到 users.csv 时,csv 中的 id 列都会增加,每次我们在 csv 中写入数据时,id 文件都会增加一个。
如果你想实现像SQL那样的自增行为,只需要找到一个最大的ID,然后增加一个。结果是新行的新标识符。
因此求最大值:
$csv = ''; // here you csv
$max = 0;
foreach (explode("\n", $csv) as $row) {
foreach (explode("\t", $row) as $index => $col) {
if ($index == 0 && (int)$col > $max)
$max = (int)$col;
}
}
因此,您在 $max
变量中获得最大 ID。正如我之前所说,只要将它增加一个,你就会有新行的新标识符。