Answers.Body: <p>To be able to do this you need to create a custom module in your Craft.
  Usually when you install craft, craft already include an example of Module inside the modules folder, you create you own but let use this first for example.</p>
  <p>Now let's check the config/app.php, and you will see something like this :</p>
  <pre><code>'modules' =&gt; [
      'my-module' =&gt; \modules\Module::class,
  ],
  </code></pre>
  <p>This means you module is registered and the module id is <code>my-module</code>. So the next step is you need controller to call your code, then go to <code>modules</code> folder create a folder called <code>controllers</code>, then inside it create a class something like this TestController, to be note the class name must use suffix <code>Controller</code>. So inside your TestController should be like this :</p>
  <pre><code>class TestController extends Controller
  {
    public function actionTest() {
  
     }
  }
  </code></pre>
  <p>So in here we can put our code inside the actionTest function. To save element, You can do it by calling saveElement from Craft and pass the entry element, it should be like this :</p>
  <pre><code>$entry = Entry::findOne($id);
  $entry-&gt;setFieldValue('priceProduct', $price);
  Craft::$app-&gt;elements-&gt;saveElement($entry);
  </code></pre>
  <p>so the full code can be like this :</p>
  <pre><code>class TestController extends Controller
  {
    public function actionTest() {
        $id = 2;
        $price = '100';
        $entry = Entry::findOne($id);
        $entry-&gt;setFieldValue('priceProduct', $price);
        Craft::$app-&gt;elements-&gt;saveElement($entry);
     }
  }
  </code></pre>
  <p>Then lastly you need to register the route for your controller, so go to config/routes.php put something like this :</p>
  <pre><code>return [
      'test' =&gt; 'my-module/test/test',
  ];
  </code></pre>
  <p>The explanation for the path : my-module is our module id that we register inside the app.php, then first test segment based on the class name so our controller class is TestController, and the last test segment is based on function name (actionTest).</p>
  <p>After that try to call it by <code>yousiteurl/test</code>.
  For the full explanation how to create controller and module you can go here.
  <a href="https://craftcms.com/docs/4.x/extend/controllers.html#request-validation-methods" rel="nofollow noreferrer">https://craftcms.com/docs/4.x/extend/controllers.html#request-validation-methods</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-09-09T17:05:28.607Z
Answers.Text: I tried to paste this code into a file price.php but nothing happened. Why is that? Don't judge strictly, I'm new to CRAFT CMS
Answers.UserDisplayName: Dimi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-09-11T00:50:55.483Z
Answers.Text: @Dimi, can you show us your section field configuration. There probably an error on validation. The return of craft save element is true or false, if you want to check if the save is success or not.
Answers.UserDisplayName: aodihis
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-11T20:12:40.573Z
Answers.Text: The field I am trying to update has the type "Plain Text Fields". Nothing supernatural. Maybe it won't be possible to launch and execute it just like that in Craft CMS? Maybe it is necessary to create a module or plugin for these purposes?
Answers.UserDisplayName: Dimi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-11T20:54:00.733Z
Answers.Text: I forgot to clarify. I use Craft CMS 4.2.5.2
Answers.UserDisplayName: Dimi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-11T20:57:41.220Z
Answers.Text: Maybe we should use something like this at the beginning of the file price.php :
  usa Craft;
  use craft\elements\Entry;
Answers.UserDisplayName: Dimi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-11T21:54:50.160Z
Answers.Text: Forgive me for my possibly stupid questions. I'm new to this CMS
Answers.UserDisplayName: Dimi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-13T06:01:06.297Z
Answers.Text: @Dimi, yes you need to create a module for these purpose, you cannot call the php directly on Craft. So you need to create module and put a controller inside them to run that script.
Answers.UserDisplayName: aodihis
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-13T22:40:07.013Z
Answers.Text: Could you provide some easy example on creating a module in craft CMS. Some very simple example. I just can't imagine how to run the code I need on a separate url in this content management system. It will be very useful for me) I really want to learn how to work in this system
Answers.UserDisplayName: Dimi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-14T03:58:05.843Z
Answers.Text: @Dimi, sure I edited the answer.
Answers.UserDisplayName: aodihis
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-16T22:33:23.893Z
Answers.Text: Detailed answer) But the first time, of course, nothing started. Here's what I did:
  1. Went to the modules folder
  2. Created the controllers folder there
  3. In the controllers folder, I created a file TestController.php and put the controller class in this file
  4. In the file config/routes.php I have entered 'test' => 'my-module/test/test
  
  When trying to go to the right URL(https://steamcool.site/test ) I get HTTP error 403 – Forbidden – yii\web\Forbidden HttpException
  
  P.S. I've simplified actionTest() a bit by putting just the code in it so far: echo "hello, world";
Answers.UserDisplayName: Dimi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-16T23:08:45.107Z
Answers.Text: Very interesting. The 403 error is an access-related error, as I understand it. After thinking about it, I logged in to the administrative panel of the site. And a miracle, I saw my line "hello, world". However, I also got some more incomprehensible lines. Is there any way to make my test url available not only to the site administrator?
Answers.UserDisplayName: Dimi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-17T00:15:26.297Z
Answers.Text: I found an error that caused me to output incomprehensible lines. Instead of echo, you should use return. It remains to be decided so that the code is visible not only to the admin
Answers.UserDisplayName: Dimi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-18T03:37:52.767Z
Answers.Text: Yes, you need to add this line public int|bool|array $allowAnonymous = true; because by default Craft Controller is set to false.
Answers.UserDisplayName: aodihis
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-18T22:47:30.613Z
Answers.Text: super! It works))) A very valuable lesson. I understand that in order to better navigate this, you would need to know Yii? Yes?
  And also, for the sake of experiment, I drove in return Entry::findOne($id). The title of the element was displayed on the screen. And how can I output the additional field of this element that I created?
Answers.UserDisplayName: Dimi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-19T01:22:16.250Z
Answers.Text: Yes you can get entry by this $entry = Entry::findOne($id), and then if you want to get additional field just return $entry->additionalField
Answers.UserDisplayName: aodihis
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-20T20:20:37.063Z
Answers.Text: And I used this construction: $price = Entry::findOne(25)->GetFieldValue('priceProduct'). In principle, it is clear how to find out the values of the fields of a certain element. Please tell me, what kind of construction should I use to sort through all the elements of a certain section?
Answers.UserDisplayName: Dimi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-21T03:50:36.987Z
Answers.Text: Oh If you want to find element on certain section you can do like this $entries = Entry::find()->section('sectionName')->all() you can put the orderBy if you want to do some sorting example like this Entry::find()->section('sectionName')->orderBy('title')all()
Answers.UserDisplayName: aodihis
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-09-06T05:47:22.253Z
Answers.LastActivityDate: 2022-10-14T03:57:39.450Z
Answers.LastEditDate: 2022-10-14T03:57:39.450Z
Answers.UserDisplayName: aodihis
Body: <p>please help me find a solution. Or at least tell me where to read about it.</p>
  <p>Task: There is a PHP file price.php . It has one variable, for example $price = 100;
  I created the &quot;store&quot; section. I need that when you click on my site on the link <a href="https://mysite.com/price.php" rel="nofollow noreferrer">https://mysite.com/price.php</a> the value of the $price variable was added to the element with id = 2 in the priceProduct field I created. Such a simple field update. How to implement this in CRAFT CMS?</p>
  
