Laravel crud decimal change dot to comma in the form - php

I am making a CRUD application, and in the Registrations table there is a price with a decimal that can be with a dot or a comma.
1: i want a validation for a decimal that it accepts dots and commas. for examlple: (9,50 0,50 0.50 9.50)
2:
What i want is because the database stores the prices with dots, i want when i try to edit the record in my application it shows the price but it replaces the dot with a comma.
I think i have to use the str_replace function. But i dont know where to use it. Because in the controller i already use str_replace to save it with a dot in the database. Maybe in the html form with jquery?
Any help would be much appriciated!

You may have a look at accessors: https://laravel.com/docs/6.x/eloquent-mutators#defining-an-accessor
Then just define the accessor in your model:
public function getPriceAttribute($value)
{
return str_replace('.', ',', $value);
}
You may define a mutator to make sure the price is always stored with a dot, even if user uses a comma:
public function setPriceAttribute($value)
{
$this->attributes['price'] = str_replace(',', '.', $value);
}
Now, each time you display the price with $registration->price it will be displayed with a comma, no matter where you use it (any controller, blade template, whatever).
And when assigning the price a value, if it contains a comma, it will be replaced by a dot, no matter where you set it.

if i get you well.I think number_format function will help you with this problem.
$num = 1999.9;
$formattedNum = number_format($num);
echo $formattedNum;
// this will return 2,000
$formattedNum = number_format($num, 2);
echo $formattedNum;
//this will return 1,999.90
However, in you instance, you can use the follow code
number_format($registration->price,2)
It will return the figure with two decimal places.
Hope it helps. You can read more on the following link https://www.w3schools.com/php/func_string_number_format.asp

Related

how to generate automatic transaction code with Laravel 7 with format : [code-date-sequence] LIKE: PSN-22102021-001

I want to generate a unique transaction code with using date an example: PSN-22102021-001 or PSN-22102021001, how to do it?
OK, not sure what database you are using, so will keep this in band.
I will assume the PSN is relevant to all codes, as you give no idea what this is.
Next, you will find generating unique IDs very difficult, and long, unless they are incremental in some way.
In PHP, you have two options (there are others, but I will let others fill those gaps). microtime(), or uniqid().
If you are using microtime(), you could use the following:
<?php
echo makeID();
function makeID($prefix = 'PRN'){
$time = str_replace('.', '-', microtime(true) );
return $prefix.'-'.$time;
}
This would return a number like: PRN-1634871496-0619
However, there is a very small possibility that two times are generated in the same microsecond, and this fails.
To ensure a unique ID, you would need to generate alpha-numeric options such as this..
<?php
echo makeUniqueId();
function makeUniqueId($prefix='PRN'){
return $prefix.'-'.uniqid();
}
This would return something like PRN-617229f798988

keep adding up code from database

# Check arrary not empty
if (!empty($results)) {
$this->code($results);
// got the mail code from database
// which is PG-000001
// how do i add , like something PG-000001 ++
}
this will return a result from database , my intention is to keep adding up the code that return from my database and the update back to the database.
now it was return PG-000001, how do i make it add up and be like PG-000002 and then update it and next time it will be PG-000002 and up to 000003 and so on.
how do i add up the text PG-000001?
If all of your codes look like this, then your really shouldn’t store them that way. It appears that the PG- at the beginning is just a prefix. If you store the actual value as an integer, you can increment as much as you like.
Anyway, the solution to your question is that you will need to
split the string
increment the second part
zero-pad the second part
combine again
Here is a little test script:
$test='PG-000001';
$pattern='/(.*-)(\d+)/';
preg_match($pattern,$test,$matches);
list(,$prefix,$value)=$matches;
$value=sprintf('%06d',$value+1);
$test="$prefix$value";
print $test;
Translation:
/(.*-)(\d+)/ is the pattern that will split the string into the prefix & numeral
preg_match applies the pattern and returns the result into the array $matches.
$matches has the original string, and then the two matches
list() copies elements of the array into variables. The leading comma skips the first element
sprintf formats the data. In this case, the code 0-pads to 6 digits
the double-quoted string is a simple way of recombining your data.

