If you create a file in hooks with the special name of {tablename}.{fieldname}.csv (where {tablename} is the name of the concerned table, and {fieldname} is the name of the multiple-choice field, for example products.tags.csv), your AppGini app would use the contents of that magic file to populate the drop-down list when users are filling in that field. It would simply override the list of options configured in AppGini.

Configure the hooks for updating the options list file

Let’s now add code so that whenever a use adds, edits or deletes records from the tags table we created in step 1, the options list file would automatically be updated. First, let’s define the update_tags_list() function that would retrieve tags from the table, and use them to populate the options list file. In the generated hooks/tags.php file, append this code block to the end of the file:

function update_tags_list() {
// retrieve existing tags
$tags = array();
$res = sql("select * from tags order by tag", $eo);
while($row = db_fetch_assoc($res))
$tags[] = $row['tag'];

// save the tags to the options list file
$list_file = dirname(__FILE__) . '/products.tags.csv';
@file_put_contents($list_file, implode(';;', $tags));
}

And invoke that function after inserting any records in the tags table by calling it in the tags_after_insert() hook:

function tags_after_insert($data, $memberInfo, &$args){
update_tags_list();
return TRUE;
}

Do the same in tags_after_update() to update the list if any tags are edited:

function tags_after_update($data, $memberInfo, &$args){
update_tags_list();
return TRUE;
}

And in tags_after_delete() as well:

function tags_after_delete($selectedID, $memberInfo, &$args){
update_tags_list();
}