Auto increment in php

Hi guys i have an order form the form action is invoice.php

in invoice php i have

$orderno=“order0001”;

when the form is submitted it shows order no as order0001

how do i increment this so that when the order form is filled out again the order shows order0002 ?

Thanks

Well, normally, you use an “id” field in your database. Mark it as an “autoincrement” field.
Then, when you “insert” a new invoice, that field is incremented without code. And, when you display
it, you pull the id from the database when you need to and display it with an echo.

But, some further info is needed. Quite often, you want to restart the numbering of orders each year or month so you do not have a ton of invoices saved in your database. You would not want to save the “order” part of the number inside the database. Waste of space. Just save the number. If you want to keep all invoices forever, use the “id” field. But, if you delete invoices in the further, like after 10 years you might delete all that are that old, etc., you would not want to use the id ad they can start over where the
previous ones are deleted. It is best to think out of the box and come up with a solid database layout to be used for all things that may come your way. My first thought is what happens when you hit 10,000 orders?
Your code would not work as you show only four digits. Look at some invoice numbers when you order
things online and get an idea of how other stores handle them. I purchased some software the other day.
The invoice number was “10599952”. So, they apparently just use the index “id” of the order number and
probably never delete invoices. Since large-integers how billions, that is not an issue. In their invoice it
says something like “Invoice# 10599952”…
Just wanted to give you some other thoughts since you posted in learning-PHP section. Hope it helps!

Hi there,

I am not using a database

There how wold your app know the LAST order id#?

It has to be somewhere your current session knows about?

What happens to the form once submitted then? Where does it go? How do you reference it later after submission?

Well, everyone uses databases for invoices… Anyway, without one, you need to save the invoice number somewhere. You can write it to a text file and then read it back as needed. Here are some samples,
just off the top of my head that might work for you…

//  To read a text file and get number...
$file = 'invoice.txt';
$invoice_number = intval(file_get_contents ( $file ));   // read file data and make sure it is a number
$invoice_number = $invoice_number + 1;   //  Add one to it
file_put_contents( $file, $invoice_number );

This will get the previously saved number and increment it and save it back.
You would need to create the file with the first invoice number or add some error checking to see if
the file exists or not. If it doesn’t, you can save it with number 1.
Also, in the display of the value, you would need to do something loosely like this:
echo "Invoice# " . $invoice_number;
This would add the text at the beginning and the number right after it.
You also should use number formatting functions to turn the “1” into “0001”. That is easy, too.

Good luck… PS: Is this for a class?

thank you Ernie i will try this. Its just a sample project i am trying to learn from.

Cool! It’s easy to write and read from a text file. As you see above. Just you will get an error the first time it reads it if it does not exist. You can use the exists() function to test if the file is there or not. And, then, if not, just write a 1 to it.

Also, I think you can add zeros like this:

$orderno = “Order” . sprintf( ‘%04d’, $invoice_number );
This basically puts some text and concatenates a four digit number to it with padded zeros…

Thank you so much excellent works a treat.

You are very welcome! See ya in your next puzzle…

Hi Ernie another puzzle.

I have a web page that displays PDF files i have uploaded.

How do i display these files in order of date ?

Here is the code that i have on the page that views the PDF files.

{
//foreach (glob(“images/.jpg") as $large)
foreach (glob("
.pdf”) as $filename) {

//echo “$filename\n”;
//echo str_replace("","","$filename\n");

echo str_replace("","","$filename
");

}
}

Many Thanks

Well, that should be it’s own thread, but…

So, you have a webpage and a list of PDF files. If you use GLOB to get the names, they are not sorted.
But, if you use the scandir() function, you can tell it to sort ASC and then they will be all set.
Example:
// Set a folder where the files are stored. This example is in the folder named “pdf” under the root folder.
$dir = “/pdfs/”;

// Sort in ascending order - this is default
$files = scandir($dir);

// Sort in descending order
$files = scandir($dir,1);

Then, use the foreach($files as $file) { to parse thru them in the order you want.

That is based on names of course. To order them by date of creation, you can do something like this:
(This uses the GLOB() function)

$dir = "/pdfs/";
$files = glob($dir.*.swf');
usort($files, function($a, $b) {
    return filemtime($a) < filemtime($b);
});

Then, of course do the foreach to display them…
Hope this helps!

Sponsor our Newsletter | Privacy Policy | Terms of Service