将查询结果集列整数值映射到相关字符串
我通过查询从数据库中获取一些数据,然后通过 while 循环循环它:
$r = mysql_query("SELECT * FROM advertisement_packages");
while ($a = mysql_fetch_assoc($r)):
echo $a['exposure]; //This prints out 1,2,3,4
endwhile;
我该怎么做,所以:
- 曝光 = 1 = 迷你
- 曝光 = 2 = 标准
等等。
I get some data from my database, with a query and then I loop it via a while loop:
$r = mysql_query("SELECT * FROM advertisement_packages");
while ($a = mysql_fetch_assoc($r)):
echo $a['exposure]; //This prints out 1,2,3,4
endwhile;
How can I do, so:
- exposure = 1 = Mini
- exposure = 2 = Standard
etc. etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
独立存储值而不是使用(可能)巨大的
if
结构可能会有所帮助:只需我的 0.02 美元即可使代码更清晰一些。
It might be helpful to store the values independently instead of using a (possibly) enormous
if
structure:Just my $0.02 to make the code a bit more legible.
**另一个好方法是在数据库中维护一个表结构,将暴露编号与其描述相关联。通过这种方式,您可以直接从数据库显示曝光描述(您还可以从 Exposure_info 表填充 Web 表单上的下拉菜单)。就按照示例SQL来看看你是否理解了。 **
创建表 photo_clients(
id INT 自动递增主键,
客户端名称 VARCHAR(100),
曝光TINYINT
);
创建表exposure_info(
暴露INT AUTO_INCRMENT PRIMARY KEY,
描述 VARCHAR(100)
);
INSERT INTO Exposure_info(描述)VALUES('迷你'),('标准'),('海报');
INSERT INTO photo_clients (client_name, 曝光) VALUES ('John Doe', 2), ('Jane Smith', 1), ('Johnny Walker', 3);
SELECT a.client_name AS 客户端,b.description AS 曝光 FROM photo_clients a,exposure_info b WHERE a.exposure = b.exposure;
上述语句的输出应类似于:
**Another good way to do this is to maintain a table structure in the database that associates the exposure number to its description. This way you can display the exposure description straight from your database (you can also populate a drop-down menu on web forms from the exposure_info table). Just follow the example SQL to see if you understand. **
CREATE TABLE photo_clients(
id INT AUTO_INCREMENT PRIMARY KEY,
client_name VARCHAR(100),
exposure TINYINT
);
CREATE TABLE exposure_info(
exposure INT AUTO_INCREMENT PRIMARY KEY,
description VARCHAR(100)
);
INSERT INTO exposure_info (description) VALUES ('mini'), ('standard'), ('poster');
INSERT INTO photo_clients (client_name, exposure) VALUES ('John Doe', 2), ('Jane Smith', 1), ('Johnny Walker', 3);
SELECT a.client_name AS client, b.description AS exposure FROM photo_clients a, exposure_info b WHERE a.exposure = b.exposure;
The output of the above statement should look something like: