php - 根据行中的值更改 sprintf 值

标签 php mysql int printf

我的问题是这样的

我有一个数据库,在“status”行中存储 int 值,我想表示某些订单状态:

0 = 已提交

1 = 已交付等。

我想要实现的是显示某些文本,如上面的示例,而不是 int 值

我用于显示整个列表的代码是

require 'database_connection.php';
$select_orders = "SELECT username, status, product1_num, product2_num FROM orders";

$result = mysql_query($select_orders);

<ol class="order_list">
        <?php while ($order = mysql_fetch_array($result)) {                 
            $order_row = sprintf("<li class='single_order %s'>
                <ul>
                    <li class='username'>%s</li>
                    <li class='status'>%s</li>
                    <li class='product'>%s</li>
                    <li class='product'>%s</li>
                </ul>
            </li>", 
            $order['status'], $order['username'], $order['status'], $order['product2_num'], $order['product1_num']);
                echo $order_row;
            }
        ?>
</ol>

我尝试在 sprintf 中使用 if 语句,但没有成功。

有谁知道如何更改此处显示的代码

<li class="status">3</li>

<li class="status">Delivered</li>

最佳答案

使用数组:

$status = array('Submitted', 'Delivered', ...);

然后在显示时使用 $status[$order['status']]

您也可以在 SQL 中执行此操作:

SELECT username,
    CASE status
        WHEN 0 THEN 'Submitted'
        WHEN 1 THEN 'Delivered'
        ...
    END AS status,
    ...

关于php - 根据行中的值更改 sprintf 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29002013/

相关文章:

php - 根据其中的内容高度调整 iframe 高度

javascript - jQuery 在 while 循环数据中获取来自 AJAX 的属性值

java - 时间戳插入到 sql 给我错误

Python:将字符串列表 'keys' 枚举为整数

Python Pandas 数据帧 : unorderable types: str() > int()

c# - 显然,在处理 DLLImport 的东西时使用 Int32 而不是 int 很重要吗?

php - 如何在 Zend 中重定向到不同的 Controller 操作

php - CodeIgniter get_where()与NOT IN

MySQL 查询性能 - 时间上的巨大差异

mysql - 在值的开头连接字符串