将查询结果集列整数值映射到相关字符串

发布于 2024-11-28 15:34:14 字数 303 浏览 1 评论 0原文

我通过查询从数据库中获取一些数据,然后通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一页 2024-12-05 15:34:14
if ($a['exposure'] == 1){
    echo "Mini";
}
elseif($a['exposure'] == 2){
    echo "Standard";
}
if ($a['exposure'] == 1){
    echo "Mini";
}
elseif($a['exposure'] == 2){
    echo "Standard";
}
墨小墨 2024-12-05 15:34:14
$labels = array(1 => 'Mini', 2 => 'Standart');
echo $labels[$a['exposure']];
$labels = array(1 => 'Mini', 2 => 'Standart');
echo $labels[$a['exposure']];
绝不放开 2024-12-05 15:34:14

独立存储值而不是使用(可能)巨大的 if 结构可能会有所帮助:

$exposures = array(
    1    => 'Mini',
    2    => 'Standard',
    // and so forth
);

while($a = mysql_fetch_assoc($r)):
    echo $exposures[ $a['exposure'] ];
endwhile;

只需我的 0.02 美元即可使代码更清晰一些。

It might be helpful to store the values independently instead of using a (possibly) enormous if structure:

$exposures = array(
    1    => 'Mini',
    2    => 'Standard',
    // and so forth
);

while($a = mysql_fetch_assoc($r)):
    echo $exposures[ $a['exposure'] ];
endwhile;

Just my $0.02 to make the code a bit more legible.

墨小墨 2024-12-05 15:34:14

**另一个好方法是在数据库中维护一个表结构,将暴露编号与其描述相关联。通过这种方式,您可以直接从数据库显示曝光描述(您还可以从 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;

上述语句的输出应类似于:

client          exposure
------------------------
Jane Smith      mini
John Doe        standard
Johnny Walker   poster

**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:

client          exposure
------------------------
Jane Smith      mini
John Doe        standard
Johnny Walker   poster
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文