Tags: <craft3>
Title: How to update a field?
ThreadText: # How to update a field?
  
  (Asked by: username_0 on 2022-09-05)
  
  please help me find a solution. Or at least tell me where to read about it.
  
  Task: There is a PHP file price.php . It has one variable, for example $price = 100;
  I created the "store" section. I need that when you click on my site on the link <https://mysite.com/price.php> the value of the $price variable was added to the element with id = 2 in the priceProduct field I created. Such a simple field update. How to implement this in CRAFT CMS?
  
  **Tags:** craft3
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-09-06. Score: 2 | Accepted answer
  
  To be able to do this you need to create a custom module in your Craft.
  Usually when you install craft, craft already include an example of Module inside the modules folder, you create you own but let use this first for example.
  
  Now let's check the config/app.php, and you will see something like this :
  
  ```
  'modules' => [
      'my-module' => \modules\Module::class,
  ],
  
  ```
  
  This means you module is registered and the module id is `my-module`. So the next step is you need controller to call your code, then go to `modules` folder create a folder called `controllers`, then inside it create a class something like this TestController, to be note the class name must use suffix `Controller`. So inside your TestController should be like this :
  
  ```
  class TestController extends Controller
  {
    public function actionTest() {
  
     }
  }
  
  ```
  
  So in here we can put our code inside the actionTest function. To save element, You can do it by calling saveElement from Craft and pass the entry element, it should be like this :
  
  ```
  $entry = Entry::findOne($id);
  $entry->setFieldValue('priceProduct', $price);
  Craft::$app->elements->saveElement($entry);
  
  ```
  
  so the full code can be like this :
  
  ```
  class TestController extends Controller
  {
    public function actionTest() {
        $id = 2;
        $price = '100';
        $entry = Entry::findOne($id);
        $entry->setFieldValue('priceProduct', $price);
        Craft::$app->elements->saveElement($entry);
     }
  }
  
  ```
  
  Then lastly you need to register the route for your controller, so go to config/routes.php put something like this :
  
  ```
  return [
      'test' => 'my-module/test/test',
  ];
  
  ```
  
  The explanation for the path : my-module is our module id that we register inside the app.php, then first test segment based on the class name so our controller class is TestController, and the last test segment is based on function name (actionTest).
  
  After that try to call it by `yousiteurl/test`.
  For the full explanation how to create controller and module you can go here.
  <https://craftcms.com/docs/4.x/extend/controllers.html#request-validation-methods>
  
  > **Comments:**
  > **username_0**: I tried to paste this code into a file price.php but nothing happened. Why is that? Don't judge strictly, I'm new to CRAFT CMS  
  > **username_1**: @username_0, can you show us your section field configuration. There probably an error on validation. The return of craft save element is true or false, if you want to check if the save is success or not.  
  > **username_0**: The field I am trying to update has the type "Plain Text Fields". Nothing supernatural. Maybe it won't be possible to launch and execute it just like that in Craft CMS? Maybe it is necessary to create a module or plugin for these purposes?  
  > **username_0**: I forgot to clarify. I use Craft CMS 4.2.5.2  
  > **username_0**: Maybe we should use something like this at the beginning of the file price.php : usa Craft; use craft\elements\Entry;  
  > **username_0**: Forgive me for my possibly stupid questions. I'm new to this CMS  
  > **username_1**: @username_0, yes you need to create a module for these purpose, you cannot call the php directly on Craft. So you need to create module and put a controller inside them to run that script.  
  > **username_0**: Could you provide some easy example on creating a module in craft CMS. Some very simple example. I just can't imagine how to run the code I need on a separate url in this content management system. It will be very useful for me) I really want to learn how to work in this system  
  > **username_1**: @username_0, sure I edited the answer.  
  > **username_0**: Detailed answer) But the first time, of course, nothing started. Here's what I did: 1. Went to the modules folder 2. Created the controllers folder there 3. In the controllers folder, I created a file TestController.php and put the controller class in this file 4. In the file config/routes.php I have entered 'test' => 'my-module/test/test  When trying to go to the right URL(https://steamcool.site/test ) I get HTTP error 403 – Forbidden – yii\web\Forbidden HttpException  P.S. I've simplified actionTest() a bit by putting just the code in it so far: echo "hello, world";  
  > **username_0**: Very interesting. The 403 error is an access-related error, as I understand it. After thinking about it, I logged in to the administrative panel of the site. And a miracle, I saw my line "hello, world". However, I also got some more incomprehensible lines. Is there any way to make my test url available not only to the site administrator?  
  > **username_0**: I found an error that caused me to output incomprehensible lines. Instead of echo, you should use return. It remains to be decided so that the code is visible not only to the admin  
  > **username_1**: Yes, you need to add this line public int|bool|array $allowAnonymous = true; because by default Craft Controller is set to false.  
  > **username_0**: super! It works))) A very valuable lesson. I understand that in order to better navigate this, you would need to know Yii? Yes? And also, for the sake of experiment, I drove in return Entry::findOne($id). The title of the element was displayed on the screen. And how can I output the additional field of this element that I created?  
  > **username_1**: Yes you can get entry by this $entry = Entry::findOne($id), and then if you want to get additional field just return $entry->additionalField  
  > **username_0**: And I used this construction: $price = Entry::findOne(25)->GetFieldValue('priceProduct'). In principle, it is clear how to find out the values of the fields of a certain element. Please tell me, what kind of construction should I use to sort through all the elements of a certain section?  
  > **username_1**: Oh If you want to find element on certain section you can do like this $entries = Entry::find()->section('sectionName')->all() you can put the orderBy if you want to do some sorting example like this Entry::find()->section('sectionName')->orderBy('title')all()  
  
  ---
  
  