Keep Decimal Place When Importing CSV to MySQL - php

I'm importing a CSV directly into my database via PHPmyadmin. Everything has gone smoothly until I checked a column that has decimal spaces. Instead of keeping the decimal places in tact it simply removed everything after the ', & .'.
I tried removing the separators before uploading and then it just shows the full number i.e. 125657458 and doesn't add in the decimals.
I have set the Datatype to 'DECIMAL' for the column.
Row From CSV:
Ricky Manager Standard Normal 156,200
Results with decimal place in-tact on upload:
Ricky Manager Standard Normal 156
Results with decimal place not present on upload:
Ricky Manager Standard Normal 156200

DECIMAL(10,0) says to round to an integer. So, 156.200 becomes 156 and 156.9 becomes 160. Do you have any examples to verify the latter?
I notice you used "," as the decimal point; have you configure things to the call "," a decimal point and "." a 'thousands separator'? That is not the default.
DECIMAL(10,3) will take in 156.789 exactly.
No datatype will accept input that has a 'thousands separator'. That is, nothing will correctly accept 123,4567,789.12 (English) or 123.456.789,12 (some European) or 12,34,567.89 (Indian).

Related

Is it possible to read a certain number of characters after a certain number of digits from the start of the file

I'm setting up a web app, where users can choose the starting point and the number of characters to read from a text file containing 1 billion digits of pi.
I have looked, but I can't find any similar problems. Because I don't know what the starting digit is, I can't use other solutions.
Here is the function written in Python:
def pi(left : int, right : int):
f.seek(left+1)
return f.read(right)
For example, entering 700 as the starting point and 9 as the number of characters should return "Pi(700,9): 542019956".
Use fseek to move the file pointer to the position you need, and fread to read the amount of characters you need - just like your Python sample code.
Actually, this capability is built in to file_get_contents.
$substr = file_get_contents('pi_file.txt', false, null, 700, 9);
A handy feature of that function that I learned about just now after using it for the past 7 years.

How to use $agi->get_data with multiple audio files

In Playback, Background, Read we can combine audio files with &, like file1&file2.
In PHPAgi instead of Read, I use $agi->get_data, but its look like accept only one file!
I also tried to play audio with Background before calling get_data:
$multipleAudioPath = 'file1&file2';
$agi->exec('Background', $multipleAudioPath);
$rawInput = $agi->get_data('blankAudioFile', $timeout, $digits);
But first character goes for breaking Background audio and didn't catch in get_data.
What can I do? Can I do it alone with $agi->get_data ? Or is there any other solution ?
You can use multiple files using Read asterisk command.
$agi->exec("Read","variable,filename&filename2")
[Syntax]
Read(variable[,filename[&filename2[&...]][,maxdigits[,options[,attempts[,timeout]]]]])
[Arguments]
variable
The input digits will be stored in the given <variable> name.
filename
file(s) to play before reading digits or tone with option i
maxdigits
Maximum acceptable number of digits. Stops reading after <maxdigits> have
been entered (without requiring the user to press the '#' key).
Defaults to '0' - no limit - wait for the user press the '#' key. Any value
below '0' means the same. Max accepted value is '255'.
options
s: to return immediately if the line is not up.
i: to play filename as an indication tone from your "indications.conf".
n: to read digits even if the line is not up.
attempts
If greater than '1', that many <attempts> will be made in the event no data
is entered.
timeout
The number of seconds to wait for a digit response. If greater than '0',
that value will override the default timeout. Can be floating point.
[See Also]
SendDTMF()
Other option is mix it together by SOX or via libsox.

Force string data type when writing to CSV file

In PHP, is there a way to force the value "00123" to be inserted into a CSV file as a string?
This way, when you open the CSV file the value will remain 00123 rather than removing the leading zeros and showing 123.
The primary reason I'd like achieve this is for a list of zipcodes, whereas there are multiple zipcodes that have leading zeros and I'd like the values to reflect that.
<?php
if( $fh = fopen('filename.csv','w') ){
$line = ['00123'];
fputcsv($fh,$line);
fclose($fh);
}
CSV does not have types. Values written using the ,"..", syntax merely delimit the value to disambiguate the usage of , within the value itself; it does not mean that the value is "a string".
I suspect your values are mangled when imported into Excel or such. There's no solution to this that CSV can offer; you can only import the file using the import wizard and specify that the column should be used as is and not cast to a number. (This may or may not actually work depending on what effed-up version of Excel you're using.)
If you don't want to go through this every time, you should be producing an XLSX file, which does have types.
I guess there is no way to do it because "CSV" files are just "Comma-Separated Values"
You have to use the editor options for csv import.

