Martinus Posted September 16, 2022 Share Posted September 16, 2022 Is there any information about updating a field value at viewing the page, with a name or number? I looked in the docs and cheatsheet and read about $page->set("field", $value); $field->save(); but have no clue on how to implement this. I cannot find any examples either. Found this code just now: echo "old" . $page->numbers; $p = $page->numbers; ++$p; echo "new " . $p; $numbers = $p; $page->save($numbers); But is not updating the field ? Link to comment Share on other sites More sharing options...
iank Posted September 16, 2022 Share Posted September 16, 2022 If you're saving a field's value to a page you need to set output formatting off first. However, to quickly save a field's new value you can use setAndSave(), which doesn't have this requirement: $oldValue = $page->numbers; $page->setAndSave('numbers', $oldValue++); Link to comment Share on other sites More sharing options...
virtualgadjo Posted September 16, 2022 Share Posted September 16, 2022 hi, just to explain what @iank said, first of all your $page->save($number) couldn't work because what is number, where to save it and so on ? to do it the way you were going to, you would have to write $number = $page->number != '' ? $page->number: 0; echo $number; $number++; echo '<br />new' . $number; $page->of(false); $page->number = $number; $page->save(); and, as you can see @iank solution is faster and, quite often, when coding the shorter, the better ? have a nice day 1 Link to comment Share on other sites More sharing options...
Martinus Posted September 16, 2022 Author Share Posted September 16, 2022 Thanks guys. It worked. I stick with @iank solution then. Just two lines of code. But I still think it is difficult to find information like this in the API, and although the forum people are friendly and all - searching in the forum is complicated. Link to comment Share on other sites More sharing options...
zoeck Posted September 16, 2022 Share Posted September 16, 2022 33 minutes ago, Martinus said: But I still think it is difficult to find information like this in the API Really? I think the api search function is good ? Just search for „save field value“ and the First result is setAndSave…https://processwire.com/search/?q=save+field+value&t=APIhttps://processwire.com/api/ref/page/set-and-save/ Link to comment Share on other sites More sharing options...
Martinus Posted September 16, 2022 Author Share Posted September 16, 2022 I was searching for 'update field' 'updating field values' etc. In my mind, I had to 'update' the field, since it was already there. But looking at $page->field and $page->save() - were it says: $pages->saveField($page, $field) was the only thing coming close. Maybe it just does not work ? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now