In my php code, while inserting new record, invoice number should be 0001. Here I want to add characters/a word(for example 'optical') and it should be like optical0001. How can I add this before the number?
$query = "select * from cust_report_invoices order by invoiceID desc limit 1";
$result = $link->query($query);
$row = $result->fetch_assoc();
$invoiceNo = $row['invoiceNo'];
$prefix = 'Baopt_';
$getinvoiceNo = $prefix . str_pad($invoiceNo + 1, 4, 0, STR_PAD_LEFT);
$sql = "INSERT INTO cust_report_invoices (invoiceNo,invoiceDate)
VALUES ('$getinvoiceNo', '$getinvoiceDate' )";
str_pad return string (doc). So just concat with .:
$prefix = 'optical';
$invoice = $prefix . str_pad($invoiceNo + 1, 4, 0, STR_PAD_LEFT);
Edit
When adding new element you need to cut the prefix out - you can use this:
$prefix = 'Baopt_';
$invoiceNo = $row['invoiceNo'];
if (substr($invoiceNo, 0, strlen($prefix)) == $prefix) // if already has prefix
$invoiceNo = substr($invoiceNo, strlen($prefix)); // remove it
$getinvoiceNo = $prefix . str_pad($invoiceNo + 1, 4, 0, STR_PAD_LEFT);
When working in PHP you can use the . as concatenation-operator. I see that you use the function str_pad that returns as string.
So you can just concat the prefix with the invoice number like this.
$prefix = 'optical';
$invoice = $prefix . str_pad($invoiceNo + 1, 4, 0, STR_PAD_LEFT);
$prefix can be anything you want.
When invoice = 0001 this example will return optical0001
Just "Opticals".$getinvoiceNo
Dot is concatenating strings.
Not sure if I understand correctly, you want to prepend "optical" to $getinvoiceNo?
If yes then it is simple,
invoiceId = "optical$getinvoiceNo"
Related
I have the code from the database, the data should appear automatically in sequence. the code is composed of text and number like this CO-00001 but when I generate the code it just adds one digit in the back but not add the value of example CO-00001 to CO-000001 how to solve it?
ex :
$id = 0902340
$query = "SELECT substr(code_id,3,5) as cd FROM tb_inv WHERE substr(code_id,3,5) = '".$id."' ORDER BY code_id DESC LIMIT 1";
$get_id = $this->db->query($query);
$show_id = $get_id->num_rows();
$new_code = $get_id->row();
if($show_id > 0){
$new = $new_code->cd + 1;
$fix_code = $new;
}else{
$fix_code = 'CO-00001';
}
you can't do algebra on alphabets, therefore you need to split the alphabets and the numerics like so:
$id = 0902340
$query = "SELECT substr(code_id,3,5) as cd FROM tb_inv WHERE substr(code_id,3,5) = '".$id."' ORDER BY code_id DESC LIMIT 1";
$get_id = $this->db->query($query);
$show_id = $get_id->num_rows();
$new_code = $get_id->row();
if($show_id > 0){
$prefix = substr($new_code->cd, 0, 3); // get the prefix 3 chars at front
$number = substr($new_code->cd, 3); // get the remaining chars
$new = $number + 1; // add it with 1, php automatically convert strings that can be converted to numbers
$fix_code = $prefix . substr("00000" . $number, -5); // re-assemble the code
}else{
$fix_code = 'CO-00001';
}
you can split the id into two parts, the string one, and the digit one. then increment the digit part then re concat the two parts again
list($strPrefix, $digitPart) = split('-', $oldId);
$digitPart++;
$newCode = $strPrefix . '-' . $digitPart;
I have a field (demo_field)(varchar) in mysql table. I want to increment this field value with a pre defined prefix.
For example my field first value is demo001. Now when ever new values inserted I want to increment the numeric numbers like demo002, demo003.
How can I do this with PHP.
try this -
//fetch data from table
$sql = $mysqli->query('select count(demo_field) as total,demo_field from tablename limit 1');
$res = $sql->fetch_assoc();
//generate string from existing data
$str = substr($res['demo_field'], 0, 4);
$dig = str_replace($str, '', $res['demo_field']);
$dig = $dig+$res['total'];
//add padding if needed
$dig = str_pad($dig, 3, '0', STR_PAD_LEFT);
//concatenate string & digits
$newStr = $str.$dig;
var_dump($newStr);
Another way without count
$sql = $mysqli->query('select max(demo_field) as demo_field from demo');
$res = $sql->fetch_assoc();
$str = substr($res['demo_field'], 0, 4);
$dig = str_replace($str, '', $res['demo_field']);
$dig += 1;
$dig = str_pad($dig, 3, '0', STR_PAD_LEFT);
$newStr = $str.$dig;
var_dump($newStr);
hope this might solve the problem with count.
another solution with max count for alphanumeric string and without padding -
$sql = $mysqli->query('select max(cast(substring(demo_field, 5) as unsigned)) as digit, demo_field from demo');
$res = $sql->fetch_assoc();
$str = substr($res['demo_field'], 0, 4);
$dig = $res['digit'] + 1;
$newStr = $str.$dig;
var_dump($newStr);
//use PHP not mysql
$pre = 'demo';
$num = 0;
define('MAX', 100);
for($num = 0; $num < MAX; $num++)
{
$pre_str = $pre . sprintf("%03d", $num);
//insert here
}
you have to use an INT field
and translate it to whatever format you want at "select" time.
In MySQL we cannot use AutoIncrement for Varchar.
I'm working right now to get a unique order number for the item being purchased.
The problem is such that I have payment gateway at quickpay and it comes up and says this: "Error in field: ordernumber"
I have tried to make it like this:
$idag = date("dmY");
$a = substr(str_shuffle('abcdefghijklmnopqrstuvwxyz#!*_-?>/+,ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), 0, 4);
$total = $idag . $a;
$sql = "SELECT pakker.pris FROM pakker WHERE pakker.id = 3";
if ($stmt = $this->mysqli->prepare($sql)) {
$stmt->execute();
$stmt->bind_result($pris);
while ($stmt->fetch()) {
$protocol = '7';
$msgtype = 'authorize';
$merchant = '89898978';
$language = 'da';
$ordernumber = $total;// here must order number came up.
I have also tried to do it like this.
$idag = date("dmY");
$rand = strtoupper(substr(uniqid(sha1(time())), 0,8));
$a = substr(str_shuffle('abcdefghijklmnopqrstuvwxyz#!*_-?>/+,ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), 0, 4);
$total = $idag . $a . $rand;
From the [API documentation for Quickpay]:
ordernumber
/^[a-zA-Z0-9]{4,20}$/
A value by merchant's own choice. Must be unique for each transaction. Usually an incrementing sequence. The value may be reflected in the your bank account list.
You need to alter your random order ID generator to exclude any non-alphanumeric characters.
i.e.
$idag = date("dmY");
$a = substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), 0, 4);
$total = $idag . $a;
if I were you, I'd use the unix timestamp and then add a few extra characters to the end.
I'm generating auto incremental code through a PHP script. The sequence goes like this, L001, L002....L039, L040. Below is the PHP code I have written. The comments show the output of each statement.
<?php
require_once("db_handler.php");
$conn = iniCon();
$db = selectDB($conn);
$query = "SELECT LID FROM locations ORDER BY LID DESC LIMIT 1";
$result = mysql_query($query, $conn);
$row = mysql_fetch_array($result);
$last_id = $row['LID']; //L040
$id_letter = substr($last_id, 0, 1); //L
$id_num = substr($last_id, 1) + 1; //040 + 1
$new_id = $id_letter . $id_num;
mysql_close($conn);
?>
I'm displaying the $new_id in a HTML textbox. though everything works fine up to this point, the new generated code shows as L41, cutting off the leading zero.
How can I stop this from happening? I need to have that zero in the front.
Thank you.
Use either str_pad or sprintf function:
$id_num = str_pad($id_num, 3, "0", STR_PAD_LEFT);
or
$id_num = sprintf("%03d", $id_num);
$id_num = substr($last_id, 1) + 1; //40 + 1
$id_num = str_pad($id_num, 3, "0", STR_PAD_LEFT); //return 041
$new_id = $id_letter . $id_num;
take a look at sprintf[1]. hint: %03d
[1] http://br2.php.net/sprintf
I have a PHP variable that looks a bit like this:
$id = "01922312";
I need to replace the last two or three numbers with another character. How can I go about doing this?
EDIT Sorry for the confusion, basically I have the variable above, and after I'm done processing it I'd like for it to look something like this:
$new = "01922xxx";
Try this:
$new = substr($id, 0, -3) . 'xxx';
Result:
01922xxx
You can use substr_replace to replace a substring.
$id = substr_replace($id, 'xxx', -3);
Reference:
http://php.net/substr-replace
function replaceCharsInNumber($num, $chars) {
return substr((string) $num, 0, -strlen($chars)) . $chars;
}
Usage:
$number = 5069695;
echo replaceCharsInNumber($number, 'xxx'); //5069xxx
See it in action here: http://codepad.org/XGyVQ1hk
Strings can be treated as arrays, with the characters being the keys:
$id = 1922312; // PHP converts 01922312 => 1 because of that leading zero. Either make it a string or remove the zero.
$id_str = strval($id);
for ($i = 0; $i < count($id_str); $i++)
{
print($id_str[$i]);
}
This should output your original number. Now to do stuff with it, treat it as a normal array:
$id_str[count($id_str) - 1] = 'x';
$id_str[count($id_str) - 2] = 'y';
$id_str[count($id_str) - 3] = 'z';
Hope this helps!
Just convert to string and replace...
$stringId = $id . '';
$stringId = substr($id, 0, -2) . 'XX';
We can replace specific characters in a string using preg_replace(). In my case, I want to replace 30 with 50 (keep the first two digits xx30), in the $start_time which is '1030'.
Solution:
$start_time = '1030';
$pattern = '/(?<=\d\d)30/';
$start_time = preg_replace($pattern, '50', $start_time);
//result: 1050