This code produces HTML from the results of a database query.
Self-taught, I have concerns about developing bad habits and I wrestle with the principles of OOP and MVC.
I would be grateful for your opinion on a number of issues:
- The use of
global
. My HTML page is generated from a template which requires a config file that contains the database connection. Addingglobal $apcDatabase
was a workaround but I don't understand why the connection was not present. - The HTML code is returned from the PHP class and then displayed with an echo statement. Is this MVC separation?
- The use of
extract
ingetDocuments
. I've read that indiscriminate use ofextract
can be problematic particularly in code review and finding out where variables have come from. I've backed myself into a corner with this and can't see an alternative way.
All comments gratefully received.
Within the HTML page:
<?php
global $apcDatabase;
require_once(LIBRARY_PATH . "/populate_documents.php");
$recommendation = new ApcDocs($apcDatabase,'recommendation');
$documents = $recommendation->getDocuments();
foreach ($documents as $document) {
echo $recommendation->addSymbols($document['header'])
. $recommendation->addSymbols($document['label'])
. $recommendation->addSymbols($document['title'])
. $recommendation->addSymbols($document['text']);
}
?>
Supporting PHP code:
class ApcDocs
{
private $apcDatabase;
private $documentType;
private $documents;
private $firstLetter;
private $lastLetter;
const URL = "http://www.website.com/";
const RECOMMENDATION = "folder/path/";
public function __construct($apcDatabase, $documentType)
{
$this->apcDatabase = $apcDatabase;
$this->documentType = $documentType;
$this->docQuery();
$this->docResults();
}
private function docQuery()
{
$definitions = array(
'recommendation' => "SELECT rag_status.rag_status,
rag_status.rag_label,
apc_documents.document_title,
apc_documents.document_text,
apc_documents.file_name
FROM apc_documents
JOIN rag_status
ON apc_documents.rag_status_id_fk = rag_status.rag_status_id
WHERE document_type_id_fk = 1
ORDER BY apc_documents.document_title ASC"
);
$this->apcDatabase->query($definitions[$this->documentType]);
}
private function docResults()
{
$this->documents = $this->apcDatabase->resultSet();
}
public function getDocuments()
{
$docDetails = array();
$this->lastLetter = null;
foreach ($this->documents as $document => $details) {
extract($details, EXTR_OVERWRITE); // create variables from $details
$this->firstLetter = $this->getFirstLetter($document_title);
$docDetails[$document]['header'] = $this->headerCode();
$docDetails[$document]['label'] = $this->labelCode($rag_label, $rag_status);
$docDetails[$document]['title'] = $this->linkCode(self::URL . self::RECOMMENDATION . $file_name,
$document_title);
$docDetails[$document]['text'] = $this->docText($document_text);
$this->lastLetter = $this->firstLetter;
}
return $docDetails;
}
private function getFirstLetter($wordToCheck)
{
return substr($wordToCheck, 0, 1);
}
private function headerCode()
{
if ($this->sameHeader()) {
return FALSE;
} else {
return "<div class='page-header' id='$this->firstLetter'><h1>$this->firstLetter</h1></div>\n";
}
}
private function sameHeader()
{
return $this->firstLetter == $this->lastLetter;
}
private function labelCode($label, $staus)
{
return "<span class='label label-$label'>$staus</span> ";
}
private function linkCode($link, $title)
{
return "<a href='$link'>$title</a>\n";
}
private function docText($text)
{
return "<p>$text</p>\n";
}
public function addSymbols($text)
{
$superscript = str_replace("(r)", "<sup>®</sup>", $text);
return str_replace("(bt)", "▼", $superscript);
}
}
?>