Xdms was created by Abu Bakar Al-Idrus to add some simple HTML-related functionality to Solar
At the time of writing, this repository contains a Table class and a Table View Helper class as a start. Other classes will be added on as time goes by. This is a guide for using the classes in this repository.
Xdms_Sql_Array is a class for converting back and forth between a PHP array and a PostgreSQL array value. This class however only works only on a single dimension array.
Converting from a PHP array into an SQL array:
$sql_array = Solar::factory('Xdms_Sql_Array');
// create a new array
$orig_array = array(
'hello world',
'backslashed \ entry',
'backslashed \" entry',
'alidrusworld',
'alidrus\'s world',
'alidrus\'s "wonderful" world',
);
// set the values from the array
$sql_array->set($orig_array);
// fetch result and put it into $result
$result = $sql_array->fetch();
// display result
echo "$result\n";
Results in the following:
[0:5]={"hello world","backslashed \\ entry","backslashed \\\" entry",alidrusworld,"alidrus's world","alidrus's \"wonderful\" world"}
Lets say, you want to do the opposite. For this example, lets say we have created a table (in PostgreSQL) like so:
CREATE TABLE test_array (id bigint NOT NULL, array_field text[]);
Assume, we have inserted a row into the table from the example above into the array_field column and are now retrieving it using Solar_Sql_Select.
// select from db
$select = Solar::factory('Solar_Sql_Select');
$result = $select->from('test_array', array('array_field'))
->where('id = 1')
->fetchAll();
$result = array_shift($result);
$result = array_shift($result);
echo "result from db:\n$result\n";
Resulting in the following output (deja vu) which is obviously not the way you want your data to be represented:
result from db:
[0:5]={"hello world","backslashed \\ entry","backslashed \\\" entry",alidrusworld,"alidrus's world","alidrus's \"wonderful\" world"}
Using the get() method:
// reset the array first $sql_array->reset(); // use sql array string to get an array $sql_array->get($result); // show new array from sql array string echo "new_array:\n"; print_r($sql_array->fetch(true));
And voila, we now have the original array we had originally:
new_array:
Array
(
[0] => hello world
[1] => backslashed \ entry
[2] => backslashed \" entry
[3] => alidrusworld
[4] => alidrus's world
[5] => alidrus's "wonderful" world
)
This class merely creates a table hint for (possible) use with Xdms_View_Helper_Table
// Load the Xdms_Table object via factory
$table = Solar::factory('Xdms_Table');
// Set the <table> tag attributes
$table->setAttribs(array(
'cellspacing' => 1,
'cellpadding' => 1,
'style' => 'margin: 5px auto 5px 5px;',
));
// Set the table caption
$table->setCaption(
'Address Book',
array('style' => 'text-decoration: underline;'
));
// Add a row into table head
$table->setHeadRow(
// column specification array
array(
// column 1 only has content
"Name",
// column 2 has content and attribs
array(
'content' => 'Email',
// override the default cell style
'attribs' => array('style' => 'background: #ffcccc;'),
// do not use <th>, meaning <td> is used instead
'use_th' => false,
),
// column 3 has only content
"Address",
),
// default attribs for all <th> and <td>
array('style' => 'background: #ffffff;',)
);
// Columns may be added without any attributes too.
$table->setBodyRow(array(
'Alfred E. Oldman',
'oldman_randomshit@gmail.com',
'13A, North Tyndall Ave, Utopia, CA 90028.',
));
// Lets show the world what we have stored in the table array
echo "<pre>\n"
.print_r($table->getTable(), true)
."</pre>\n";
As expected, the code above doesn't render a table right out but rather dumps the array containing the table structure. This is where the Xdms_View_Helper_Table class comes in to the rescue.
Replacing the last echo in the example code above with the following code;
echo $this->table($table);
produces the following HTML markup:
<table cellspacing="1" cellpadding="1" style="margin: 5px auto 5px 5px;">
<caption style="text-decoration: underline;">Address Book</caption>
<thead>
<tr>
<th style="background: #ffffff;">Name</td>
<td style="background: #ffcccc;">Email</td>
<th style="background: #ffffff;">Address</td>
</tr>
</thead>
<tbody>
<tr>
<td>Alfred E. Oldman</td>
<td>oldman_randomshit@gmail.com</td>
<td>13A, North Tyndall Ave, Utopia, CA 90028.</td>
</tr>
</tbody>
</table>
Like most things in life, there's more than one way to do things.
echo $this->table(array(
'cellspacing' => 1,
'cellpadding' => 1,
'style' => 'margin: 5px auto 5px 5px;',
))
->addCaption('Address Book', array('style' => 'text-decoration: underline;'))
->addHeadRow(
array(
"Name",
array(
'content' => 'Email',
'attribs' => array('style' => 'background: #ffcccc;'),
'use_th' => false,
),
"Address"
),
array('style' => 'background: #ffffff;')
)
->addBodyRow(array(
'Alfred E. Oldman',
'oldman_randomshit@gmail.com',
'13A, North Tyndall Ave, Utopia, CA 90028.',
))
->fetch();
The code above produces exactly the same HTML markup as in the Xdms_Table example.