We have to remember toclose our table, form, and PHP tags, and then output the HTML template exactly like the last page example... Altering Related RecordsLet’s modify the web page to al
Trang 1The rest of the page is pretty humdrum compared to all that We have to remember to
close our table, form, and PHP tags, and then output the HTML template exactly like the last page example
$portal_html = ‘</table>’;
$portal_html = ‘</form>’;
?>
<html>
<head>
<title>08_02</title>
</head>
<body>
<table border=”1”>
<tr>
<th>ID</th>
<td><?php echo $id; ?></td>
</tr>
<tr>
<th>Name</th>
<td><?php echo $name; ?></td>
</tr>
<tr>
<th>Model Number</th>
<td><?php echo $model_number; ?></td>
</tr>
<tr>
<th>Price</th>
<td><?php echo $price; ?></td>
</tr>
<tr>
<th>Created At</th>
<td><?php echo $created_at; ?></td>
</tr>
<tr>
<th>Created By</th>
<td><?php echo $created_by; ?></td>
</tr>
</table>
<?php echo $portal_html; ?>
</body>
</html>
Creating Related Records
Trang 2Altering Related Records
Let’s modify the web page to allow users to edit or delete related records in the portal by adding two hyperlinks to the third column See Figure 8.15 for an example of the page displayed in a browser
FIGURE 8.15 Users now have navigation to edit or delete pages for related inventory records
The concept here is almost identical to the edit and delete links that we covered in Chapter 7, “Altering FileMaker Data.” There, we added edit and delete links to the product list page Here, we are adding those same links to the portal
Here is the completed code:
<?php
define(‘FM_HOST’, ‘127.0.0.1’);
define(‘FM_FILE’, ‘Product Catalog’);
define(‘FM_USER’, ‘esmith’);
define(‘FM_PASS’, ‘m4rg0t’);
require_once (‘FileMaker.php’);
$fm = new FileMaker(FM_FILE, FM_HOST, FM_USER, FM_PASS);
$record = $fm->getRecordById(‘Product’, $_GET[‘recid’]);
if (isset($_POST[‘new_portal_row’])) {
$new_row = $record->newRelatedRecord(‘Inventory’);
$new_row->setField(‘Inventory::Location’, $_POST[‘location’]);
$new_row->setField(‘Inventory::Quantity’, $_POST[‘quantity’]);
$result = $new_row->commit();
$record = $fm->getRecordById(‘Product’, $_GET[‘recid’]);
}
$id = $record->getField(‘ID’);
Trang 3$name = $record->getField(‘Name’);
$model_number = $record->getField(‘Model Number’);
$price = $record->getField(‘Price’);
$created_at = $record->getField(‘Created At’);
$created_by = $record->getField(‘Created By’);
$portal_records = $record->getRelatedSet(‘Inventory’);
$portal_html = ‘<form action=”08_02.php?recid=’
➥$record->getRecordId() ‘“ method=”post”>’;
$portal_html.= ‘<table border=”1”>’;
$portal_html.= ‘<tr>’;
$portal_html.= ‘<th>Location</th>’;
$portal_html.= ‘<th>Quantity</th>’;
$portal_html.= ‘<th> </th>’;
$portal_html.= ‘</tr>’;
foreach($portal_records as $portal_record) {
$portal_html.= ‘<tr>’;
$portal_html.= ‘<td>’ $portal_record->getField(‘Inventory::Location’)
➥ ‘</td>’;
$portal_html.= ‘<td>’ $portal_record->getField(‘Inventory::Quantity’)
➥ ‘</td>’;
$portal_html.= ‘<td>’;
$portal_html.= ‘<a href=”08_04.php?recid=’.$portal_record->getRecordId().’”>
➥edit</a>’;
$portal_html.= ‘ ’;
$portal_html.= ‘<a href=”08_05.php?recid=’.$portal_record->
➥getRecordId().’”>delete</a>’;
$portal_html.= ‘</td>’;
$portal_html.= ‘</tr>’;
}
$portal_html.= ‘<tr>’;
$portal_html.= ‘<td><input type=”text” name=”location” value=”” /></td>’;
$portal_html.= ‘<td><input type=”text” name=”quantity” value=”” /></td>’;
$portal_html.= ‘<td><input type=”submit” name=”new_portal_row”
➥value=”Save” /></td>’;
$portal_html.= ‘</tr>’;
$portal_html.= ‘</table>’;
$portal_html.= ‘</form>’;
?>
<html>
<head>
<title>08_03</title>
</head>
<body>
<table border=”1”>
<tr>
Altering Related Records
Trang 4<td><?php echo $id; ?></td>
</tr>
<tr>
<th>Name</th>
<td><?php echo $name; ?></td>
</tr>
<tr>
<th>Model Number</th>
<td><?php echo $model_number; ?></td>
</tr>
<tr>
<th>Price</th>
<td><?php echo $price; ?></td>
</tr>
<tr>
<th>Created At</th>
<td><?php echo $created_at; ?></td>
</tr>
<tr>
<th>Created By</th>
<td><?php echo $created_by; ?></td>
</tr>
</table>
<?php echo $portal_html; ?>
</body>
</html>
Here is the completed code with descriptions inline It begins with the usual suspects:
<?php
define(‘FM_HOST’, ‘127.0.0.1’);
define(‘FM_FILE’, ‘Product Catalog’);
define(‘FM_USER’, ‘esmith’);
define(‘FM_PASS’, ‘m4rg0t’);
require_once (‘FileMaker.php’);
$fm = new FileMaker(FM_FILE, FM_HOST, FM_USER, FM_PASS);
$record = $fm->getRecordById(‘Product’, $_GET[‘recid’]);
Here is the code block that handles any incoming requests for the creation of a new portal row It’s identical to the preceding example
if (isset($_POST[‘new_portal_row’])) {
$new_row = $record->newRelatedRecord(‘Inventory’);
$new_row->setField(‘Inventory::Location’, $_POST[‘location’]);
$new_row->setField(‘Inventory::Quantity’, $_POST[‘quantity’]);
Trang 5$result = $new_row->commit();
$record = $fm->getRecordById(‘Product’, $_GET[‘recid’]);
}
This section is also identical to the prior examples We are just grabbing the field values from the Product record so we can output them in the HTML template section at the end
of the page
$id = $record->getField(‘ID’);
$name = $record->getField(‘Name’);
$model_number = $record->getField(‘Model Number’);
$price = $record->getField(‘Price’);
$created_at = $record->getField(‘Created At’);
$created_by = $record->getField(‘Created By’);
Next, I am prepping for the foreachloop by compiling the open HTML for the portal
form and table I get the related set, open the form and table tags, and load the header
HTML
$portal_records = $record->getRelatedSet(‘Inventory’);
$portal_html = ‘<form action=”08_02.php?recid=’ $record->getRecordId()
➥ ‘“ method=”post”>’;
$portal_html.= ‘<table border=”1”>’;
$portal_html.= ‘<tr>’;
$portal_html.= ‘<th>Location</th>’;
$portal_html.= ‘<th>Quantity</th>’;
$portal_html.= ‘<th> </th>’;
$portal_html.= ‘</tr>’;
Here is the start of the foreachloop that creates the guts of the portal HTML
foreach($portal_records as $portal_record) {
$portal_html.= ‘<tr>’;
$portal_html.= ‘<td>’ $portal_record->getField(‘Inventory::Location’)
➥ ‘</td>’;
$portal_html.= ‘<td>’ $portal_record->getField(‘Inventory::Quantity’)
➥ ‘</td>’;
$portal_html.= ‘<td>’;
The following line is new to this example, but should look familiar because I used a
similar technique in Chapter 7 for the View Product page I am using the getRecordId() method to pull the internal record ID out of the portal record object so I can include it in the URL that points to the edit page We will look at the edit page in the next example
$portal_html.= ‘<a href=”08_04.php?recid=’.$portal_record->getRecordId()
➥.’”>edit</a>’;
Altering Related Records
Trang 6This line just inserts a nonbreaking space between the edit link and the delete link:
$portal_html.= ‘ ’;
Here is the delete link It’s just like the edit link except that it points to a different page and is displayed as “delete,” of course
$portal_html.= ‘<a href=”08_05.php?recid=’.$portal_record->getRecordId()
➥.’”>delete</a>’;
These lines close out the td,tr, and foreachloop:
$portal_html.= ‘</td>’;
$portal_html.= ‘</tr>’;
}
Next, we have the code that draws the new related record row It’s identical to the
previous example
$portal_html.= ‘<tr>’;
$portal_html.= ‘<td><input type=”text” name=”location” value=”” /></td>’;
$portal_html.= ‘<td><input type=”text” name=”quantity” value=”” /></td>’;
$portal_html.= ‘<td><input type=”submit” name=”new_portal_row”
➥value=”Save” /></td>’;
$portal_html.= ‘</tr>’;
Now, just close the table, the form, the PHP block, and output the HTML template exactly like before
$portal_html.= ‘</table>’;
$portal_html.= ‘</form>’;
?>
<html>
<head>
<title>08_03</title>
</head>
<body>
<table border=”1”>
<tr>
<th>ID</th>
<td><?php echo $id; ?></td>
</tr>
<tr>
<th>Name</th>
<td><?php echo $name; ?></td>
</tr>
<tr>
<th>Model Number</th>
Trang 7<td><?php echo $model_number; ?></td>
</tr>
<tr>
<th>Price</th>
<td><?php echo $price; ?></td>
</tr>
<tr>
<th>Created At</th>
<td><?php echo $created_at; ?></td>
</tr>
<tr>
<th>Created By</th>
<td><?php echo $created_by; ?></td>
</tr>
</table>
<?php echo $portal_html; ?>
</body>
</html>
Editing a Related Record
Editing a related record is exactly the same as editing any other record That being the
case, this example is going to be the same as the Edit Record example in Chapter 7 except that it will be pointed at an Inventory record instead of a Product record See Figure 8.16 for an example of the page displayed in a browser
Editing a Related Record
FIGURE 8.16 The edit page for an inventory record
Here is the completed code:
<?php
define(‘FM_HOST’, ‘127.0.0.1’);
define(‘FM_FILE’, ‘Product Catalog’);
define(‘FM_USER’, ‘esmith’);
Trang 8define(‘FM_PASS’, ‘m4rg0t’);
require_once (‘FileMaker.php’);
$fm = new FileMaker(FM_FILE, FM_HOST, FM_USER, FM_PASS);
$message = ‘’;
if (isset($_POST[‘action’]) and $_POST[‘action’] == ‘Save’) {
$edit = $fm->newEditCommand(‘Inventory’, $_REQUEST[‘recid’]);
$edit->setField(‘Location’, $_POST[‘location’]);
$edit->setField(‘Quantity’, $_POST[‘quantity’]);
$edit->execute();
$message = ‘<p>Your changes have been saved</p>’;
}
$record = $fm->getRecordById(‘Inventory’, $_REQUEST[‘recid’]);
$location = $record->getField(‘Location’);
$quantity = $record->getField(‘Quantity’);
?>
<html>
<head>
<title>08_04</title>
</head>
<body>
<?php echo $message; ?>
<form action=”08_04.php” method=”post”>
<input type=”hidden” name=”recid” value=”<?php echo $record->getRecordId();
➥?>” />
<table border=”1”>
<tr>
<th>Location</th>
<td><input type=”text” name=”location”
➥value=”<?php echo $location; ?>” /></td>
</tr>
<tr>
<th>Quantity</th>
<td><input type=”text” name=”quantity”
➥value=”<?php echo $quantity; ?>” /></td>
</tr>
</table>
<input type=”submit” name=”action” value=”Save” />
</form>
</body>
</html>
And here it is again with the few minor changes called out
<?php
define(‘FM_HOST’, ‘127.0.0.1’);
define(‘FM_FILE’, ‘Product Catalog’);
Trang 9define(‘FM_USER’, ‘esmith’);
define(‘FM_PASS’, ‘m4rg0t’);
require_once (‘FileMaker.php’);
$fm = new FileMaker(FM_FILE, FM_HOST, FM_USER, FM_PASS);
$message = ‘’;
if (isset($_POST[‘action’]) and $_POST[‘action’] == ‘Save’) {
Note that, here, we are now pointing the first parameter of the newEditCommand()at the Inventory layout and the setField()methods have been updated to be appropriate to
the Inventory table
$edit = $fm->newEditCommand(‘Inventory’, $_REQUEST[‘recid’]);
$edit->setField(‘Location’, $_POST[‘location’]);
$edit->setField(‘Quantity’, $_POST[‘quantity’]);
$edit->execute();
$message = ‘<p>Your changes have been saved</p>’;
}
Pointing at the Inventory layout in this getRecordById()method:
$record = $fm->getRecordById(‘Inventory’, $_REQUEST[‘recid’]);
Pulling values for the fields of the Inventory table:
$location = $record->getField(‘Location’);
$quantity = $record->getField(‘Quantity’);
?>
The only significant change to the HTML section is that we are outputting fields appropri-ate to the table that we are working with
<html>
<head>
<title>08_04</title>
</head>
<body>
<?php echo $message; ?>
<form action=”08_04.php” method=”post”>
<input type=”hidden” name=”recid” value=”<?php $record->getRecordId(); ?>” />
<table border=”1”>
<tr>
<th>Location</th>
<td><input type=”text” name=”location”
➥value=”<?php echo $location; ?>” /></td>
</tr>
<tr>
<th>Quantity</th>
Editing a Related Record
Trang 10<td><input type=”text” name=”quantity”
➥value=”<?php echo $quantity; ?>” /></td>
</tr>
</table>
<input type=”submit” name=”action” value=”Save” />
</form>
</body>
</html>
Deleting a Related Record
I hate to sound like a broken record, but deleting a related record is exactly the same as deleting any other record That being the case, this example is going to be the same as the Delete Record example in Chapter 7 except that it will be pointed at an Inventory record instead of a Product record See Figure 8.17 for an example of the page displayed in a browser
FIGURE 8.17 When a user clicks the delete link on an inventory portal row, a confirmation page opens
Here is the completed code:
<?php
define( ‘FM_HOST’, ‘127.0.0.1’ );
define( ‘FM_FILE’, ‘Product Catalog’ );
define( ‘FM_USER’, ‘esmith’ );
define( ‘FM_PASS’, ‘m4rg0t’ );
require_once (‘FileMaker.php’);
$fm = new FileMaker(FM_FILE, FM_HOST, FM_USER, FM_PASS);
$record = $fm->getRecordById(‘Inventory’, $_REQUEST[‘recid’]);
if (!isset($_POST[‘action’])) {
$page_content = ‘Are you sure you want to delete the ‘.$record->getField
➥(‘Location’).’ inventory record?’;
$page_content = ‘<form action=”08_05.php” method=”post”>’;
$page_content = ‘<input type=”hidden” name=”recid” value=”’.$record->
➥getRecordId().’” />’;
$page_content = ‘<p>’;
$page_content = ‘<input type=”submit” name=”action” value=”Delete” />’;
$page_content = ‘<input type=”submit” name=”action” value=”Cancel” />’;
$page_content = ‘</p>’;
Trang 11$page_content = ‘</form>’;
} else {
if ($_POST[‘action’] == ‘Delete’) {
$record->delete();
$page_content = ‘<p>Record was deleted.</p>’;
} else {
$page_content = ‘<p>Action cancelled Record was not deleted.</p>’;
}
}
?>
<html>
<head>
<title>08_05</title>
</head>
<body>
<?php echo $page_content; ?>
</body>
</html>
This page begins like all the others:
<?php
define( ‘FM_HOST’, ‘127.0.0.1’ );
define( ‘FM_FILE’, ‘Product Catalog’ );
define( ‘FM_USER’, ‘esmith’ );
define( ‘FM_PASS’, ‘m4rg0t’ );
require_once (‘FileMaker.php’);
$fm = new FileMaker(FM_FILE, FM_HOST, FM_USER, FM_PASS);
Point the getRecordById()method at the Inventory layout:
$record = $fm->getRecordById(‘Inventory’, $_REQUEST[‘recid’]);
If the actionelement of the $_POSTsuperglobal array has not been set, the user is viewing the page for the first time, so prompt the user to confirm the deletion and include Cancel and Delete buttons:
if (!isset($_POST[‘action’])) {
$page_content = ‘Are you sure you want to delete the ‘.$record->getField
➥(‘Location’).’ inventory record?’;
$page_content = ‘<form action=”08_05.php” method=”post”>’;
$page_content = ‘<input type=”hidden” name=”recid” value=”’.$record->
➥getRecordId().’” />’;
$page_content = ‘<p>’;
$page_content = ‘<input type=”submit” name=”action” value=”Delete” />’;
$page_content = ‘<input type=”submit” name=”action” value=”Cancel” />’;
$page_content = ‘</p>’;
$page_content = ‘</form>’;
Deleting a Related Record