第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

帶有計(jì)數(shù)器的php id并保存文件

帶有計(jì)數(shù)器的php id并保存文件

PHP
湖上湖 2023-04-15 10:34:57
我想在它前面做一個(gè)帶有標(biāo)準(zhǔn)前綴的計(jì)數(shù)器:202000,然后計(jì)算 2020001、2020002、2020003 等。并且這些數(shù)據(jù)必須保存在我服務(wù)器上的文件或數(shù)據(jù)庫(kù)或 txt 文件中,但不會(huì)事情。我嘗試了很多,但我可以做到。我對(duì) PHP 很陌生。我現(xiàn)在有了這段代碼,但我有一些問(wèn)題,所以只保存了最后一個(gè)數(shù)字,并且前綴是否被計(jì)數(shù)器替換,如果你超過(guò) 10,你會(huì)得到 2020010,我想要 20200010。無(wú)論如何,這是我的代碼:  <!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貢獻(xiàn)1898條經(jīng)驗(yàn) 獲得超8個(gè)贊

您不必在文件中保存實(shí)際前綴 (202000)。只需保存計(jì)數(shù)器(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);  

?>


查看完整回答
反對(duì) 回復(fù) 2023-04-15
?
收到一只叮咚

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超5個(gè)贊

您可以計(jì)算所有文件行,將它們用作計(jì)數(shù)器的基數(shù)


$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;

您可以通過(guò)計(jì)算行數(shù)來(lái)對(duì) MySQL 執(zhí)行相同的操作,然后使用 counter + 1 再次添加前綴;


查看完整回答
反對(duì) 回復(fù) 2023-04-15
  • 2 回答
  • 0 關(guān)注
  • 125 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)