Database (Single table)
How to use database?
Create fmt folder in the path app->helloworld
The table structure is placed in the .fmt file, which should be located under fmt folder.
Example :
Place employee.fmt file in the fmt folder
employee.fmt file sample content is shown below.
id,24,N
name,255,T
address,255,T
tel,255,T
You can use the following code in your class file (Example : helloworld.php) to use the table.
class helloworld {
private $ffm_employee;
function __construct(Controller $ctl){
$this->ffm_employee = $ctl->db("employee");
}
}
Select Data
You can use getall() method to retrieve all data from the table.
function employee(Controller $ctl){
$employee_list = $this->ffm_employee->getall();
$ctl->assign("employee_list",$employee_list);
$ctl->show_multi_dialog("employee", "employee.tpl", "Employee Details",1000,true,true);
}
You can use following code to display employee details in smarty template file. (Example : employee.tpl)
<table>
<tr>
<td>Name</td>
<td>Address</td>
<td>Tel</td>
</tr>
{foreach $employee_list as $employee}
<tr>
<td>{$employee.name}</td>
<td>{$employee.address}</td>
<td>{$employee.tel}</td>
</tr>
{/foreach}
</table>
Insert Data
You can use insert() method to insert data into the table.
This method accepts one argument. This argument should be the dataset, which needs to be inserted into the table.
function insert(Controller $ctl){
$data = $ctl->POST();
$this->ffm_employee->insert($data);
$ctl->assign("message","Inserted");
}
Sample template file source code is shown below. (form submit) This is just for your reference if you need to simply understand the whole process.
Submit form data and store the submitted data in to the table.
<form id="form">
<p>Name</p> <input type="text" name="name">
<p>Address</p> <textarea name="address"></textarea>
<p>Telephone </p> <input type="text" name="tel">
</form>
<button class="ajax-link" data-form="form" data-class="employee" data-function="insert">Send</button>
Update Data
To update data in the table, you can use update() method.
This method accepts the dataset, which needs to be updated as its argument.
$this->ffm_employee->update($employee);
1) Sample template file source code is shown below. (To display editable data on the form - employee_edit.tpl)
2) Sample class file source code is shown below. (To send editable data to the view template file)
3) Sample class file source code is shown below. (To retrieve form data from the template file and update the database table)
<form id="form{$employee['id']}">
<input type="text" name="id" value="{$employee['id']}">
<p>Employee No</p><input type="text" name="employee_no" value="{$employee['employee_no']}" class="employee_no">
<p>Name</p> <input type="text" name="name" value="{$employee['name']}" class="name">
<p>Address</p> <textarea name="address" class="address">{$employee['address']}</textarea>
<p>Telephone </p> <input type="text" name="tel" value="{$employee['tel']}" class="tel">
<p>Gender </p>{html_radios name="gender" options=$gender selected=$employee['gender']}
</form>
<button class="ajax-link" data-form="form{$employee['id']}" data-class="employee" data-function="update">Update</button>
function edit(Controller $ctl){
$id = $ctl->POST("id");
$employee = $this->ffm_employee->get($id);
$ctl->assign("employee",$employee);
$ctl->show_multi_dialog("employee_edit".$id, "employee_edit.tpl", "Edit Employee",1000,true,true);
}
function update(Controller $ctl){
$employee = $ctl->POST();
$this->ffm_employee->update($employee);
$ctl->close_multi_dialog("employee_edit".$ctl->POST('id'));
}
Delete Data
delete() method is used to delete data from the table.
This method accepts the record id, which needs to be deleted as its argument.
$this->ffm_employee->delete($id);
1) Sample template file source code is shown below. (To have the confirmation popup window for the deleting item - delete_confirmation.tpl)
2) Sample class file source code is shown below. (To send deleting item data to the delete confirmation template file)
3) Sample class file source code is shown below. (To retrieve confirmed data from the template file and delete the specified record from the database table)
Are you sure you want to delete this?
{$emp_id}
<form id="form{$emp_id}">
<input type="hidden" name="emp_id" value="{$emp_id}">
</form>
<button class="ajax-link" data-form="form{$emp_id}" data-class="employee" data-function="delete">Send</button>
function delete_confirm(Controller $ctl){
$id = $ctl->POST("id");
$ctl->assign("emp_id",$id);
$ctl->show_multi_dialog("delete_confirmation".$id, "delete_confirmation.tpl", "Delete Employee",1000,true,true);
}
function delete(Controller $ctl){
$id = $ctl->POST('emp_id');
$this->ffm_employee->delete($id);
$ctl->close_multi_dialog("delete_confirmation".$id);
$this->employee($ctl);
}