Added: 02/02/2014 | Updated: 9 Years Ago
Overview and Example
For nearly every database table we create, we will make 2 objects. The objects will extend 2 classes:
WMTResults
WMTResultThe 2 objects will one be singular and the other plural, and extend them
These objects are used for accessing the database and common functions.
Below is an example of an object extending WMTResults:$questionSearches = new QuestionSearches();
$questionSearches->showAllFields();
$questionSearches->loadAll();
$trContent = $questionSearches->getTable($contentTRHTML);
In this example it is using the QuestionSearches() object. Searches is plural, so it is the one to use for searching.
After creating the object we execute the method showAllFields(). This makes sure that all fields from the database are loaded
Next, we load data, we use loadAll(). We can either use loadAll() or we could use: loadCustomQuery("SELECT....."). Also, we could build out custom methods for the object to load data if an object has some specific type of recurring need. If you create your own methods for your objects for loading data always have it be loadXXXX() for the name of the method.
After that, we have the code to get the results, getTable();
In this example, the content of the variable, I'll paste so you can understand.
$contentTRHTML = "~id~~question~~answfound~~added~
The ~~ are the names of the fields in the database
WMTResults
Methods/Variables
Getting The ResultsMethods that get the results from the query are named getXXXX(). You can get either objects, or a string result.
STRING: getTable($trTemplate = "")The getTable() method returns a string where ~~ is replaced with the value of each field in the database table. For example, ~id~ would be replaced with the database field called id. If $trTemplate == "" then it will use a tr/td with each field from the database as the default.
OBJECTS: getResults()This returns an array of the objects (the matching object that extends WMTResult). In order to use getResults() you must first turn objects on using the method useObjectsOn().
Custom Values In ResultsYou can customize it so that the results returned when executing a getXXX method have their numeric value swapped with a string value. For example, often in a database you store items as an number. Like yes/no is typically stored as 0/1. A time stamp is another example of a number being stored to represent something (a date). Also, when database tables have relationships with each other tyipcally this is stored as a number. There are many other examples where text is stored as the number that representing the text. There are several methods available to assist you in turning the number into the correct string in the results that are returned.
Values Based Off DateaddDate($databaseField, $dateFormat)So, now let's say we want to give values in results something other than the value in the database. For example, a time stamp needs to be converted from a number to a date you would add this line. In this example added is the name of the field in the database and m/d/y is the date format to use (same as date() function)
$questionSearches->addDate("added", "m/d/y");
Like shown below:
$questionSearches = new QuestionSearches();
$questionSearches->showAllFields();
$questionSearches->addDate("added", "m/d/y");
$questionSearches->loadAll();
$trContent = $questionSearches->getTable($contentTRHTML);
Values Based Off INDEX/ARRAYaddValueLabel("answfound", array("No", "Yes"))Now, let's say we need to add something other than date. Like in th example at the top if we want ~answfound~ to say, yes, no. We can use this command (and just add it where you added addDate()):
$questionSearches->addValueLabel("answfound", array("No", "Yes"));
So, we can use the addValueLabel to attach any array to a field in the database. And then it replaces the numeric value of the field with the key with that number inside the array.
Value Based Off MethodaddMethod($fField, $fName)This allows you to call a method to get the value.
WMTResult
Methods/Variables
Other Important Methods
Adding a New RecordTo add a new record to the database you would use the add() method. For example:
$postVals = array();
$postVals["question"] = $_POST["qnasearchquestion"];
$postVals["answfound"] = $_POST["qnasearchanswfound"];
$postVals["added"] = $_POST["qnasearchadded"];
$questionSearch = new QuestionSearch();
if (!$questionSearch->add($postVals)) exit("COULD NOT ADD");
When you use ->add() you need to pass it an array. Have each index of the array be the name of the database field that the value should be stored in.