亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

帶有計數器的php id并保存文件

帶有計數器的php id并保存文件

PHP
湖上湖 2023-04-15 10:34:57
我想在它前面做一個帶有標準前綴的計數器:202000,然后計算 2020001、2020002、2020003 等。并且這些數據必須保存在我服務器上的文件或數據庫或 txt 文件中,但不會事情。我嘗試了很多,但我可以做到。我對 PHP 很陌生。我現在有了這段代碼,但我有一些問題,所以只保存了最后一個數字,并且前綴是否被計數器替換,如果你超過 10,你會得到 2020010,我想要 20200010。無論如何,這是我的代碼:  <!DOCTYPE html><html dir="ltr">  <head>    <meta charset="utf-8">    <title></title>  </head>  <body>    <?php    $file = 'counter.txt';    // default the counter value to 1    $before = uniqid('202000');    $counter = 1;    // add the previous counter value if the file exists    if (file_exists($file)) {        $counter += file_get_contents($file);    }    // write the new counter value to the file    file_put_contents($file, $counter);    echo $before + $counter;    ?></body></html>有人可以幫我嗎?
查看完整描述

2 回答

?
汪汪一只貓

TA貢獻1898條經驗 獲得超8個贊

您不必在文件中保存實際前綴 (202000)。只需保存計數器(1、2、3、4、5 等)


<?php

$file = 'counter.txt';

$prefix = '202000';


//If file exists then grab content of file(nr) and add it with one else set counter=1

file_exists($file) ? $counter = file_get_contents($file)+1 : $counter = 1;


//This is the number you would like to use (2020001, 2020002 ... 20200010 etc)

$use_number = $prefix .  $counter;  


//Save counter value to file (1,2,3,4 etc...)

file_put_contents($file, $counter);  

?>


查看完整回答
反對 回復 2023-04-15
?
收到一只叮咚

TA貢獻1821條經驗 獲得超5個贊

您可以計算所有文件行,將它們用作計數器的基數


$file = 'someFiel.txt';


$counter = 0; // the file lines


$handle = fopen($file, "r");

while(!feof($handle)){

  $line = fgets($handle);

  $counter++;

}


fclose($handle); //close the file reader


$next_line_no = $counter + 1; // this will add 1 to your counter

然后只需添加您的前綴和下一行


$next_line = '202000' . $next_line_no;

您可以通過計算行數來對 MySQL 執行相同的操作,然后使用 counter + 1 再次添加前綴;


查看完整回答
反對 回復 2023-04-15
  • 2 回答
  • 0 關注
  • 126 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號