Is there a simple way to return WHAT is similar between 2 strings in PHP?

I've looked into the similar_text() and levenshtein() functions, but they only seem to return THAT there are similarities and the percentage of those similarities.
What I am trying to do is compare 2 strings to determine WHAT is actually similar between the two.
Basically:
<?php
$string1 = "IMG_1";
$string2 = "IMG_2";
echo CompareTheseStrings($string1,$string2); // returns "IMG_";
If this wonderful function doesn't exist, what would be the best way to accomplish this?
My end game plan is to read through a list of file names and then replace the similar text with something user defined or just remove it all together, but I don't want to replace each files unique identifier.
Reading your 'end goal' I think you're going about this completely the wrong way I think you should really be looking at str_replace
$string1 = "IMG_1";
$string2 = "IMG_2";
// you would create a loop here to insert each string
str_replace("IMG", "NEW_TERM", $string1);
If you want to remove the text altogether then just pass an empty string in as the 2nd parameter

PHP format number before insert into database

i'm using $_POSTto send form values to PHP and then insert into database, i have some inputs for prices values that looks like this:
1.000.000,00 (1 million in BRL "Brazilian Real")
I need to convert it to 1000000.00 (DECIMAL(10,2) to insert into database)
How can i do that using PHP number_format()?
Thanks!
EDIT
If number_format() is not the function i'm looking for, what's best to be using in this case?
Also, i need help finding a solution, even if the user value is 100.000,00
You can not do that with number format, it works other way around.
The thing that you do is bad practice, View layer should send primitive data to Controller - if you are using some advanced javascript component to represent to the user formatted number, that is fine, but underlaying form control should send primitive data, i.e. 10000000.00
Now, if nothing that I have stated to you is not applicable at this particular moment, and having in mind that this format that you have send is fixed (dot for thousand separator, coma for decimal separator), you can clean the value by using simple str_replace.
But, the trick is to replace first dot with empty string, then coma with dot.
str_replace(',', '.',str_replace('.', '', $number));
Got it?
But know that what you are doing is bad approach and wrong implementation, eventually, it will bite you in the a**.
<?php
$number = '1.000.000,00';
$replaced_number = str_replace(array('.',','), array('',''), $number);
echo number_format($replaced_number,2,'.','');
?>
The easiest way is to use str_replace() function:
<?php
$p = '1.000.000,00';
$p = str_replace('.', '', $p);
$p = str_replace(',', '.', $p);
echo $p;
At first replace the (.) and (,) with str_replace by '' and then use t the following function
number_format($replaced_number,$decimal)
If you have a look at php.net you will easily see the right syntax for your goal:
number_format($number,2,'.','')
The first parameter is the number of decimal places that in your case is 2.
The second is the symbol to use for decimal separator and the third is the one to be used for thousands that in your case will be nothing .

How I can split the student_no in codeigniter?

I want increase the student_no, when i click the finish button.
student_no : ABS10000
So i want to split the student_no and increase one. How?
Can i use character_limiter()?
$string1 = "ABS100000";
$string1 = character_limiter($string1, 2);
$string2 = "ABS100000";
$string2 = character_limiter($string2, 3,8);
$string = $string + 1;
echo $string1;
echo $string2;
This is correct...
You're using that helper function quite wrong. Also, your code makes little sense at some points.
character_limiter() belongs to the Text helper, it's aim is to provide help in formatting, mostly; look at the companion functions and you will see it.
Also, the third (optional) parameter should be a text appended at the end of the trimmed string, something such an ellipsis, for ex. Your second example, which tells to "limit the string to 3 characters and add and 8 at the end" is just plainly wrong, according to your original intentions.
I'm not aware of any helper function (here or in other helpers) that can do what you want, but since you're coding in php, I don't know why can't just do something like (I know there might be better ways to do this):
$string1 = str_replace('ABS','',"ABS100000"); // becomes 100000
$string1 = intval($string1) + 1; //100000 is cast to INT and then 1 is added;
$new_string = 'ABS'.$string1; //gives ABS100001;
I'm assuming 'ABS' is something fixed and you just want to increment the numerical part of this string.If not, please be more clear in you question and add the relevant informations.

Categories