The PHP Series Examples
Reading a description or tutorial is always interesting but it can be hard to understand everything and all the features included. This post will use our 6 PHP classes we have created and look upon a few example on how they can interact with each other.
We will begin with a example between the Database and Paging class. We start by including the two classes in our default.php page using ‘require’. We create objects of our classes and we simply start with a connection to our database and starts our paging.
$data = new Database('database', 'server', 'user', 'password');
$paging = new Paging('SELECT * FROM person', 5, 'result' ,$data);
This is what we need to do to instantiate our classes. Next up is to loop through the resultset the paging object gets. In this example we will use the public fetchObject() method in the Paging class.
while($rs = $paging->fetchObject()):
echo $rs->username . '<br/>';
endwhile;
To finish off this example we need to echo out the paging links.
echo $paging->getLinks();
Simple! We are done! To get the example working you need to make sure database, server, user and password are correct to access your MySQL database. The SQL string needs to be replaced by a real SQL string and lastly, the $rs->username must be change to a column name in your resultset.
Next up a Database and Record class example. We start again by creating objects of our Database and Record class:
$data = new Database('database', 'server', 'user', 'password');
$record = new Record('person', 'id', $data);
After that we go straight to save a new record in the person table. In the example I know that there are two fields in the person table: username and password. Since we didn’t include fields in the instantiating of the Record class all fields in the person table is open to us.
$record->username = 'Niklas';
$record->password = 'test';
And lastly we save the record using the save() method making a new record entry in the person table.
$record->save();
We continue with the example and this time we are going to try to find an entry using the find() method and update the password. We are going search for the latest inserted user. Since we just added a new user to the person table we will access that tuple and update the password value.
$record = new Record('person', 'id', $data);
$id = $data->fetchTuple('SELECT id FROM person ORDER BY id DESC');
$record->find($id);
We be sure we have a record to work with we echo out both the username and the password.
echo $record->username;
echo $record->password;
The result will be ‘Niklas’ and ‘test’. OK, now we want to update the password from ‘test’ to ‘updated’. Lastly we save the record and echo out the password to see if the task is complete.
$record->password = 'updated';
$record->save();
echo $record->password;
The answer: ‘updated’. Perfect! To be sure that the value is saved in the database you need to check the table using your MySQL Query Browser or simple create a new Record object and find the id we just updated. Now we have code for saving a new record and updating an existing one.
We continue with another example between the Database and Forms class. We will first create a form from scratch and then try to auto generate one using the generate() method in the Forms class. The last task will be a bit hard to just download and get up and running smoothly since it’s tightly integrated with my personal MySQL database. Anyway, you will see the idea on how to use the classes.
We start as we did before with creating objects from our classes:
$data = new Database('database', 'server', 'user', 'password');
$form = new Form('GET', 'example_database_forms.php');
We use the same Database settings as before and the Form object will use ‘GET’ as the method and go to the example_database_forms.php page on submit. We will create two fields, a username field, password field and then include a submit button.
$form->addField('username', 'text', array('required'=>true, 'format'=>'letter', 'value'=>'Username', 'min'=>3));
$form->addField('password', 'password', array('required'=>true, 'format'=>'string', 'value'=>'Password'));
$form->addField('submit', 'submit', array('value'=>'Submit'));
echo $form->create();
Both fields are required, we have set a format pattern, letter and string, and the Username field got a min validation rule. Both fields also got a default value and the labels are showing. We have more validation and styling rules to use on our Form:
- label: set the label for each field.
- showLabel: true or false, decides if the field label should be visible or not.
- boxClass: each field is surrounded by a div box, this sets the class of that box.
- fieldClass: each field has a class, this sets the class of the field.
- snippet: a code snippet which is placed at the very end of the field.
To include one more validation rule you simply include a key => value into the last parameter, the array. This example requires not only the Database and Forms class to be included, the Common class is used by the Forms class and must be included too. We cannot forget about the validation JavaScript either so in the head we insert that.
Now we have created a small form from scratch and now we will auto-generate a form based on a MySQL table. To do this we need to provide a database object and a table name when we create our Forms object.
$form = new Form('POST', 'example_database_forms.php', array('db'=>$data, 'table'=>'person'));
The auto-generation of the form is simple. The next line does it all for us.
$form->generate();
Now we have the whole table as a form in our Form object. We can’t see anything on the screen because we haven’t created the HTML tags yet. In the example we are not using any default values and rules either. The next example is taken care of that. But, back to this example first. All we need to do now is to include a submit button and echo out the form.
$form->addField('submit', 'submit', array('value'=>'Submit'));
echo $form->create();
These four lines of code have generated a form from a table. You notice will notice a little * in the right corner of the labels, those are required. The generate() method takes care of a lot of cool things.
We stick to the Forms class example for a little while longer. Now we will try to fill an auto-generated Form with default value. Say we want to edit a user in our database. We will also say that each field requires to be longer than 3 chars. To complete this task there are a few things we must look at.
- First include the Record class so we will be able to get the user values.
- Give the generate() method a parameter.
So, we still start with including the Record class and then the instantiating of the objects:
require'(lib/Record.php');
$record = new Record('person', 'id', $data);
$form = new Form('POST', 'example_database_forms.php', array('db'=>$data, 'table'=>'person'));
Next we need to fetch the values from a $record object. We start with a search to find the user we are looking for and then fetching the values from that user.
$id = $data->fetchTuple('SELECT id FROM person ORDER BY id DESC');
$record->find($id);
$values = $record->getValues();
OK, we search for the last inserted user id and used the find() method in the Record class and saved the values from that class into a local variable $values. All we have to do now is to include those values in the generate() method parameter. We also said that each field must be at least 3 chars long, this is also included in the generate() parameter.
$form->generate(array('values'=>$values, 'min'=>3));
Simple. Now if you look at your result, and everything is OK, you will see that your form has default values and when submitting is check so each field is longer that 3 chars. We end it all with adding a submit button and echo the form out.
$form->addField('submit', 'submit', array('value'=>'Submit'));
echo $form->create();
The final example will be between the Database, Forms and Record classes again. We will take a look how you fast and easy can save a whole form using just a few lines. Our header will look like this:
require('lib/Database.php');
require('lib/Common.php');
require('lib/Form.php');
require('lib/Record.php');
session_start();
We have included everything we need and we start off by creating our Database object:
$data = new Database('database', 'server', 'user', 'password');
Nothing new there, the Database object has been the same throughout all these examples. Next we do a check if a $_GET['create'] has been provided. If it is, we are going to save the incoming information otherwise we echo out our form. Now, you shouldn’t use ‘GET’ when you are posting things, especially not registration forms. This is just a example so we are able to see which information we are sending. Do NOT use ‘GET’ in real applications.
The Form object will not look that different from before. We have added a hidden field named ‘create’ to the form just so we can catch that in our if / else statement.
if(isset($_GET['create'])) {
$record = new Record('person', 'id', $data);
$record->set($_GET);
$record->save();
echo 'User is saved!';
} else {
$form = new Form('GET', 'example_database_forms_record.php');
$form->addField('username', 'text', array('required'=>true, 'format'=>'letter', 'min'=>3));
$form->addField('password', 'password', array('required'=>true, 'format'=>'string'));
$form->addField('create', 'hidden', array('value'=>true));
$form->addField('submit', 'submit', array('value'=>'Submit'));
echo $form->create();
}
So there you have it! A few examples on how to use the classes we have been working on! Everyone is available for download so you can check them out more carefully. Just remember to change the parameters so they will work in your machine and towards your MySQL database. Enjoy!
About this entry
You’re currently reading “The PHP Series Examples,” an entry on Is the coffee still warm?
- Published:
- July 6, 2008 / 9:03 am
- Category:
- July
- Tags:
- common, create form, database, downloads, error handler, examples, forms, generate form, mysql, paging, PHP classes, record, save form, validation


No comments yet
Jump to comment form | comments rss [?] | trackback uri [?]