你好,我會試著解釋清楚,這有點令人困惑
我有一個 .csv 檔案,用以下格式命名 users.csv:
id name surname email
并使用此資料:
1 paul harrison [email protected]
2 robin martinez [email protected]
3 alma halford [email protected]
問題是我需要將表單中的資料引入到 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 檔案都會增加一個。
uj5u.com熱心網友回復:
如果你想實作像 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 。正如我之前所說,只需將其增加一,您就有新行的新識別符號。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/361794.html
