如何格式化我的 HTML table 以停止重复数据库中的 ID 或客户端名称?

How to Format my HTML table to STOP repeated ID or Client Name from Database?

我对从数据库中提取数据后格式化 HTML table 有疑问。 同时,table加载到Table后,又重复了Client_Name。我的数据库中有 tables 的关系,从 parent table(客户端)到 child table(卡片,和网站)。

我的数据库架构:

+---------------+----------------+--------------+
| **Clients**   | **Sites**      | **Cards**    |
+---------------+----------------+--------------+
| Client_ID(pk) | Sites_ID(pk)   | Card_#       |
+---------------+----------------+--------------+
| Name          | Location       | repair       |
+---------------+----------------+--------------+
|Address        | Client_ID(fk)  | sites_ID(fk) |
+---------------+----------------+--------------+
|               | name           |client_ID(fk) |
+---------------+----------------+--------------+

这是mySQL声明:

SELECT 
    cl.client_name, 
    jb.job_number,
    jb.repair,
    st.site_name
FROM
    cards jb
        INNER JOIN
    clients cl ON (cl.client_id = jb.client_id)
        LEFT JOIN
    sites st on (st.site_id = jb.site_id)
WHERE
    anD cl.client_id = jb.client_id

我的 HTML 在 table 中显示数据的代码是

<table width='100%' cellpadding= '1' border='0'>

                        <thead>
                            <tr>

                            <td style="font-size:13px; text-align:Left;"><b>Date</b></td>
                            <td style="font-size:13px; text-align:Left;"><b>Job #</b></td>
                            <td style="font-size:13px; text-align:left;"><b>Site name</b></td>
                            <td style="font-size:13px; text-align:left;"><b>Description</b></td>
                            </tr>
                            </thead>
                                <?php while($userData = mysql_fetch_assoc($tresult)){
                                if($i%2==0)
                                    $classname = 'evenRow';
                                else if($i%2==1) 
                                   ?>                           

    <tr>
    <td>
    <?php echo mysql_real_escape_string ($userData['client_name)']); ?>
    </td>
    <td>
     <?php echo mysql_real_escape_string($userData['job_number']);?>
    </td>
    <td>
     <?php echo mysql_real_escape_string($userData['site_name']);?>
    </td>
    <td>
     <?php echo mysql_real_escape_string($userData['repair']);?>
    </td>
                     </tr>                        
                                        <?php $i++;
                                }
                                    ?>         
                                        </tr>

                           </table>

这是它的显示方式?

| **Client_Name*| **Job#** | **Site_name**|**repair** |
+---------------+--------- +--------------+
|client 1       | 899500   | Marcell      | rebooted 
+---------------+----------+--------------+
|Client 1       | 6623120  | Lovan        | cool-down
+---------------+----------+--------------+
|client 2       | 442120   | Brackes      | setup
+---------------+----------+--------------+
|client 2       | 546698   | client_ID(fk)| replaced
+---------------+----------------+--------+

我不想看到重复的Client_Name。我需要的是:列出的客户端 1 是 child 示例:

    | **Client_Name*| **Job#** | **Site_name**|**repair** |
    +---------------+--------- +--------------+
    |client 1       | 899500   | Marcell      | rebooted 
    +---------------+----------+--------------+
    |               | 6623120  | Lovan        | cool-down
    +---------------+----------+--------------+
    |client 2       | 442120   | Brackes      | setup
    +---------------+----------+--------------+
    |               | 546698   | client_ID(fk)| replaced
    +---------------+----------------+--------+

显示在 HTML table 上。是否可以显示table这种格式?我必须做什么?

非常欢迎任何人帮助!!!

这是我的完整代码

<?php 
session_start();
include ('db.php');


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Company Monthly Reports</title>
</head>
<body id="main_body" >
    <div id="form_container">


                            <table width='100%' cellpadding= '1' border='0'>

                        <thead>
                            <tr>
                            <td style="font-size:13px; text-align:Left;"><b>Client Name</b></td>
                            <td style="font-size:13px; text-align:Left;"><b>Date</b></td>
                            <td style="font-size:13px; text-align:Left;"><b>Job #</b></td>
                            <td style="font-size:13px; text-align:left;"><b>Site name</b></td>
                            <td style="font-size:13px; text-align:left;"><b>Description</b></td>
                            </tr>

                            <?php
                            $sqlSelect = "SELECT 
                                                    cl.client_name,
                                                    DATE(jb.date),
                                                    jb.job_number,
                                                    jb.repair,
                                                    st.site_name
                                            FROM
                                                        job_cards jb
                                            INNER JOIN
                                                        clients cl ON (cl.client_id = jb.client_id)
                                            LEFT JOIN
                                                        sites st on (st.site_id = jb.site_id)
                                            WHERE
                                                        jb.completed =  1 
                                            AND cl.client_id = jb.client_id
                                            AND jb.date >= DATE_ADD(DATE(NOW()), INTERVAL - 30 DAY)
                                            ORDER BY cl.client_name ASC";
                                //echo $sqlSelect;
                                $tresult = mysql_query($sqlSelect);

                                $last = '';                 
                                while($userData = mysql_fetch_assoc($tresult)){
                                    if($i%2==0)
                                        $classname = 'evenRow';
                                    else if($i%2==1)


                                ?>
                            </thead>
                            <tbody>
                                        <tr class='<?php if(isset($classname)) echo $classname;?>'>




                                    <td width=250>                                  
    <?php 
    if ($last == strtolower($userData['client_name)'])) {
    echo "&nbsp;";
}
else {
    $last == strtolower($userData['client_name)']);
    echo mysql_real_escape_string ($userData['client_name)']);
}

    //echo mysql_real_escape_string ($userData['client_name']); ?>
                                    </td>
                                    <td width=100>
    <?php echo mysql_real_escape_string ($userData['DATE(jb.date)']); ?>
    </td>
    <td width=50>
     <?php echo mysql_real_escape_string($userData['job_number']);?>
    </td>
    <td>
     <?php echo mysql_real_escape_string($userData['site_name']);?>
    </td>
    <td>
     <?php echo mysql_real_escape_string($userData['repair']);?>
    </td>

                                        <?php $i++;
                                }
                                    ?>         
                                        </tr>
                                    </tbody>    
                           </table>
    </div>

</body>
</html>

您需要在此处修改您的 php 代码

<?php echo mysql_real_escape_string ($userData['client_name']); ?>

改成这样

<?php
if ($last == strtolower($userData['client_name'])) {
    echo "&nbsp;";
}
else {
    $last = strtolower($userData['client_name']);
    echo mysql_real_escape_string ($userData['client_name']);
}
?>

并且您需要在

循环之前进行更改
</thead>
                                <?php while($userData = mysql_fetch_assoc($tresult)){

改为

</thead>
    <?php 
    $last = '';
    while($userData = mysql_fetch_assoc($tresult)){

应该做的工作