I did this for some restricted database access, and other than not having the API functions available, it worked fine. You can write it as any .php file, with more includes and whatever you would usually do. I just returned everything at the end, as you would do in a snippet; I didn’t use a function in the snippet, just an include statement. I suspect that the final return statements are redundant, but did it in both places anyway just in case. Doesn’t seem to hurt anything.
include "./assets/snippets/termbase/termbase.inc.php";
return $output;
And in the included .php file, I included manager/includes/config.inc.php and wrote the code:
...
// get the languages available in the database
function getLanguages() {
include "./manager/includes/config.inc.php";
$db = mysql_connect($database_server,$database_user,$database_password) or die("cannot connect to database");
$selected = @mysql_select_db($dbase, $db) or die ("cannot select database");
$query = "SHOW COLUMNS FROM tb_termbase";
$result = mysql_query($query) or die ("Cannot query the database");
$languages = array();
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$languages[] = $row['Field'];
}
}
$languages = array_slice($languages, 2);
return $languages;
}
...
// start drawing the result table
$output .= "<table class='wide' id='glossary' border='0' cellpadding='0' cellspacing='0'>";
$output .= "<tr>";
foreach($display_languages as $language) {
$output .= "<th>$language</th>";
}
$output .= "</tr>";
for($j=0;$j<$rows;$j++) {
$output .= "<tr>";
$list = mysql_fetch_row($result);
for($i=0;$i<count($list);$i++) {
$style = $j%2 == 0 ? 'data1' : 'data2';
$output .= "<td class='data $style'>$list[$i]</td>";
}
$output .= "</tr>";
}
$output .= "</table>";
etc. etc.
return $output;
?>
This is why I was interested in a separate database abstraction class file.