I currently have a php loop running exactly how I need it with proper validations (in both php and javascript) with one exception, if the month is less than 2 digits, (i.e. 1,2,3,4), I need for a '0' to appear before:
01 - January
02 - February
...
10 - October
My code for the loop is currently:
<select name="Month">
<option value="">Month</option>
<?php
for ($i=1; $i<=12; $i++)
{
echo "<option value='$i'";
if ($fields["Month"] == $i)
echo " selected";
echo ">$i</option>";
}
?>
</select>
Also note, this month date is being stored in session, not interested in printing to screen
Try this when outputting the month:
sprintf("%02d", $month); // 01, 02 .. 09, 10, 11...
Use sprintf($format, [$var, [$var...).
Here, have some code:
function padLeft($char, $s, $n) {
return sprintf("%" . $char . $n . "d", $s);
}
function padWithZeros($s, $max_length) {
return padLeft('0', $s, $max_length);
}
Related
The invoice number looks like this INV{Year}{Month}-1A Ex: INV202201-1A
The suffix 1A's numerical value can only go up to 9. Then the Alphabet should change to B, then the numerical value again goes to 9B then has to change to C & so on. Also when the month changes the prefix should change back to 1A. I've already tried adding all alphabets to an array but I can't seem to figure it out.
I would really appreciate it if someone can point me out the right logic for this.
The total of 1A to 9Z is 234.
The data for $usedPostfix should be from the database or stored somewhere else.
$usedPostfix = array(
"202112" => 220,
"202201" => 10,
"202202" => 0,
);
This is where we find the unused postfix to be returned.
$date = date_format(new DateTime(), 'Ym');
function getInvoice($counter, $date)
{
foreach (range('A', 'Z') as $char) {
for ($i = 1; $i < 10; $i++) {
if ($counter > 0) {
$counter--;
continue;
}
return "INV" . $date . "-" . $i . $char;
}
}
}
$date is using Ym format. (e.g: 202201)
Examples:
Using $date
echo getInvoice($usedPostfix[$date], $date); // OUTPUT: INV202201-2B
Using custom $date (* for example purpose)
echo getInvoice($usedPostfix["202112"], $date); // OUTPUT: INV202201-5Y
echo getInvoice($usedPostfix["202202"], $date); // OUTPUT: INV202201-1A
My textarea ( $_POST['data'] ) contains 10 strings, each separated by a new line (\n). For example:
January
February
March
April
May
Jun
July
August
September
November
In PHP, how can I select only the first 5 elements from this $_POST['data']?
I tried:
$_POST['data'] = array_slice(explode("\n", $_POST['data']), 0, 5);
but it doesn't seem to work..
Try
<?php
if (isset($_POST)){
$str = $_POST['data'];
$lines=explode("\n", $str);
for($i = 0; $i < 5; ++$i) {
echo $lines[$i];//just get the list
echo "$lines[$i]"."<br>";//break the lines with br
echo "$lines[$i]"."\n";//break the lines with nr
}
}
?>
How to always show less than 2 position int number using php ?
This below code
<?PHP
for($i=0;$i<=100;$i++)
{
echo $i."<BR>";
}
?>
result will be like this
0
1
2
3
4
5
6
7
8
9
10
.
.
.
100
I want to always show less than 2 position int number. like this how can i apply my php code ?
01
02
03
04
05
06
07
08
09
10
.
.
.
100
Just paste the lines inside your loop
if ($i < 10) {
$i= str_pad($i, 2, "0", STR_PAD_LEFT);
}
And print $i.
I don't know for sure if this works, but you can try it like this:
for($i=0;$i<=100;$i++)
{
if($i < 10) {
$i = "0$i";
echo $i;
}
else {
echo $i."<BR>";
}
}
You can use the function sprintf or the function str_pad like this ...
<?PHP
for ($i = 0; $i <= 100; $i++)
{
echo sprintf('%02d', $i) . "<BR>";
}
?>
... or this ...
<?PHP
for ($i = 0; $i <= 100; $i++)
{
echo str_pad($i, 2, '0', STR_PAD_LEFT) . "<BR>";
}
?>
Credits: https://stackoverflow.com/a/1699980/5755166
You could checkout the sprintf function that allows you to format the output http://php.net/manual/en/function.sprintf.php
Something like this perhaps
echo sprintf("%'.02d\n", 1);
You can use str_pad for adding 0's:
str_pad($var, 2, '0', STR_PAD_LEFT);
The 0 will not be added if the length is greater or equal 2.
I have a standard form array in PHP like this. I need to account for up to 10 hours of labor in 15 minute increments with 10 as a value for every 15 min or 40 per hour. Is it possible to automate this into some sort of array with PHP instead of hardcoding each of these values? It just seems like there should be a better way and I have no idea how to start?
<select size="1" name="labor" id="labor">
<option value="80">2 Hours</option>
<option value="70">1 Hour 45 min.</option>
<option value="60">1 Hour 30 min.</option>
<option value="50">1 Hour 15 min.</option>
<option value="40">1 Hour</option>
<option value="30">45 Minutes</option>
<option value="20">30 Minutes</option>
<option value="10">15 Minutes</option>
</select>
Probably easiest to hardcode the solution. I think this is one of those situations where it is OK as #Wrikken mentioned, as it makes the code very clean and easy to maintain (Imagine coming back to this in a year or two). In addition this situation can also be handled very well with a database table.
First use an array to store you values and descriptions:
$list = array();
$list['10'] = '15 Minutes';
....
Then loop through the entire array to generate your dropdown:
<select size="1" name="labor" id="labor">
<?php
foreach($list as $value => $desc){
$option = "<option value=$value>" . $desc . "</option>";
echo $option;
}
?>
</select>
You need two values, the 10-units increment and the user-displayed value. Run a for loop over $i (or some other variable) from 1 to 40 and calculate your values inside the loop:
10-increment is easy, just multiply $i by ten.
For the displayed value, multiply by 15 to get the total amount of minutes. Then transform that into hours and minutes using the modulo operator (which gives you the minutes) and standard divison (to get the hours).
This should do it. Change the start number of $i to how many total minutes the dropdown should contain. It´s now set to 750 minutes.
echo '<select>';
for ($i = 750; $i >= 15; $i -= 15) {
$hours = $i / 60;
$min = $i % 60;
echo '<option>';
if ($hours >= 1)
echo floor($hours)." Hours ";
if ($min > 1)
echo $min." Minutes";
echo '</option>';
}
echo '</select>';
NOTE: This code is not perfect, the start number needs to be evenly divided with 15 to generate your desired result, (however, it works).
Something like:
<select size="1" name="labor" id="labor">
<?php
for ($x = 1; $x < 11; $x++) {
echo '<option value="';
echo $x*10;
echo '">';
echo $x*15;
echo " Minutes</option>";
}
?>
</select>
Throw in an if statement there to seperate it so that once (x/10*15)>60 it starts carrying over into hours instead of having 75/90/105/120 minutes.
I have some files i want to upload. And when i upload them, i want the first to be numbered 00, the second to be numbered 01. I wrote a function to get the newest number, but of course, this ain't workin
$i = 00;
while($i < $filecount)
{
if(!file_exists("../photo/$i.jpg"))
{
return $i;
print($i);
}
else if(file_exists("../photo/$i.jpg")) {
$i = $i + 01;
}
But now, when i upload a file, it just numbers it: 0, 1, 2, 3.
I got some new code :
function getFileNumber() {
$i = 0;
while ($i < $filecount) {
$fn = sprintf("../photo/%02d.jpg", $i);
if (!file_exists($fn)) {
return $i;
} else {
$i ++;
}
}
}
But it returns 00 every time, but i want to to go 00,01,02,03. Somebody any ideas?
Integer literals don't contain any formatting. 1 is the value one, it can't be "01", that's a formatting problem. As such, format your numbers:
$file = sprintf('%2u.jpg', $i);
You can use
echo str_pad($i, 2, "0", STR_PAD_LEFT);
Additionally, your print() after the return will never be executed and use if/else instead of if/elseif.
Check out sprintf() that allows for pretty flexible string formatting - especially check out the padding and width specifiers. You're looking for something along the lines of
$i = 0;
while ($i < $filecount)
{
$fn = sprintf("../photo/%02d.jpg", $i);
if (!file_exists($fn))
{
return $i;
}
else {
$i++;
}
}
print($i) is a dead code - it will never get executed
Second, you don't need to check else if(file_exists("../photo/$i.jpg")) again.
00 is not valid integer, it's simplified to just 0, as 01 simplified to 1 (actually it's treated as octal digits, so 08 and 09 is invalid)
So, to have the real 00, 01, 02 you'll have to format your output with spritf
$q=0;
$file_suffix = sprintf("%02d", $q);
if(!file_exists("../photo/{$file_suffix}.jpg")){
print($i);
return $i;
}
else{
$q+=1;
}