Programming

Folders and Files

Class & Controller

View

Form

Image

PDF

Chart

Database (Single table)

Database (Relational table)

Sort (Drag and Drop)

Image Upload/Display

File Upload/Download

CSV Upload/Download

Database ( How to retrieve data from database) - Filter

Database ( How to retrieve data from database) - Select

Database (Relational table)

In this example, we will describe how to make a relationship with another table. Let's imagine you have a Posts table. Now you want to add tags to them. A Post will have multiple Tags.

Lets create database scheema.

posts.fmt

id,24,N
title,255,T
body,255,T

tags.fmt

id,24,N
name,255,T
post_id,24,N

Implemet the class.

class posts {

private $frm_posts;
private $frm_tags;

function __construct(Controller $ctl) {
$this->frm_posts = $ctl->db("posts");
$this->frm_tags = $ctl->db("tags");
}

Make Relationship

Open a window to add tag

<button class="ajax-link" data-form="form{$post.id}" data-class="posts" data-function="add_tags" data-post_id="{$post.id}">Add Tags</button>

//open a window to add tag
function add_tags(Controller $ctl)
{
$post_id = $ctl->POST('post_id');
$ctl->assign("post_id", $post_id);
$ctl->show_multi_dialog("add_tags".$post_id, "add_tags.tpl", "Add Tags",500,true,true);
}

While creating a tag, "post_id" must be sent to the function to make a relation to the post.

<form id="form_add_tag{$post_id}">

Tag: <input type="text" name="name" value="" >

<button data-post_id="{$post_id}" class="ajax-link" data-form="form_add_tag{$post_id}" data-class="posts" data-function="add_tags_exe">Add</button>

</form>

//save tag
function add_tags_exe(Controller $ctl)
{
$data = $ctl->POST();
$this->frm_tags->insert($data);
}

Retrieve Related Data

Select related tags to a Post and parse them to the post-editing page.

function view_post_edit_page(Controller $ctl){
//filter post data
$post_id = $ctl->POST('id');
$post = $this->frm_posts->get($post_id);
$ctl->assign("post", $post);

//filter related tags to the post (use post_id field)
$tags = $this->frm_tags->filter(['post_id'], [$post_id]);
$ctl->assign("tags", $tags);

$ctl->show_multi_dialog("edit".$post_id, "edit.tpl", "Edit Posts",1000,true,true);
}

Edit Tag

Load a view to edit a tag.

<div class="ajax-link delete_btn listbutton" data-form="form" data-class="posts" data-function="edit_tag" data-id="{$tag.id}" data-post_id="{$tag.post_id}">

<span class="ui-icon ui-icon-pencil"></span>

</div>

function edit_tag(Controller $ctl)
{
$id = $ctl->POST('id');
$post_id = $ctl->POST('post_id');

$tag = $this->frm_tags->get($id);

$ctl->assign("id", $id);
$ctl->assign("tag", $tag);
$ctl->assign("post_id", $post_id);
$ctl->show_multi_dialog("edit_tags".$id, "edit_tags.tpl", "Edit Tags",500,true,true);
}

<form id="form_edit_tags{$tag.id}">

Tag: <input type="text" name="name" value="{$tag.name}" >

<button class="ajax-link" data-form="form_edit_tags{$tag.id}" data-class="posts" data-function="edit_tags_exe" data-id="{$tag.id}" data-post_id="{$tag.post_id}">Save</button>

</form>

Updating the tag.

function edit_tags_exe(Controller $ctl)
{
$data = $ctl->POST();

$this->frm_tags->update($data);
$ctl->close_multi_dialog("edit_tags".$data['id']);
}

Delete Tag

<div class="ajax-link delete_btn listbutton" data-form="form" data-class="mposts" data-function="delete_tag" data-id="{$tag.id}" data-post_id="{$tag.post_id}">

<span class="ui-icon ui-icon-trash"></span>

</div>

function delete_tag(Controller $ctl){

$id = $ctl->POST('id');
$post_id = $ctl->POST('post_id');

$this->frm_tags->delete($id);
}

FOCUS Business Platform