FORMfields, the premiere web framework
Login
Support Home | Knowledge Base | Documentation | Forums | Contact Us
Info
FORMfields & FORMgen :: Generated Forms

Parent-Child (1 to N) Forms

(1560 views. Last Updated: 2008-04-20 1:20 PM)

Here is an example of how to create a form that references a child form using a 1 to N database relation. The entities in this example are companies and employees and there can be multiple employees in a company.

Note: This tutorial requires a basic understanding of programming in PHP.

1. Use FORMgen to create a new form called "employees":
a. Add the fields:
- PersonNameField called "name"
- EmailAddrField called "email"
- PhoneField called "phone"
b. On step 5:
- Check "Insert Form Data Into Database"
- Uncheck "Enable Searching", "Enable Record Importing" and "Enable Record Exporting"

2. Use FORMgen to create a new form called "companies":
a. Add the fields:
- PersonNameField called "company"
- StateField called "state"
b. On step 5:
- Check "Insert Form Data Into Database"
- Uncheck "Display Thank You Page"

3. Use phpMyAdmin to edit the employees table and add the attribute:
Field: company_id
Type: INT

3. Modify companies.php:
Add the following:
// Generate the URL that will allow us to edit the employees
$db = &FfDb::s();
$companyId = $db->getSimpleValue("SELECT LAST_INSERT_ID()");
$GLOBALS["FF_DONE_URL"] = "http://devhost/FORMfields/forms/generated/extras/cp_employees.php?company_id=" . $companyId;
above the line:
if (!FF_DISPLAY_THANK_YOU_PAGE) {

4. Modify cp_employees.php:
a. Add:
$phpSelfWithId = $_SERVER["PHP_SELF"] . "?company_id=" . $_REQUEST["company_id"];
after:
FfConfig::verifyLogin();
b. Add:
$ffForm->addField(new HiddenField("company_id", null));
at the top of the Field Specification.

c. Replace all instances of:
header("Location: " . $_SERVER["PHP_SELF"]);
with:
header("Location: " . $phpSelfWithId);
d. Replace:
$sql =
    "SELECT "
        . "CONCAT('<a href=\"?ff_id=',ff_id,'\">" . FfLH::t("View") . "</a>') AS ' ',"
        . (FF_CAN_EDIT ? "CONCAT('<a href=\"?ff_id=',ff_id,'&amp;action=edit\">" . FfLH::t("Edit") . "</a>') AS '  '," : "")
        . (FF_CAN_DELETE ? "CONCAT('<a href=\"?ff_id=',ff_id,'&amp;action=delete\">" . FfLH::t("Delete") . "</a>') AS '   '," : "")
        . "ff_id AS '" . FfLH::t("ID") . "',"
        . FF_SELECT_CLAUSE . ","
        . "DATE_FORMAT(ff_inserted_on, '%Y-%m-%d %l:%i %p') AS '" . FfLH::t("Inserted On") . "',"
        . "DATE_FORMAT(ff_timestamp, '%Y-%m-%d %l:%i %p') AS '" . FfLH::t("Last Updated") . "' "
    . "FROM " . FF_FORM_NAME;
with:
$sql =
    "SELECT "
        . "CONCAT('<a href=\"?ff_id=',ff_id,'&amp;company_id=" . $_REQUEST["company_id"] . "\">" . FfLH::t("View") . "</a>') AS ' ',"
        . (FF_CAN_EDIT ? "CONCAT('<a href=\"?ff_id=',ff_id,'&amp;company_id=" . $_REQUEST["company_id"] . "&amp;action=edit\">" . FfLH::t("Edit") . "</a>') AS '  '," : "")
        . (FF_CAN_DELETE ? "CONCAT('<a href=\"?ff_id=',ff_id,'&amp;company_id=" . $_REQUEST["company_id"] . "&amp;action=delete\">" . FfLH::t("Delete") . "</a>') AS '   '," : "")
        . "ff_id AS '" . FfLH::t("ID") . "',"
        . FF_SELECT_CLAUSE . ","
        . "DATE_FORMAT(ff_inserted_on, '%Y-%m-%d %l:%i %p') AS '" . FfLH::t("Inserted On") . "',"
        . "DATE_FORMAT(ff_timestamp, '%Y-%m-%d %l:%i %p') AS '" . FfLH::t("Last Updated") . "' "
    . "FROM " . FF_FORM_NAME
    . " WHERE company_id='" . mysql_escape_string($_REQUEST["company_id"]) . "'";
e. Replace:
<div style="margin:0px 0px 10px 5px;" class="ffField"><a href="?">&laquo; <?= FfLH::t("Go Back to List"?></a></div>
with:
<div style="margin:0px 0px 10px 5px;" class="ffField"><a href="?company_id=<?= $_REQUEST["company_id"?>">&laquo; <?= FfLH::t("Go Back to List"?></a></div>
f. Replace:
<div style="margin:0px 0px 10px 5px;" class="ffField"><a href="?action=delete&amp;ff_id=<?= $_REQUEST["ff_id"?>"><?= FfLH::t("Delete Record"?></a></div>
with:
<div style="margin:0px 0px 10px 5px;" class="ffField"><a href="?action=delete&amp;ff_id=<?= $_REQUEST["ff_id"?>&company_id=<?= $_REQUEST["company_id"?>"><?= FfLH::t("Delete Record"?></a></div>
g. Replace:
<div style="margin:0px 0px 10px 5px;" class="ffField"><a href="?">&laquo; <?= FfLH::t("Go Back to List"?></a></div>
with:
<div style="margin:0px 0px 10px 5px;" class="ffField"><a href="?company_id=<?= $_REQUEST["company_id"?>">&laquo; <?= FfLH::t("Go Back to List"?></a></div>
h. Replace:
<div style="margin:0px 0px 10px 5px;" class="ffField"><a href="?ff_id=<?= $_REQUEST["ff_id"?>&amp;action=edit"><?= FfLH::t("Edit This Record"?></a></div>
with:
<div style="margin:0px 0px 10px 5px;" class="ffField"><a href="?ff_id=<?= $_REQUEST["ff_id"?>&amp;action=edit&company_id=<?= $_REQUEST["company_id"?>"><?= FfLH::t("Edit This Record"?></a></div>
i. Replace:
<div style="margin:0px 0px 10px 5px;" class="ffField"><a href="?action=delete&amp;ff_id=<?= $_REQUEST["ff_id"?>"><?= FfLH::t("Delete Record"?></a></div>
with:
<div style="margin:0px 0px 10px 5px;" class="ffField"><a href="?action=delete&amp;ff_id=<?= $_REQUEST["ff_id"?>&company_id=<?= $_REQUEST["company_id"?>"><?= FfLH::t("Delete Record"?></a></div>
j. Replace:
<div style="margin:10px 0px 10px 5px;" class="ffField"><a href="?action=new"><?= FfLH::t("New Record"?></a></div>
with:
<div style="margin:10px 0px 10px 5px;" class="ffField"><a href="?action=new&company_id=<?= $_REQUEST["company_id"?>"><?= FfLH::t("New Record"?></a></div>
k. Add:
<div style="margin:10px 0px 10px 5px;font-size:14px;" class="ffField"><a href="http://www.formfields.com">Click here when you are done adding employees.</a></div>
before:
<? if (FF_CAN_IMPORT) { ?>

5. Copy cp_employees.php to cp_employees_admin.php and then edit cp_employees_admin.php by replacing:
<div style="margin:10px 0px 10px 5px;font-size:14px;" class="ffField"><a href="http://www.formfields.com">Click here when you are done adding employees.</a></div>
with:
<div style="margin:10px 0px 10px 5px;font-size:14px;" class="ffField"><a href="http://devhost/FORMfields/forms/generated/extras/cp_companies.php">Click here when you are done editing employees.</a></div>

6. Modify cp_companies.php:
a. Replace:
...
$ffForm->insertValuesIntoDb(FF_FORM_NAME);
header("Location: " . $_SERVER["PHP_SELF"]);
...
with:
...
$ffForm->insertValuesIntoDb(FF_FORM_NAME);

// Redirect to a page that will allow us to edit the employees
$db = &FfDb::s();
$companyId = $db->getSimpleValue("SELECT LAST_INSERT_ID()");
header("Location: http://devhost/FORMfields/forms/generated/extras/cp_employees_admin.php?company_id=" . $companyId);

//header("Location: " . $_SERVER["PHP_SELF"]);
...
b. Replace:
$GLOBALS["FF_DB"]->query("DELETE FROM " . FF_FORM_NAME . " WHERE ff_id=" . $_REQUEST["ff_id"]);
with:
// Delete any employees
$db = &FfDb::s();
$db->query("DELETE FROM employees WHERE company_id='" . mysql_escape_string($_REQUEST["ff_id"]) . "'");
$GLOBALS["FF_DB"]->query("DELETE FROM " . FF_FORM_NAME . " WHERE ff_id=" . $_REQUEST["ff_id"]);
c. Add:
<div style="margin:10px 0px 10px 5px;font-size:14px;" class="ffField"><a href="http://devhost/FORMfields/forms/generated/extras/cp_employees_admin.php?company_id=<?= $_REQUEST["ff_id"?>">Click here to view or edit employees.</a></div>
after both:
    <?= FfMenuHelper::getSectionTitleHeader(FfLH::t("Edit Record")) ?>
        <?= $ffForm->getTableTag(); ?>
    <?= FfMenuHelper::getSectionTitleFooter() ?>
</form>
and:
    <?= FfMenuHelper::getSectionTitleHeader(FfLH::t("View Record")) ?>
        <?= $ffForm->getDisplayTableTag(); ?>
    <?= FfMenuHelper::getSectionTitleFooter() ?>
</form>
Info
Support | Earn Money
Copyright © 2005-2019 Brain Book Software LLC.
Built with FORMfields, the premiere web framework.