I very much agree with
shmem above. In practice I would separate the code in two parts:
- Code that retrieves the data from database and manipulates it. SQL, argument validation, sorting, access, control, etc goes here.
- Code that presents the data to the user. User interface (e.g. HTML) generation (possibly using one of the suggested frameworks) goes here.
This is consistent with the
Model-view-controller (MVC) architectural pattern.