TUTORIAL: "Form" Class #2: Viewing Data - mudmin/UserSpice5-Dev GitHub Wiki
If you want to make our form more useful you will want to load the data from the database for the record identified in $_GET['id'].
You change which record we are viewing/editing by changing the URL in the address bar of your browser - if you go to www.example.com/tutorial/form2a.php?id=2 then you would view the record with id=2; if you go to www.example.com/tutorial/form2a.php?id=27 then you would view the record with id=27 (but we only have 2 or 3 records in this tutorial database, so don't try id=27).
We'll need to change our form a bit to match an actual table in your database (foo in our case). Here's what the database table structure for foo looks like:

So here is the form as it matches the structure in table foo:
<?php
$myForm = new Form ([
'foo' => new FormField_Text,
'a' => new FormField_Text,
'b' => new FormField_Text,
'bool' => new FormField_Checkbox,
'save' => new FormField_ButtonSubmit,
]);
echo $myForm->getHTML();
You can view that as tutorial/form2a.php. A screenshot looks like this:

And to load the data from the database table we will make changes like this:
<?php
$fooId = @$_GET['id'];
$db = DB::getInstance();
$fooData = $db->queryById('foo', $fooId)->first();
$myForm = new Form ([
'foo' => new FormField_Text,
'a' => new FormField_Text,
'b' => new FormField_Text,
'bool' => new FormField_Checkbox,
'save' => new FormField_ButtonSubmit,
], [
'data' => $fooData,
]);
echo $myForm->getHTML();
We'll look at each of those lines in detail in a moment, but first let's load that page in our browser and see what it looks like (you can get to it at tutorial/form2b.php). Here's a screenshot using ?id=1:

Once you have that working on your system, try changing to ?id=2 or ?id=3 to see how different data is loaded each time.
Let's see how we did that, considering each new change to the code as we moved from tutorial/form2a.php to tutorial/form2b.php:
$fooId = @$_GET['id'];
This has nothing to do with the Form class, but is rather simple PHP to get the value you placed in the URL with ?id=1 and place that 1 into a variable named $fooId.
$db = DB::getInstance();
$fooData = $db->queryById('foo', $fooId)->first();
These 2 lines make use of the DB class of UserSpice to access the database and look up the first record in the table foo that matches the given $fooId (as assigned from $_GET['id'] above). It places the resulting row in the variable $fooData for use later on.
Finally, we added a second argument (another array) to pass in some options to the $myForm constructor:
], [
'data' => $fooData,
]);
All this does is say that $myForm should load the data in the form from the contents of the $fooData variable.
We could have saved ourselves the use of a variable by doing it like this:
], [
'data' => $db->queryById('foo', $fooId)->first(),
]);
But it's probably clearer code when you put it into a well-named variable and then use the variable.
Note that the 'data' option only works when we are using forms that deal with a single record. That won't work when we get into more complicated forms that list many records on a single form.
Now we have successfully viewed data on a simple form connected to the database. The next thing is to allow changes to occur. Click on the next link in the tutorial:
TUTORIAL: "Form" Class #3: Modifying Data