Im making an insert record code in php then i need to make a auto code, or the user just can look at the prepared value of the textbox, Ill just use this code.
$ayos = mysql_fetch_array(mysql_query("select ib_code from item_brand order by ib_code desc limit 1"));
$new_val = $ayos['ib_code'] + 1;
when you display it just use:
echo $ayos['ib_code'];
for example the output is 006. the thing i cant resolve was when i try to display value of $new_val in the textbox, for sure our expected result if the $ayos['ib_code'] = 006 then the $new_val is equal to 007, but the problem is it is only 7 not 007, when displaying inside the textbox.. thx
Try
if you want to display a number left-padded with zeros, consider using printf() (or one of its numerous relatives).
Tested Example:
<?php
$a = '006';
$b = $a + 1;
?>
<input type="text" value="<?php printf("%03d", $b); ?>" />
Above code is tested.
sprintf() can do that:
echo sprintf('%03d', $ayos['ib_code']);
Or use str_pad() - but that's a little bit longer in code:
echo str_pad($ayos['ib_code'], 3, 0, STR_PAD_LEFT);
Response to Some User Comment
well if there are value like 0006 or 000006 then we can use above code is as below.
<?php
$a = '000006';
$b = $a + 1;
?>
<input type="text" value="<?php printf("%0".strlen($a)."d", $b); ?>" />
what i am doing is passing length dynamically so if in future number size increase code will still work as expected.
Using the + operator forces PHP to interpret the variable as a number, which will truncate any leading zeroes. You'll have to do some interesting string manipulation most likely.
The 006 ins converted to number and after that it adds 1 to it, It becomes 7 , When it becomes number it truncates zeros from left side.Use below code, it will pad 0 to left.
echo str_pad($ayos['ib_code'], 3, "0", STR_PAD_LEFT); //becomes 007
Use $realnumber = intval($new_val);
echo $realnumber;
Assign $new_val variable like below:
//GET count of leading 0's into variable
$leading_zero_count = strspn($ayos['ib_code'], "0");
$new_val = $ayos['ib_code']+1;
/**
* Pad the $new_val variable with 0's so that
* new length = total leading 0's + new length of $new_val
**/
$new_val = str_pad ($new_val, $leading_zero_count+strlen($new_val),0, STR_PAD_LEFT);
Related
I doubt if it is possible but I'm looking for the following:
E.g. $number's value is 1, can I get the next number, in this case 2, to be the value of another variable, e.g. $newnumber?
I prefer to do this in SQLite, so the numbers are stored in a database.
Try: $newnumber = ((int) ($number)) + 1, if this is for a primary key though just set the column to auto increment
$newnumber=$number+1;
I think it can't get more simpler than that.
Without knowing the larger scope of what you're trying to accomplish, my suggestion would be to use the increment operator in PHP.
Original answer:
Something like:
$number = 1;
$newNumber = $number++;
Correct answer (above gives wrong result):
$number = 1;
$number++;
$newNumber = $number;
From there you can do whatever you want with the second variable.
Hi i need to save a 010 number in $number and if i do like this php will remove the starting 0
$number = 010
And echo of this will return 10 how can i make it not to remove the initial 0
BR
Martin
Use it as a String:
$number = '010';
Use str_pad() function.
echo str_pad('10',3,'0',STR_PAD_LEFT)
http://php.net/manual/en/function.str-pad.php
Do remember that numbers starting with 0 can also be treated as octal number notation by the PHP compiler, hence if you want to work with decimal numbers, simply use:
$num = '010';
This way the number is saved, can be stored in the database and manipulated like any other number. (Thx to the fact that PHP is very loosely typed language.)
Another method to use would be:
Save number as $num = 10;
Later while printing the value you can use sprintf, like:
sprintf("%03d", $i);
This will print your number in 3 digit format, hence 0 will be added automatically.
Another method:
<?php
$num = 10;
$zerofill = 3;
echo str_pad($num, $zerofill, "0", STR_PAD_LEFT);
/* Returns the wanted result of '010' */
?>
You can have a look at the various options available to you and make a decision. Each of the method given above will give you a correct output.
I have a weird question here. How can I do this.
So I have a decimal number 1.2; I would like to replace the decimal point with an add sign so that it would be 1+2 and output the value which is 3.
This is what I have tried so far.
replace the dot using str_replace
<?php
$a = 1.2;
$added_decimal = str_replace('.','+',$a);
echo $added_decimal;
The above code outputs 1+2, so it does not compute it.
I also tried converting the decimal to an array like so;
<?php
$a = 1 .'.'. 2; //Concatenated it
explode('.',$a); //entered the delimeter
echo $a[0] + $a[2]; //this outputs 3;
So this one works fine, but then the problem would be when $a was 1.20. This would also output 3 if my code above was use. How would I go about getting 1+ 20 and output 21?
Your second code snippet is almost correct. You just need to store the explode() result in a variable. See this code snippet :
<?php
$a = 1 .'.'. 20; //Concatenated it
$x = explode('.',$a); //entered the delimeter
echo $x[0] + $x[1]; //this outputs 21;
When you do $a[0], you actually access the index of the string not the explode() result.
Hello I hope someone could help me cus Iam little bit confused about task I have to do in PHP
I need php file that is unique registration ID with these parameters:
First is AA00001 and next one is DF00002.
So first letter + 3 and second + 5, but numbers going in +1 order.
Could someone give me hint how to achieve this?
Thank you!
In pseudocode:
get previous ID
separate first letter, second letter and number
convert first letter to number, add 3, modulo 26, convert back to letter
convert second letter to number, add 5, modulo 26, convert back to letter
add 1 to number, add zero-padding to reach 5 digits
concatenate them all together
set this as the new "previous ID"
Note that you'll need to ensure that this happens atomically - i.e. that you don't have multiple processes working on the same ID, else they'd get the same "next" ID. This will IMHO be the hardest part.
You can use substr to split the ID, dechex and hexdec to convert to/from decimal to hexadecimal, which gives you the A+3=D part, and you can use str_pad to front pad the integer with zeros, which gives you the second part, and then you just concatenate them.
ETA: Something like this:
$id = 'AA00001';
$first = dechex((hexdec(substr($id,0,1))+3)%16);
$secnd = dechex((hexdec(substr($id,1,1))+5)%16);
$int = str_pad(substr($id,2)+1,5,"0",STR_PAD_LEFT);
$newid = strtoupper($first.$secnd.$int);
ETA2: Unless you meant to go AA00001, DF00002, GK00003, JP00004, MU00005, PZ00006, SE00007 etc in which case you need
$first = chr(((ord(substr($id,0,1))-62)%26)+65);
$secnd = chr(((ord(substr($id,1,1))-60)%26)+65);
$lastid = 'AA00001';
$first = substr($lastid, 0, 1);
$second = substr($lastid, 1, 1);
$numeric = substr($lastid, 2);
$next_first = chr(((ord($first) - ord('A') + 3) % 26) + ord('A'));
$next_second = chr(((ord($second) - ord('A') + 5) % 26) + ord('A'));
$next_numeric = sprintf('%05d', intval($numeric) + 1);
$new_id = $next_first . $next_second . $next_numeric;
// DF00002
First you have to parse the last reg id.(using substr) Then,
store each value in a var corresponding to the place
$first , $second, $numberpart. then
$first = ($first + 3 ) % 16;
$second =($first + 5 ) % 16;
$number = $number + 1;
THen update the record accordingly converting$first, $second to their appro. letters.
How do I output a value as a number in php? I suspect I have a php value but it is outputting as text and not as a number.
Thanks
Here is the code - Updated for David from question below
<?php
if (preg_match('/\-(\d+)\.asp$/', $pagename1, $a))
{
$pageNumber = $a[1];}
else
{ // failed to match number from URL}
}
?>
If I call it in: This code it does not seem to work.
$maxRows_rs_datareviews = 10;
$pageNum_rs_datareviews = $pagename1; <<<<<------ This is where I want to use it.
if (isset($_GET['pageNum_rs_datareviews'])) {
$pageNum_rs_datareviews = $_GET['pageNum_rs_datareviews'];
}
If I make page name a static number like 3 the code works, if I use $pagename1 it does not, this gives me the idea $pagename1 is not seen as a number?
My stupidity!!!! - I used $pagename1 instead of pageNumber
What kind of number? An integer, decimal, float, something else?
Probably the easiest method is to use printf(), eg
printf('The number %d is an integer', $number);
printf('The number %0.2f has two decimal places', $number);
This might be blindingly obvious but it looks like you want to use
$pageNum_rs_datareviews = $pageNumber;
and not
$pageNum_rs_datareviews = $pagename1;
echo (int)$number; // integer 123
echo (float)$number; // float 123.45
would be the easiest
I prefer to use number_format:
echo number_format(56.30124355436,2).'%'; // 56.30%
echo number_format(56.30124355436,0).'%'; // 56%
$num = 5;
echo $num;
Any output is text, since it's output. It doesn't matter what the type of what you're outputting is, since the human eye will see it as text. It's how you actually treat is in the code is what matters.
Converting (casting) a string to a number is different. You can do stuff like:
$num = (int) $string;
$num = intval($string);
Googling php string to number should give you a beautiful array of choices.
Edit: To scrape a number from something, you can use preg_match('/\d+/', $string, $number). $number will now contain all numbers in $string.