# Update field from php with update() and increment()

**URL:** <https://forum.getkirby.com/t/update-field-from-php-with-update-and-increment/9881>\
**Category:** Questions\
**Tags:** v2\
**Created:** [March 5, 2018, 1:58am UTC](https://forum.getkirby.com/t/update-field-from-php-with-update-and-increment/9881 "2018-03-05T01:58:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![JanStieler](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/janstieler/32/15642_2.png) [@JanStieler](https://forum.getkirby.com/u/JanStieler)\
**Post date:** [March 5, 2018, 1:58am UTC](https://forum.getkirby.com/t/update-field-from-php-with-update-and-increment/9881/1 "2018-03-05T01:58:31Z")

</div>

Hi,  
i try to generate file and update a field directly from a controllerfile for getting a automated invoicenumbering.  
The file is perfect generated but the increment and updating is not correct.

```auto
//Invoice Number generation and save
$invoiceNrFile = 'content/validation-registration/invoice-number/invoicenr.en.txt';

if(!file_exists($invoiceNrFile)){
    $invoiceNrContent = array('title' => 'Aktuelle Rechnungsnummer', 'invoicenumber' => '0000');
    $invoiceNr = $invoiceNrContent['invoicenumber'];
    $newInvoicePage = page('validation-registration')->children()->create('invoice-number', 'invoicenr', $invoiceNrContent);
}
else {
    $invoiceNr = page('validation-registration/invoice-number')->increment('invoicenumber', 1);
    $newInvoiceNr = page('validation-registration/invoice-number')->update(array('invoicenumber' => $invoiceNr), 'en');
}

```

With this I get a value `validation-registration/invoice-number`as new invoicenumber. What do I 'm wrong?

Cheers

---

<div class="post-metadata">

**Author:** ![flokosiol](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/flokosiol/32/468_2.png) [@flokosiol](https://forum.getkirby.com/u/flokosiol)\
**Post date:** [March 5, 2018, 5:18am UTC](https://forum.getkirby.com/t/update-field-from-php-with-update-and-increment/9881/2 "2018-03-05T05:18:35Z")

</div>

I’ve never used `increment()` by myself, but I think you don’t need to update the page afterwards. Have you tried to simply use

```auto
page('validation-registration/invoice-number')->increment('invoicenumber', 1);

```

If you need the new `invoicennumber` afterwards, you should get it as usual …

```auto
$newInvoiceNr = page('validation-registration/invoice-number')->invoicenumber();

```

[untested].

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [March 5, 2018, 7:01am UTC](https://forum.getkirby.com/t/update-field-from-php-with-update-and-increment/9881/3 "2018-03-05T07:01:59Z")

</div>

What is your default language? If English is the non-default language, your `increment()` function will only work in the default language, see this post for a solution: [$page-\>increment($field) in multilang environment](https://forum.getkirby.com/t/page-increment-field-in-multilang-environment/9658/4?u=texnixe)

---

<div class="post-metadata">

**Author:** ![JanStieler](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/janstieler/32/15642_2.png) [@JanStieler](https://forum.getkirby.com/u/JanStieler)\
**Post date:** [March 7, 2018, 3:57pm UTC](https://forum.getkirby.com/t/update-field-from-php-with-update-and-increment/9881/4 "2018-03-07T15:57:47Z")

</div>

Hi,  
english is my default language. But your method works fine but not with left-hand zeros.  
`0000`will increment to `1`and not to `0001`.  
Exist for this also a method solution?

Cheers.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [March 7, 2018, 4:03pm UTC](https://forum.getkirby.com/t/update-field-from-php-with-update-and-increment/9881/5 "2018-03-07T16:03:16Z")

</div>

No, but why do you need this? You can add leading zeros in your template. This is just a formatting thing, I don’t think it is required to actually save those numbers in the file.

---

<div class="post-metadata">

**Author:** ![JanStieler](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/janstieler/32/15642_2.png) [@JanStieler](https://forum.getkirby.com/u/JanStieler)\
**Post date:** [March 7, 2018, 4:05pm UTC](https://forum.getkirby.com/t/update-field-from-php-with-update-and-increment/9881/6 "2018-03-07T16:05:54Z")

</div>

I use this number for invoices. But yes I can add the zeros later.

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:29pm UTC](https://forum.getkirby.com/t/update-field-from-php-with-update-and-increment/9881/7 "2025-02-06T17:29:52Z")

</div>

Archived on February 6, 2025, 5:29pm UTC


