如何在 PHP Web 应用程序中管理度量单位? [英] How to manage measurement units in a PHP web application?

查看:56
本文介绍了如何在 PHP Web 应用程序中管理度量单位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 LAMP 网络应用程序,该应用程序使用不同的测量单位管理大量数据.这个 php 应用程序使用自定义 MVC 框架.我们现在在不同国家/地区拥有客户,我们希望为客户提供公制、英制或组合之间的选择.

I am working on a LAMP web application which is managing a lot of data with different measurement units. This php application use a custom MVC framework. We now have customers in different countries and we would like to offer the choice to the customer between metric, imperial or combination.

目前,所有数据均以国际单位保存在我们的 MySQL 数据库中.一些表有多个包含不同单位的数据的列.以客户(用户)选择的单位管理所有数据显示的最佳数据库架构是什么?

Currently, all the data are saved in international unit in our MySQL database. Some tables have multiple columns containing data with different units. What would be the best DB architecture to manage the display of all the data in the unit chosen by the customer (the user)?

是否有任何可以帮助我们的 php 类、php 函数或 Web 服务?

Is there any php classes, php function or web service which could help us?

谢谢!

更新:

如果我决定使用像 Zend Framework 类这样的 UnitConverter,那么为我的数据库中的每个数据定义单元的最佳方法是什么?

If I decide to use a UnitConverter like the Zend Framework class, what would be the best way to define the unit for each data that I have in my Database?

  • 一个新表measurement_unit"包含我的数据库中每种数据类型的测量类型?
  • 我是否应该在我的数据库中的每个数据列旁边添加一个新的measurement_type"列?

推荐答案

这个问题的解释有点含糊,不清楚应用的范围,但是从系统架构的角度来说,如果你是使用 MVC 框架,您可能希望保持所有数据库单元相同并创建一个 UnitConversion 控制器.该控制器将标准单位作为输入并根据所需单位输出一个值.将在数据库中的用户/客户端记录中保留一个标志,让您知道他们喜欢哪个单位,这样您就不会在两次登录之间丢失此信息.将标准单位格式(比如米)的值和所需单位的标志(比如英尺")传入你的控制器,让它进行转换并返回一个值.

The explanation of the problem is a bit vague and I'm not sure the scope of the application, but from a system architecture point of view, if you are using a MVC framework, you would probably want to keep all of your database units the same and create a UnitConversion Controller. This Controller would take the standard unit as input and output a value based on the unit desired. A flag would be kept on your user/client record in the database to let you know which Unit they prefer so you don't lose this information between logins. Pass in the value in a standard unit format (say, meters) and the desired unit's flag (say, 'FEET') into your Controller and let it do the conversion and return a value.

我不会尝试在数据库中保留不同的单位类型,因为您最终可能会编写各种代码来尝试管理异常和维护(例如,当客户更改单位时更新所有值).在数据库中保留一个标准单位,并通过类似于 Robert 提到的 Zend Framework 的 php 类进行转换.谷歌搜索php 单位转换"将显示一些可能满足您需求的类.

I would not try to keep different unit types in the database as you will likely end up writing all kinds of code trying to manage the exceptions and maintenance (updating all of the values when clients change their units, for example). Keep a standard unit in the database and do the conversions via a php class similar to how the Zend Framework mentioned by Robert does. Googling "php unit conversion" will bring up some classes that may suite your needs.

更新中:

仍然不确定我是否看到了整个问题,但我会尽量尽我所能回答.和以前一样,最好在数据库中保留 1 个单位系统,例如公制.user_pref 中的measurement_type 告诉客户想要什么,比如'IMPERIAL'.根据您的数据库的分布情况,您可以选择两种解决方案之一来保存值:

Still not sure I'm seeing the entire problem, but I'll try to answer as best as I understand. As before it is best to keep 1 unit system in the database, say, metric. The measurement_type in user_pref tells what the client wants, say 'IMPERIAL'. Depending on how spread out your database is you might choose one of two solutions to hold values:

  1. 对于数据库中的项目,您可能具有不同的属性(列),例如重量、高度、体积等.

  1. For items in your DB you might have different properties (columns), like weight, height, volume, etc.

您可能有一个 Item 表来保存项目.然后你有一个包含属性的属性表.Property 表有 4 列:property_id(primary key)、property(HEIGHT、WIDTH、LENGTH、WEIGHT)、property_type(SIZE,MASS,VOLUME,AWESOMENESS) 和 value.然后你有一个 Property_Lookup 表,它有 2 列:item_id、property_id 和这 3 个表之间的连接将为你提供属于一个项目的每个属性的所有值和单位类型.在此模式中,我仍会将值"列中的所有条目保留在单个单位系统中(在此示例中为指标).有关多对多关系的更多信息,请参阅此链接(http://www.tomjewett.com/dbdesign/dbdesign.php?page=manymany.php).

You might have an Item table which holds items. Then you have a Property table which holds properties. The Property table has 4 columns: property_id(primary key), property(HEIGHT, WIDTH, LENGTH, WEIGHT), property_type(SIZE,MASS,VOLUME,AWESOMENESS), and value. Then you have a Property_Lookup table which has 2 columns: item_id, property_id and a join between these 3 tables will give you all of the values and unit types of each property belonging to an Item. In this schema I would still keep all of the entries in the 'value' column in a single unit system (in this example metric). See this link for more on many-to-many relationships (http://www.tomjewett.com/dbdesign/dbdesign.php?page=manymany.php).

您的模型将检索数据并将这些属性封装在一个单元 { system(METRIC*,IMPERIAL,BOTH);类型(大小,质量,体积);价值 } 迷你模型.将其传递给您的控制器.在渲染时,您的 View 将期望基于客户端所需的单位值,因此当您的 Controller 为您的 View 汇总数据时,它将通过 UnitConversion 库发送 Unit 对象.UnitConversion 库将检查客户首选系统的用户模型和 Unit 模型中的系统"并进行必要的转换(因为库可以假设 Unit 模型中的系统在来自数据库时是公制的,它使这一步更容易).然后它将以正确的单位(如果选择 BOTH 则为单位)输出一个数字,该数字可以传递给视图.

Your Models will retrieve data and encapsulate these properties in a Unit { system(METRIC*,IMPERIAL,BOTH); type(SIZE,MASS,VOLUME); value } mini-Model. Pass that to your Controller. On render your View will expect a unit value based on what the client wants, so when your Controller is putting together data for your View it will send Unit objects through the UnitConversion Library. The UnitConversion Library will check the User Model for the client's prefered system and the 'system' in the Unit model and make the necessary conversion (since the Library can assume the system in the Unit model is metric when coming from the database, it makes this step a little easier). It will then output a number in the correct unit (units if BOTH is chosen), which can be passed on to the View.

上面的一句话是,在处理系统架构时总是没有正确"的问题解决方案.这就是我根据给定的信息组织事物的方式,但您可能需要对其进行大量调整以使其完全适合您正在使用的内容.也就是说,我会调整上述内容以在您的系统中工作,而不是调整您的系统以使上述内容正常工作!希望这能给你一些好主意.

A quick word on the above is that always when dealing in system architecture there is no 'correct' solution to a problem. This is how I would organize things based on the information given, but you will probably need to tweak it quite a bit to get it to fit in perfectly with what you're working with. That said, I would tweak the above to work in your system, and not tweak your system to get the above to work! Hope this gives you some good ideas.

这篇关于如何在 PHP Web 应用程序中管理度量单位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