patternphpMinor
HTML content from database results
Viewed 0 times
databasecontentresultsfromhtml
Problem
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:
All comments gratefully received.
Within the HTML page:
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,
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 $apcDatabasewas 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
extractingetDocuments. I've read that indiscriminate use ofextractcan 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:
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,
Solution
To elaborate on my comment, building html from raw data, on the one hand, and fetching data from a
database, on the other, are separate concerns.
So your first step should be splitting out the interaction with the database
into a single class, let's call it
with your database connection details, and then it will be the only class
that needs to know about those.
returns the data you'll need for this report. You can of course add other methods for different
kinds of pages or reports that you may need in the future.
As for your view, let's break it down into it's logical sections:
I would break down your views correspondingly.
Consider using a template engine for creating views -- php has many.
However, in your case, the views are so simple, I'd consider using an extremely lightweight solution like the one described here. I've used it myself, it's a single short file include, and it works great.
You'd create a template for each of the three sections listed above. The whole page would make use of the "letter section" template, and the letter section template would make use of the individual article template.
Finally, you'd need one extremely simple class whose job would be to wire together these elements:
This would be called something like
database, on the other, are separate concerns.
So your first step should be splitting out the interaction with the database
into a single class, let's call it
Repository. That class can be initializedwith your database connection details, and then it will be the only class
that needs to know about those.
Repository can have a static method like apcReportData, whichreturns the data you'll need for this report. You can of course add other methods for different
kinds of pages or reports that you may need in the future.
As for your view, let's break it down into it's logical sections:
- The whole page itself, which consists of a list of sections, with letters as titles.
- A "lettered section", which consists of a title (a letter) and a list of articles.
- An individual article, which consists of a tag, an article title, and a summary.
I would break down your views correspondingly.
Consider using a template engine for creating views -- php has many.
However, in your case, the views are so simple, I'd consider using an extremely lightweight solution like the one described here. I've used it myself, it's a single short file include, and it works great.
You'd create a template for each of the three sections listed above. The whole page would make use of the "letter section" template, and the letter section template would make use of the individual article template.
Finally, you'd need one extremely simple class whose job would be to wire together these elements:
- Grabbing data from the Repository
- Transforming that data, if necessary, for the top level, whole page view.
- Calling the whole page view, feeding in the data, and returning the resulting html
This would be called something like
MonographListBuilder, or AssembleMonographList.Context
StackExchange Code Review Q#112974, answer score: 2
Revisions (0)
No revisions yet.