How to create unique number for each new post?

Client is selling antique furniture. Items don’t have names, so to make it easy to separate them (when visitor is calling about certain item), client wants each item to have unique number. And if some item is deleted, so that this number won’t be assigned again.

Also would be good to have easy numbers, like 5491, but not something like 8hYp15G

Do you know any easy solution to do that, which doesn’t require programming?

How else would you do it without programming it? You could base the product list on a spread sheet and use the virtual page features to generate them from that. Get excel to put a unique number in the first column that is readable. You can pull that number into a hidden field.

that way your client just needs to call out a row number in the spreadsheet, and you can create new ones just by updating the spreadsheet.

The auto id plugin, but don’t know if it works gor existing pages

I nearly suggested that but it generates IDs similar to 8hYp15G which isn’t as simple as requested.

@Roman, does the client already have an ID number for each item? Most stores keep some sort of inventory - and in their own inventory items usually need to have a unique number. If that is the case, the client might be able to just use that.

If not, then the client is actually asking you to create a numbering system for their stock. Sure, you can do that, but it will require some programming. You will need to do 2 things:

  1. generate a unique number
  2. insert that number automatically, where it’s needed - i.e., either the page title, or in a field, depending on where you are storing the item data on the site

Generating a RANDOM number is easy, but there is very little guarantee that the number you get ‘at random’ will actually be unique. In your example, if we only have the option of using 4 digits, that means that we have at least a 1-in-9999 chance that the number is already in use. This is why, when programmers are generating ‘unique’ ID numbers, they will usually use a. mixture of numbers and LETTERS, in order to increase the number of options. The AutoID Plugin allows you to get automatic, unique IDs for pages and fields, and makes it easy for you to insert these values where they are needed. But like most code that generates proper unique IDs, these IDs will be a long string made up of both numbers and letters…

If this type of solution is no good for you, then you may try to write your own ‘ID generator’. There are several built-in PHP functions that can help you generate ‘random’ values, and also a Kirby plugin called Random which can help you generate all kinds of random strings. You just have to remember, however, that ‘random’ does not mean ‘unique’, and that you then have to use certain techniques and cross-checks to ensure the uniqueness of the values you create.

Once you have your unique ID, inserting it where it’s needed is relatively easy with Kirby. If using a plugin like AutoID, you’ll have several different ways - all documented in the plugin docs. If you’re generating your own value, then you can use it as a default value in a field, have it automatically calculated in a page model, or even inject it into the page title via a hook - it all depends on where you’re storing your data, and how you’re using it.

I hope this info helps.

That’s true but as far as I know, you can adapt the scheme. Must admit that I haven’t checked yet.

There doesn’t seem to be a config option to alter the number, but does look like theres a function that can be tweaked to simplify it if you forked it.

I dont what side effect it will have but there is a function below it that uses it in an if else state, so that needs to be tweaked a little too. @bnomei Can you lend a hand here? How can we get a simpler number without hacking the plugin?

How about

  1. giving the site blueprint a number field to remember the current counter value and enter 1 or 1000 or any number to start from. Then disable the field.
  2. Give every Item-Blueprint a disabled number field, which will later carry the ID
  3. Create a onPageCreate:after hook and within that, you take the number from site, increase the value by +1, fill the new value into the newly created page and also update the site number field.

Now every newly created page has a “auto incrementing” ID. You could loop through all existing pages and assign consecutive numbers, before you start creating new pages. The number field could be used to sort pages, through that, non-assigned pages can easily be spotted.

As long as you always increase the centrally stored value, this system will assign every number only once. Make sure, that these values can not be changed by a user.

Probably not death-proof, but maybe a straight forward solution for not too complicated projects, with not too many users.

1 Like

Maybe we are overcomplicating it. The point is to make communication easier for your client. In the past with sites with lots of pages to manage, ive made a secret page the lists them all out in a table with the basics like title, status, etc listed in the table row. I made them link back to the panel edit pages and to the page on the front end.

You could simply number them and get your client to use this secret “master list” as a reference point. You could use the pages creation date for the number or something (although i think that might be a bit long). You just need some unique number that wont change. Just numbering them wont do it because when one gets deleted, the numbers of all the ones that come after it will change.

Well, yes, you can set your own generator in config via the generator option.

It looked to me from the source code like that config option was used as a seed, so you still end up with a complex number. There is a simpler number generator in the code but it is used as a fall back. Perhaps I misread / misunderstood the way it hangs together.

like @texnixe said. if you overwrite the generator option you should be fine.

you can cut some corners and get asimple hash fast if you use the getToken with different params

Why not using when the item was created ?

i think the questions was about having an int based id. otherwise any unique random string will do.