用变量替换 php 中的重复文件引用?

Replace repeated file reference in php with a variable?

提问人:Jozef Pietrzak 提问时间:7/9/2023 更新时间:7/9/2023 访问量:29

问:

我有一些代码可以对PHP表中的数据进行排序,但它必须引用它所在的文件名,如下所示:

重复的“joey_all”可以被变量代替吗? 我需要经常复制和修改此文件,并且需要能够快速将名称更改为新文件名。 在这种情况下,这是如何做到的?

<body>
            <table>
                <tr>
                    <th><a href="joey_all.php?column=status&order=<?php echo $asc_or_desc; ?>">Status<i class="fas fa-sort<?php echo $column == 'status' ? '-' . $up_or_down : ''; ?>"></i></a> </th>
                    <th><a href="joey_all.php?column=ordered_by&order=<?php echo $asc_or_desc; ?>">Ordered By<i class="fas fa-sort<?php echo $column == 'ordered_by' ? '-' . $up_or_down : ''; ?>"></i></a> </th>
                    <th><a href="joey_all.php?column=order_date&order=<?php echo $asc_or_desc; ?>">Order Date<i class="fas fa-sort<?php echo $column == 'order_date' ? '-' . $up_or_down : ''; ?>"></i></a> </th>
                    <th><a href="joey_all.php?column=cart_number&order=<?php echo $asc_or_desc; ?>">Cart Number<i class="fas fa-sort<?php echo $column == 'cart_number' ? '-' . $up_or_down : ''; ?>"></i></a></th>
                    <th><a href="joey_all.php?column=po_cc&order=<?php echo $asc_or_desc; ?>">PO<i class="fas fa-sort<?php echo $column == 'po_cc' ? '-' . $up_or_down : ''; ?>"></i></a></th>
                    <th><a href="joey_all.php?column=vendor&order=<?php echo $asc_or_desc; ?>">Vendor<i class="fas fa-sort<?php echo $column == 'vendor' ? '-' . $up_or_down : ''; ?>"></i></a></th>
                    <th><a href="joey_all.php?column=item_number&order=<?php echo $asc_or_desc; ?>">Item#<i class="fas fa-sort<?php echo $column == 'item_number' ? '-' . $up_or_down : ''; ?>"></i></a></th>
                    <th><a href="joey_all.php?column=description&order=<?php echo $asc_or_desc; ?>">Description<i class="fas fa-sort<?php echo $column == 'description' ? '-' . $up_or_down : ''; ?>"></i></a></th>
                    <th><a href="joey_all.php?column=take_to&order=<?php echo $asc_or_desc; ?>">Take To<i class="fas fa-sort<?php echo $column == 'take_to' ? '-' . $up_or_down : ''; ?>"></i></a></th>
                    <th><a href="joey_all.php?column=final_location&order=<?php echo $asc_or_desc; ?>">Final Location<i class="fas fa-sort<?php echo $column == 'final_location' ? '-' . $up_or_down : ''; ?>"></i></a></th>
                    <th><a href="joey_all.php?column=received_special_notes&order=<?php echo $asc_or_desc; ?>">Receiving Instructions<i class="fas fa-sort<?php echo $column == 'received_special_notes' ? '-' . $up_or_down : ''; ?>"></i></a></th>
                    <th><a href="joey_all.php?column=received_by&order=<?php echo $asc_or_desc; ?>">Received By<i class="fas fa-sort<?php echo $column == 'received_by' ? '-' . $up_or_down : ''; ?>"></i></a></th>
                    <th><a href="joey_all.php?column=date_received&order=<?php echo $asc_or_desc; ?>">Date Received<i class="fas fa-sort<?php echo $column == 'date_received' ? '-' . $up_or_down : ''; ?>"></i></a></th>
                    <th><a href="joey_all.php?column=carrier&order=<?php echo $asc_or_desc; ?>">Carrier<i class="fas fa-sort<?php echo $column == 'carrier' ? '-' . $up_or_down : ''; ?>"></i></a></th>
                    <th><a href="joey_all.php?column=confirmed&order=<?php echo $asc_or_desc; ?>">Ariba Confirmed<i class="fas fa-sort<?php echo $column == 'confirmed' ? '-' . $up_or_down : ''; ?>"></i></a></th>
                    <th><a href="joey_all.php?column=capital_eq&order=<?php echo $asc_or_desc; ?>">Capital EQ<i class="fas fa-sort<?php echo $column == 'capital_eq' ? '-' . $up_or_down : ''; ?>"></i></a></th>
                </tr>
                <?php while ($row = $result->fetch_assoc()): ?>
                <tr>
                    <td<?php echo $column == 'status' ? $add_class : ''; ?>><?php echo $row['status']; ?></td>
                    <td<?php echo $column == 'ordered_by' ? $add_class : ''; ?>><?php echo $row['ordered_by']; ?></td>
                    <td<?php echo $column == 'order_date' ? $add_class : ''; ?>><?php echo $row['order_date']; ?></td>
                    <td<?php echo $column == 'cart_number' ? $add_class : ''; ?>><?php echo $row['cart_number']; ?></td>
                    <td<?php echo $column == 'po_cc' ? $add_class : ''; ?>><?php echo $row['po_cc']; ?></td>
                    <td<?php echo $column == 'vendor' ? $add_class : ''; ?>><?php echo $row['vendor']; ?></td>
                    <td<?php echo $column == 'item_number' ? $add_class : ''; ?>><?php echo $row['item_number']; ?></td>
                    <td<?php echo $column == 'description' ? $add_class : ''; ?>><?php echo $row['description']; ?></td>
                    <td<?php echo $column == 'take_to' ? $add_class : ''; ?>><?php echo $row['take_to']; ?></td>
                    <td<?php echo $column == 'final_location' ? $add_class : ''; ?>><?php echo $row['final_location']; ?></td>
                    <td<?php echo $column == 'received_special_notes' ? $add_class : ''; ?>><?php echo $row['received_special_notes']; ?></td>
                    <td<?php echo $column == 'received_by' ? $add_class : ''; ?>><?php echo $row['received_by']; ?></td>
                    <td<?php echo $column == 'date_received' ? $add_class : ''; ?>><?php echo $row['date_received']; ?></td>
                    <td<?php echo $column == 'carrier' ? $add_class : ''; ?>><?php echo $row['carrier']; ?></td>
                    <td<?php echo $column == 'confirmed' ? $add_class : ''; ?>><?php echo $row['confirmed']; ?></td>
                    <td<?php echo $column == 'capital_eq' ? $add_class : ''; ?>><?php echo $row['capital_eq']; ?></td>
                </tr>
                <?php endwhile; ?>
            </table>
        </body>
    </html>
    <?php
    $result->free();
}

