Search code examples
phpvariables

Replace repeated file reference in php with a variable?


I have some code that sorts data in a table on PHP, but it has to reference the file name it is inside like so:

Can the repeated 'joey_all' be replaced by a variable? I need to replicate and modify this file frequently and need to be able to quickly change the name to the new file name. How is this done in this case?

<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();
}

I know we can make the variable like:

$filename = "joey_all";

I am not understanding if my variable $filename needs to be in quotes, single quotes, parenthesis, or something else to be interpreted here correctlty?


Solution

  • Yes, you can replace your filename with a PHP variable.


    Solution

    Just prepare a variable $filename with the name of your file and then use it e.g. similar as in another cases in your code <?php echo $filename; ?>.

    Here below is a short code snippet with an example:

    <?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>


    Knowledge

    I am not understanding if my variable $filename needs to be in quotes, single quotes, parenthesis, or something else to be interpreted here correctlty?

    Looking at your case in code snipped that you shown, you write the table and its contents in HTML, so if u want use the PHP variable in it for any purpose the easiest way is to echo it by injecting a PHP snippet in proper place. In this case it doesn't matter how you declare $filename, or with quotes or single quotes, because it will be convert as string and you have to store just a text in this variable. For another purposes which quotes should can use you can read in PHP manual.