简体   繁体   中英

add value +1 in phrase number

I want to add value in phrase in database sql in database i save phrasr like this

DO-2500-01
DO-2500-02

now my question how can add +1 in last value like this DO-2500-03 / DO-2500-04 this my code

$getse = $DB_con->prepare("SELECT serial FROM `customer` WHERE user_add=:id ORDER BY serial DESC LIMIT 1");
$getse->execute(array(":id"=>$user_id));                  
$getse = $getse->fetch(PDO::FETCH_OBJ);
$addone = $getse->serial + 1;
echo $addone;

this is my code i get last serial and i want to add +1 for example last serial in database is DO-2500-04 I want to get this value and add +1 To become like this DO-2500-05

Split string, increase the last part and combine it back

$addone = explode('-', "DO-2500-04"); 
$addone[count($addone)-1] += 1;
// Append 0 if the last part less then 10
$addone[count($addone)-1] = str_pad($addone[count($addone)-1], 2, 0, STR_PAD_LEFT);
echo $addone = implode('-', $addone);

You can get this based on your requirement.

If DO-2500-99 should be DO-2501-00 then below code can work.

$serialArr= explode('-', $getse->serial); 
$serialNo = (int)$serialArr [1].$serialArr [2];
$newSerialNo = $serialNo + 1; 
$newSerialNo = substr($newSerialNo, 0, -2)."-".substr($newSerialNo , -2);
$newSerial = $serialArr[0].'-'.$newSerialNo;

If DO-2500-99 should be DO-2500-100 then below code can work.

$getse = 'DO-2500-99';
$serialArr= explode('-', $getse->serial); 
$serialNo = (int)$serialArr [2];
$serialArr[2] = $serialNo + 1; 
$newSerial = implode('-',$serialArr);

DO-2500-04 it's a String value and you cannot use arithmetic operators on it. If you just want last counter to get incremented, what you need to do is get the last value by splitting the string by - .

Then get the last value and increment it and then again join the array by - .

To split and join the array we can use explode() and implode() .

This is how you can get the last value from string using explode()

$values=explode("-","DO-2500-04"); // $values is now array.
$lastvalue=(int)$values[2];
$lastvalue++;
if($lastvalue<9) {
   $lastvalue = "0" . $lastvalue;
}
$values[2]=$lastvalue;
$incremented_string=implode("-",$values);

First parameter in explode denotes the chracter by you want to split the string same as first parameter in implode denotes the glue by you want to join the array.

http://php.net/manual/en/function.explode.php

http://php.net/manual/en/function.implode.php

Just for fun, you could do a lot of this in the SQL like:

$getse = $DB_con->prepare("SELECT serial, 
    LEFT(serial,8) AS serialBase,
    SUBSTRING(serial,8) AS serialIdx 
    FROM `customer` WHERE user_add=:id
    ORDER BY serial DESC LIMIT 1");
$getse->execute(array(":id"=>$user_id));                  
$getse = $getse->fetch(PDO::FETCH_OBJ);
$addone = $getse->serialBase + str_pad(($getse->serialIdx + 1),2,'0',STR_PAD_LEFT);
echo $addone;

Updated the SQL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM