Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

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 Repository. That class can be initialized with 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, which 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:

  1. The whole page itself, which consists of a list of sections, with letters as titles.
  2. A "lettered section", which consists of a title (a letter) and a list of articles.
  3. 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 herethe 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:

  1. Grabbing data from the Repository
  2. Transforming that data, if necessary, for the top level, whole page view.
  3. Calling the whole page view, feeding in the data, and returning the resulting html

This would be called something like MonographListBuilder, or AssembleMonographList.

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 Repository. That class can be initialized with 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, which 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:

  1. The whole page itself, which consists of a list of sections, with letters as titles.
  2. A "lettered section", which consists of a title (a letter) and a list of articles.
  3. 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:

  1. Grabbing data from the Repository
  2. Transforming that data, if necessary, for the top level, whole page view.
  3. Calling the whole page view, feeding in the data, and returning the resulting html

This would be called something like MonographListBuilder, or AssembleMonographList.

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 Repository. That class can be initialized with 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, which 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:

  1. The whole page itself, which consists of a list of sections, with letters as titles.
  2. A "lettered section", which consists of a title (a letter) and a list of articles.
  3. 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:

  1. Grabbing data from the Repository
  2. Transforming that data, if necessary, for the top level, whole page view.
  3. Calling the whole page view, feeding in the data, and returning the resulting html

This would be called something like MonographListBuilder, or AssembleMonographList.

Source Link
Jonah
  • 4.4k
  • 15
  • 23

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 Repository. That class can be initialized with 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, which 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:

  1. The whole page itself, which consists of a list of sections, with letters as titles.
  2. A "lettered section", which consists of a title (a letter) and a list of articles.
  3. 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:

  1. Grabbing data from the Repository
  2. Transforming that data, if necessary, for the top level, whole page view.
  3. Calling the whole page view, feeding in the data, and returning the resulting html

This would be called something like MonographListBuilder, or AssembleMonographList.