It's time to create the pages needed to add data to the regions-table. Have in mind that when we create those pages you will be able to access the ADD-links for all tables. However, only the Regions will work, the other ones will generate errors!
Before we create the add-page we need to create another code chunk. This file is one of the corner stones of this CMS, so it's really important to get it right. This file will contain information about the database tables. It's important that this file reflects the structure of the database so whenever you make structural changes in your database, say you add a new field or changes the maximum length of a text field, you also need to come to this file and make the same changes. Let's get to it, shall we:
<?php
switch ($pageName) {
case "regi" :
$fieldNames = array("regionID","regionName","officialName","tvSystem","consoleRegion","titleRegion","countryRegion");
$fieldTitle = array("Region ID","Region name","Official name","TV system","Console region","Title region","Country");
$fieldType = array("text","text","text","select","checkbox","checkbox","checkbox");
$fieldLength = array(3,100,100,0,0,0,0);
$required = array(1,1,0,0,0,0,0);
$listing = array(1,1,1,1,0,0,0);
$table = "regions";
$mainField = "regionName";
break;
// INSERT NEW CODE BLOCKS HERE LATER
}
?>
Now let's walk through this code:
Important to think about when using this file is that the $pageName-variable needs to be set prior to this code chunk. In our case it should contain the value "regi".
The switch test then checks if it finds a match, which it does. Now it's time to create no less than six arrays that will hold data that is crucial to the form that we will soon generate. Let's check them out one by one:
There are also two variables that is crucial for the functionality:
As new tables are created in the database you will be asked to come back and add new code to this file. That code should be entered after the break;-command and before the closing bracket "}" of the switch.
This file will generate all fields you need to fill out in order to add a new row to your database table. Regardless of what table you want to add data to, this file will be loaded. This makes it necessary to tell the script what fields you want to use (you may not want all fields to appear in the form). This is done with a few arrays that is set by a switch test. Let's get to shall we!
<?php
include("php/switchPage.php");
include("php/switchTable.php");
$pageTitle = substr($title,0,strlen($title)-1);
if (substr($pageTitle,strlen($pageTitle)-2) == "ie") {
$pageTitle = substr($pageTitle,0,strlen($pageTitle)-2)."y";
}
?>
<h2>ADD <?php echo $pageTitle; ?></h2>
<form id="create">
<input type="hidden" id="receiver" value="create_data"></input>
<input type="hidden" id="table" value="<?php echo $table; ?>"></input>
<input type="hidden" id="fieldNames" value="<?php echo implode(",",$fieldNames); ?>"></input>
<?php
for ($i = 0; $i < count($fieldNames); $i++) {
if ($fieldType[$i] != "none") {
echo "<span class=\"form_item\">";
echo "<span class=\"form_head\">".$fieldTitle[$i].":</span>";
echo "<input class=\"";
if ($required[$i] == 1) { echo "required "; }
echo "form_item_".$fieldType[$i]."\" type=\"".$fieldType[$i]."\" id=\"".$fieldNames[$i]."\" value=\"\"";
if ($fieldLength[$i] != 0) { echo " maxlength=\"".$fieldLength[$i]."\""; }
echo "></input>";
echo "</span>";
}
}
?>
<span class="form_item">
<input class="form_item_subm" value="Add <?php echo $pageTitle; ?>" type="button" onclick="validateForm('create','3');"></input>
</span>
</form>
The first thing we do is to insert the code chunks created at page 06 and above.
The first thing we do is to import the code chunk we created at page 06. With this file comes a variable that we need: $pageName. We didn't use that variable back at page 06, but here it is needed. If you click on the Add-link in the Regions-menu this variable will hold the value "regi".
The code on this page is mainly PHP with some HTML. To make it easier to read I've highlighted all HTML for you. Variables created in the imported switchPage-file is displayed in blue while variables created in the switchTable-file is red. The orange variable is the counter within the for-loop.
The three hidden INPUT-fields are very important. The receiver-field contain the name of the processing page that should be called once the validation script has approved the submission. The table-field stores the name of the database table. This is important since all INSERT-actions, reagrdless of database table, is processed by the same file. Thirdly the fieldNames stores a string with all the table field names we want to insert data in.
Some HTML is added and then comes the for-loop that will build the form for us. The loop will run as many times as there are fields in the $fieldNames-array. That's why it's so important that you match the numer of values in the other arrays. There is a few if-statements that only outputs HTML if a certain condition is met. Otherwise the code is quite simple to follow. The \" characters is escaped quotes. The quotes belong to the HTML-part but to keep the PHP-script from interpret them as PHP-quotes they need to be escaped.
Finally a form button is added which will call the validateForm()-function when you press it. That's what we're going to do next!
Next Up: Validation script