Is it possible to store presentation data in a database?

I have to display rows from a database table (SQL SERVER 2005) on a web page. These lines contain statusID (foreign key), which is then determined by the status table (for example, id, name, modifiedDate).

Different statuses should be displayed differently (say, they just have a different background color).

I use php to query the database and create a table of web pages. When I create the table, I am going to apply the css class to the element based on the status of this row.

I have at least 2 options:

  • Define the code logic in php to process it, and if the states are changed in the database, I will have to change the code.

  • Save the β€œclass” in the database and simply apply the class that was saved.

The latter option seems better to me, but I'm not sure if embedding presentation data in a database is a poor design choice. This will be the basis on which I create several intranet utilities, and I would like to start with the right foot.

+8
css sql database php sql-server-2005
source share
4 answers

There is nothing wrong with storing any data in a database, including presentation data. If this helps you get effective results while writing less code, then this is good practice. You need to make sure that you are not confusing your presentation logic with the database logic.

You can make sure that these problems are separated by encapsulating the data for your presentation level in the properties of the elementInfo object (for example).

Since this is the CSS class you are talking about, this presentation data should be stored separately from business data. . Thus, while it is normal to store both presentation data and business data in the database, it is unacceptable to store them in one table.

Refresh re: comment No, you should not add the PresentationClassRecord identifier as the FK for the business object. I made a sample db approach below. I called DummyTable your business objects, and the rest are specifications. The most important part is StatusPresentationAssignmentTable

  ----------------------------------------------- DummyTable ----------------------------------------------- Id Name SomeOtherDataField StatusId PK int varchar int FK int ----------------------------------------------- StatusTable ----------------------------------------------- Id Name ModifiedDate PK int varchar datetime ----------------------------------------------- PresentationTable ----------------------------------------------- Id PresentationType Value PK int varchar sample data: 43 CssClass prettyBackground ----------------------------------------------- StatusPresentationAssignmentTable ----------------------------------------------- StatusId PresentationId FK int FK int 

Now with two simple connection conditions, you can get presentation data and completely separate it from your business data. Your script might do something like checking if the Dummy status has any presentation purpose. If so, he looks at PresentationType, gets the appropriate function for applying presentation data to the presentation, and executes it. (You will need to have a function for each PresentationType that knows how to handle the value - something that can be encapsulated with something like function applyPresentationValue(presentationElement, presentationType, presentationValue) , which calls another function applyCssClass(presentationElement, value) if presentationType == "CssClass" ).

+4
source share

The class itself does not represent presentation data. Its just a shortcut for each type of status. Theoretically, you could use it for many other purposes than to determine what color should be when it appears on a web page.

When you combine this class with a given set of styles, then its presentation information. And you will do it in your CSS file, not in the database.

However, parameter 1 does not necessarily require you to change your PHP code if the conditions in the database change. Your PHP can simply generate a class name from id or name status. Your CSS should change if the name / status name has changed, but is this possible? Should each status remain constant, are new statuses added if there are changes in the states that the application should represent?

+4
source share

While you can store css class information in a database, many content management systems do this, it is probably best to make status a part of the class name.

i.e. status = open, closed use php logic to generate table rows and set the css class to status_ {name} then any time you add a new status or rename it, you only need to add / edit the css file, php conversion is not required.

.status_open{background-color:green;}

.status_closed{background-color:red;}

+3
source share

I see the advantages and disadvantages of storing this in the database. Obviously, it is convenient to have class information there, but this is not part of the application.

I would lean in order not to store it there and do something at the presentation level to process it based on state. My reasoning is that, especially since you create utilities, data can be used through the API or something later when the class is meaningless.

+1
source share

All Articles