Creating a code128 barcode in PHP, using a font instead of rendering it as an image

I need to be able to convert any string into a code128 barcode that can be printed on a PDF.
My idea was to embed a code128 font into the PDF and simply use that font to render the string that I need to show as a barcode, letter for letter.
However, I found out that I also need to calculate a checksum and include the start and stop characters.
Is this not possible using PHP? I have not found any solution anywhere. The only solutions that I could find are for directly rendering the barcode as an image, which does not help in my current situation, since I need to use a font to create the barcode in the PDF.
If you restrict your efforts to 128B, you have access to upper and lower case characters, numbers and most punctuation. It also saves you from having to write code to shift in and out of A and C symbologies. This makes the code to calculate the checksum really trivial.
Code 128B start character has a value of 104. The stop character for all Code 128 variations has a value of 106, but that value does not figure into the chksum calculation. Let's pick the string "Hello" for a real life exercise. You'll want to make sure you have access to the Code 128 table. All the values I will be discussing are out of that table and not ASCII or UTF-8.
The checksum is calculated by adding the multiple of a character’s value by its position in the barcode with the exception of the start code.
While the start code’s position is ‘1’, so is the position of the first character following the start code. So the start code and the first byte of data (‘H’) are both multiplied by the number 1 ((104 × 1) + (40 × 1) = 144).
The following 4 bytes get an incrementally higher multiplier ((69 x 2) + (76 × 3) + (76 × 4) + (79 × 5) = 1065). Summing it all up together (144 + 1065) we get 1209.
You should be able to use the modulus operator (1209 % 103) in PHP to get 76 for the checksum character for the Code 128B string “Hello” (the 103 is a constant, trust me on that). So the final array of codes to map "Hello" into a Code 128 barcode is:
[104 40 69 76 76 79 76 106].
You'll need lookup tables to convert the string character values to Code 128B character values to whatever your barcode font is expecting. But all you need is a loop, an array index, an accumulator for the sum of the factors and a modulus operator against the constant value of 103.
The easisest way to generate a barcode in PHP for PDF is to use a dedicated software library.
AFAIK, the most complete one is currently the tc-lib-barcode (https://github.com/tecnickcom/tc-lib-barcode) that allows you to generate both linear and bidimensional barcodes. The included example should give you a quick start.
The source code is fully PSR-2 compliant and can be easily added to your PHP projects using Composer.
The original code has been ported and refactored from TCPDF and already used in billions of documents.

Always display specified number of decimal places in Excel

I need to display two decimal places in Excel (xlsx). I'm using PHPExcel. In PHP, I can use something like the following to display only the specified number of decimal paces.
echo sprintf("%0.2f", $row['price']);
which always displays two decimal places even though the value contains no decimal point i.e if the value of $row['price'] is 1500, it will echo 1500.00. I need to write the same value to excel. I tried
$sheet->setCellValue("A1", sprintf("%0.2f", $row['price']));
but it displays 1500 instead of displaying 1500.00 (because Excel automatically assumes it to be a numeric value and the part after the decimal point i.e .00 in this case is truncated).
I also tried the following
$sheet->getStyle("A1")->getNumberFormat()->setFormatCode('0.00');
but it's not sufficient to achieve what is specified.
How can I (always) display the specified number of decimal places in Excel using PHPExcel?
$sheet->getStyle("A1")->getNumberFormat()->setFormatCode('0.00');
should work, as long as the value in cell A1 is a number and not a formatted string... don't try to force Excel formatting by using sprintf, simply use the format codes.
Formatted as number with comma. Assuming that value is not exceeded by 9 digits.
$sheet->getStyle("A1")->getNumberFormat()->setFormatCode('###,###,###.##');
You can also do this:
$sheet->setCellValue("A1",sprintf("%0.2f",$row['price']),true)->getStyle()->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00);
Adding 'true' at setCellValue, you refer directly to the cell instead of the worksheet and you can call the getStyle method without the cell value.
If you want some special characters just use html_entity_decode:
$currencyFormat = html_entity_decode("€ 0,0.00",ENT_QUOTES,'UTF-8');
$objPHPExcel->getActiveSheet()
->getStyle("A1")
->getNumberFormat()->setFormatCode($currencyFormat);
$objPHPExcel->getActiveSheet()->getStyle('ce')->getNumberFormat()->setFormatCode('0.00');
Try that:
$sheet->getStyle("A1")->getNumberFormat()->setFormatCode('### ### ### ##0.00');
it works.

Categories