我一直在开发用于可视化某些数据的程序.我的程序从 MySQL 数据库获取特定输入并绘制一些图形(libchart 库),创建一些表格等.
I have been developing a program for the visualization of some data. My program takes specific input from a MySQL database and draws some graphs (libchart library), creates some tables etc.
我的问题是现在它是一个代码地狱.我有大约 7 个带有 HTML/CSS 和 PHP/SQL 代码的 php 文件(索引、图形页面、图库等)(其中一些只有 php 扩展名,但里面只有 HTML).我暂时阅读和理解该项目没有问题,但我想如果其他人尝试,他可能会头疼.另外,继续这样的编程是不切实际的,因为该项目将来可能不容易扩展.
My problem is that right now its a code hell in there. I have around 7 php files (index, graph-page, gallery, etc) with HTML/CSS and PHP/SQL code all together (some of them just have the php extension but have only HTML inside). I have no problem to read and understand the project for the time being, but I guess if someone else tried to, he might get a headache. Plus, continuing programming like this is not practical because the project might not be easily scalable in the future.
您对如何成功地将 HTML/CSS 与 PHP/SQL 分开有什么建议吗?我不想使用框架,因为我没有做任何需要用户输入、会话处理等的事情.我只是运行一些查询并将结果可视化.我在这里主要谈论架构,如果适用,也许可以帮助我的脚本(我已经阅读了 Smarty,但我不确定这是否是我需要的).
Do you have any suggestion on how to successfully seperate HTML/CSS from PHP/SQL? I don't want to use a framework since I'm not doing anything that requires user-input, session handling, etc. I just run some queries and visualize the results. I'm mostly talking about architecture here, and if applicable perhaps a script to help me (I've read about Smarty but I'm not sure if that's what I need).
对于如何成功地将 HTML/CSS 与 PHP/SQL 分离,您有什么建议吗?
Do you have any suggestion on how to successfully seperate HTML/CSS from PHP/SQL?
恭喜您了解如何改进代码.这是前提条件,你需要想改进它,而且这个话题很长.所以你的意愿很重要.
Congrats for looking how you can improve code. That's the precondition, you need to want to improve it and the topic is lengthy. So your will is crucial.
我很轻松地开始,然后尝试提供一些提示.如果您缺少经验,请寻找一个起点,当然是下面列表中的最后一个.但首先要注意的是:
I start lightly and then try to give some tips. As you're missing experience, look for one point to start with, most certainly the last one of the list below. But first things first:
要将某些东西彼此分开,您需要有一些代码来分开:
To separate something from each other, you need to have some code that separates:
[HTML/CSS/PHP/SQL]
[HTML/CSS] <--> [SEPARATOR] <--> [PHP/SQL]
这里的分隔符实际上也是 PHP 代码,但我想你明白了.
The Separator here actually is PHP code as well, but I think you get the idea.
正如您所看到的,只有 Separator 与 HTML/CSS 和 PHP/SQL 对话.
As you can see only the Separator talks with HTML/CSS and PHP/SQL.
所以 HTML/CSS 和 PHP/SQL 都需要有一个带有 Separator(之间的线)的接口来完成这项工作.
So both HTML/CSS and PHP/SQL need to have an interface with Separator (the line between) to make this work.
通常在程序中,您会传递经过处理的数据.数据非常动态,可能具有复合复杂性,尤其是当您将数据传递给应正确格式化的输出例程时.
Normally in a program you pass around data that get's processed. The data is pretty dynamic and can have a compound complexity, especially if you pass data to an output routine that should format it properly.
有多种方法可以编写这样的分隔符(或多个分隔符).您可以分层您的软件或提供在其区域或域中执行操作的组件.例如.你有一个数据库层或数据库组件来处理与数据库的交互.
There are multiple ways of how such a Separator (or multiple of them) can be written. You can layer your software or provide components that do things in their area or domain. E.g. you have a database layer or database component that takes care about the interaction with the database.
或者您有一个模板引擎,负责将您的字符串和数组放入一些可读的 HTML 中.
Or you have a templating engine that takes care to put your strings and arrays into some readable HTML.
简而言之,这就是软件设计的通心粉理论:
In short this is the pasta theory of software design:
就像我们在生活中吃不同的意大利面一样,在编程时我们也需要处理所有这些不同类型的代码,并且随着时间的推移我们会形成自己喜欢的口味.小时候我们吃饱,但随着时间的推移,我们开始自己做饭并改变食谱.
Like we eat different pasta in our lives, when programming we need to deal with all these different type of code as well, and we develop our own preferred taste over time. As a kid we're feed but over time we start to cook something our own and vary the recipes.
所以我认为这是一个很好的观点,你只是不想在接下来的几周内吃 MVC Framework X 和很多很棒的,只是因为有人告诉你这是现在吃的方式.在吃之前,有品尝,对吧?更不用说快餐了,你知道像这些带酱汁的面条——只加水.呃.
So I think it's a good point you just don't want to now eat MVC Framework X with much awesome for the next weeks only because somebody told you it's the way to eat now. And before eating, there is tasting, right? Not to mention fast-food, you know like these noodles with sauce in package - only add water. Urgh.
我不知道您的输出需要哪些数据以及输入是什么.以下是输出 HTML/CSS 并与 MySQL 数据库交互的应用程序的一些粗略重构技巧.这不能是一个完整的列表,描述只能粗略地勾勒出一些想法:
I don't know which data your output needs and what the input is. Following are some rough refactoring tips for applications that output HTML/CSS and interact with a MySQL database. This can not be a complete list and the descriptions can only roughly outline some thoughts:
style
属性(如果您还有一些).这使您的 CSS 代码可重用且更加模块化.它将帮助您找到 HTML 中的缺陷并将结构 (HTML) 与演示文稿 (CSS) 分开.有效的 HTML 始于对 CSS 的有效使用,这两者结合起来非常强大,通常这已经可以减轻您的程序输出例程.foreach
.这是创建模板的一种非常简单的技术.您可以使用 PHP,因此您实际上非常灵活(只需绘制属于您的视图层和应用程序的一部分的代码的边框,例如为视图提供值).$component->getThatData()
然后以一种方式返回数据规范化形式.制作这些组件,然后使用专用的数据库组件与数据库进行对话.在您的应用程序代码(业务逻辑)中,仅使用数据库组件,最好使用您创建的对象来获取数据,因此您的主代码中不再有任何 SQL 行.style
attributes if you still have some. This makes your CSS code re-useable and more modular. It will help you to find flaws inside your HTML and to separate the Structure (HTML) from the Presentation (CSS). Effective HTML start with effective usage of CSS, those two are very powerful together and often this already will lighten your programs output routines.foreach
over subarrays. This is a very simple technique to create a template. You can use PHP for it, so you're actually really flexible (just draw the border which code belongs into your view layer and which is part of the application, e.g. providing values for the view). $component->getThatData()
which then returns data in a normalized form. Make those components then use a dedicated database component to talk over with the database. In your application code (business logic) only use the database component and preferably the objects you create to get the data, so you don't have any line of SQL any longer inside your main code.这篇关于如何在我的 PHP/SQL/HTML/CSS 代码上实现 MVC 风格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!