You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将PHP页面中治疗价格列移至表格右侧?附关联代码

Fixing Price Column Placement in Your Dental Clinic Table

Got it, let's get your table sorted so the price column sits right next to the corresponding treatment (instead of all prices showing up below the treatments). Here's what's off with your current code and how to fix it:

What's Causing the Issue

Right now, you're looping through the treatment list first and generating all those rows, then looping through the price list and generating another set of rows. That's why prices are all on their own at the bottom instead of being in the right column next to each treatment. Also, your current SQL query has a critical SQL injection vulnerability—we'll fix that too since it's a big security risk.

Step-by-Step Fix

1. Secure Your Database Query First

Replace your existing database code with this using prepared statements to avoid injection:

<?php
ini_set('error_reporting', E_ALL);
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "halcondentalclinic";

$conn = new mysqli($servername, $username, $password, $dbname);

// Use prepared statement to prevent SQL injection
$sql = "SELECT treatment, totalprice FROM payment WHERE patient_id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $id); // Use "i" instead of "s" if $id is an integer
$stmt->execute();
$result = $stmt->get_result();

if ($row = $result->fetch_assoc()) {
    $treatments = $row['treatment'];
    $treatmentss = $row['totalprice'];
    
    $treatments_chunks = explode(",", $treatments);
    $treatments_chunkss = explode(",", $treatmentss);
} else {
    // Handle case where no payment record exists for this patient
    $treatments_chunks = [];
    $treatments_chunkss = [];
}
?>

2. Rewrite the Table to Pair Treatments and Prices

Instead of two separate loops, loop through both arrays at the same time so each treatment gets its matching price in the right column. Here's the corrected table code:

<table class="table table-bordered" width="50%" cellspacing="0">
    <thead>
        <th>Treatment</th> <!-- Renamed from "Teeth" to match your data; adjust if needed -->
        <th>Price</th>
    </thead>
    <tbody>
        <?php
        // Loop through both arrays together (assuming they have matching lengths)
        foreach($treatments_chunks as $index => $treatment){
            // Grab the matching price for this treatment (fallback to N/A if missing)
            $price = isset($treatments_chunkss[$index]) ? $treatments_chunkss[$index] : 'N/A';
            echo '<tr>';
            echo '<td>' . htmlspecialchars($treatment) . '</td>'; // Prevent XSS attacks
            echo '<td>' . htmlspecialchars($price) . '</td>';
            echo '</tr>';
        }
        ?>
    </tbody>
</table>

Key Changes Explained

  • Combined Loop: Using the index from the treatments array lets us pull the corresponding price, putting both values in the same row.
  • Security Fixes: Added htmlspecialchars() to block XSS attacks when outputting user data, and switched to prepared statements to eliminate SQL injection risks.
  • Error Handling: Added a check for missing patient records so your table won't break if there's no matching payment data.

Now your table will display each treatment in the left column and its matching price in the right column, exactly how you wanted!

内容的提问来源于stack exchange,提问作者yansa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 15:22:33