我知道我们可以使变量像这样:

$filename = "joey_all";

我不明白我的变量是否需要用引号、单引号、括号或其他东西来解释这里是否正确?$filename

PHP 变量

评论


答:

1赞 Gandalf the Gay 7/9/2023 #1

是的,你可以用PHP变量替换你的文件名。


溶液

只需准备一个带有文件名的变量,然后使用它,例如,类似于代码中的其他情况。$filename<?php echo $filename; ?>

下面是一个带有示例的简短代码片段:

<?php
    $filename = 'joey_all';
?>

  <body>
    <table>
      <tr>
        <th><a href="<?php echo $filename; ?>.php?column=status&order=<?php echo $asc_or_desc; ?>">Status<i class="fas fa-sort<?php echo $column == 'status' ? '-' . $up_or_down : ''; ?>"></i></a> </th>
        <th><a href="<?php echo $filename; ?>.php?column=ordered_by&order=<?php echo $asc_or_desc; ?>">Ordered By<i class="fas fa-sort<?php echo $column == 'ordered_by' ? '-' . $up_or_down : ''; ?>"></i></a> </th>
        <th><a href="<?php echo $filename; ?>.php?column=order_date&order=<?php echo $asc_or_desc; ?>">Order Date<i class="fas fa-sort<?php echo $column == 'order_date' ? '-' . $up_or_down : ''; ?>"></i></a> </th>

        /* The rest of your code */

      </tr>
    </table>
  </body>


知识

我不明白我的变量$filename是否需要用引号、单引号、括号或其他东西来解释这里正确?

在你展示的代码中查看你的情况,你用 HTML 编写表格及其内容,所以如果你想将其中的 PHP 变量用于任何目的,最简单的方法是在适当的位置注入一个 PHP 片段。在这种情况下,您如何声明,或者使用引号或单引号并不重要,因为它将被转换为字符串,并且您只需在此变量中存储文本。对于引号应该使用的另一个目的,您可以阅读PHP手册echo$filename