Working with Databases In Figaro
All of your php code (and Figaro shorthand) will be placed inside your Root level pages. Make sure you have configured your database settings on lines 43-46 in config.Settings.php.
There are two very useful ways to connect and pull information from your database without having to write a line of SQL. The two most useful are get(); and getBY();
Using get();
The get(); function returns all or some of the fields from the table within your database as an array. All arguments are optional and if there are no arguements then it fetches the entire table.
$Database->table_name->get($fields = array(), $conditions = null, $orderBy = null, $limit = null)
Practical Uses:
- $Mysql->tablename->get(); will return all of the fields in the table you specify.
- $Mysql->tablename->get('fieldname1, fieldname2'); will return only fieldname1 and fieldname2.
- get(); can take several arguments - $Mysql->tablename->get('field1, field2, field3', 'field4 = 3', 'field5 ASC', '4'); will return field1, field2, field3 where field 4 = 3 ordering by field5 ascending and it will limit the number of items returned to 4.
Using getBY();
The getBY(); function returns all or some of the fields from the table within your database as an array conditionally based on the table name used. I.E. getBYfieldname('1'); would return all of the fields in the database where fieldname = 1. Figaro automatically knows what all of your fieldnames are so you automatically have access to a getBY function for each table name in your database. Proper use: $Mysql->tablename->getBYfieldname('condition');
Looping Thru Arrays
The foreach loop is the basis for most of your workload in Figaro.
//define your page template
$Page = new Page('path to template');
//define your content element
$Page->defineElement($thisPage, 'path to content element');
//use get to pull info from the db
$var = $Mysql->tablename->get('field1');
//loop thru the array using the names of the fields ($data['fieldname'])
foreach ($var as $row=>$data)
{
//use the .= to add each loops output to your variable otherwise only one result
$varList .= $data['field1'];
}
//send varList to your pointer in your content element
$Page->$thisPage->addContent('RIGHTCOLUMN', $varList);
//render the page
$Page->render();