显示基于外键的数据

时间:2018-10-05 04:42:20

标签: php mysql sql html-table

您之前发送的代码在我的系统上可以正常使用,因此我还在另一个模块中使用了该模块,管理员可以在其中查看日期。但是我遇到了问题。

是这种情况,第一个首先添加其订单的用户,甚至下一个具有相同名称和名称的用户现在都被正确记录并保存。但是,问题在于记录完成后,它会显示记录的一阶的重复值。

  DATE   |   NAME   |   SECTION   |   PAYABLE   |   PRODUCT   |   QUANTITY
10/04/18 |   User1  |  Section 1  |     990     |  Magic Mug  |      3
                                                |   T-shirt   |      3
10/04/18 |   User1  |  Section 1  |     630     |  Magic Mug  |      3
                                                |   Thumbler  |      2
10/04/18 |   User1  |  Section 1  |     990     |  Magic Mug  |      3
                                                |   T-shirt   |      3

这是我使用的PHP代码,仅在其中添加了日期。

$last_date = NULL;
$last_name = NULL;
$last_section = NULL;
$last_payable = NULL;

while ($row = mysql_fetch_array($result)) {
    $date = "";
    $name = "";
    $section = "";
    $payable = "";
    if ($last_name === NULL || $last_date != $row['date'] ||
        $last_name != $row['name'] ||
        $last_section != $row['section'] ||
        $last_payable != $row['payable']) {

        $last_date = $row['date'];
        $last_name = $row['name'];
        $last_section = $row['section'];
        $last_payable = $row['payable'];
        $date = $row['date'];
        $name = $row['name'];
        $section = $row['section'];
        $payable = $row['payable'];
    }
    echo '<tr style="text-align:center;">';
    echo '<td>'.$date.'</td>';
    echo '<td>'.$name.'</td>';
    echo '<td>'.$section.'</td>';
    echo '<td>'.$payable.'</td>';
    echo '<td>'.$row['product'].'</td>';
    echo '<td>'.$row['qty'].'</td>';
    echo '</tr>';
}

2 个答案:

答案 0 :(得分:1)

您的预期输出建议,当这三个值之一发生更改时,我们应该一次显示名称,部门和应付款值。因此,我们可以略微修改对your previous question的回答,以在迭代结果集时保持这三列的状态。

$last_date = NULL;
$last_name = NULL;
$last_section = NULL;
$last_payable = NULL;

while ($row = mysql_fetch_array($result)) {
    $date = "";
    $name = "";
    $section = "";
    $payable = "";
    if ($last_name === NULL ||
        $last_date != $row['date'] ||
        $last_name != $row['name'] ||
        $last_section != $row['section'] ||
        $last_payable != $row['payable']) {

        $last_date = $row['date'];
        $last_name = $row['name'];
        $last_section = $row['section'];
        $last_payable = $row['payable'];
        $date = $row['date'];
        $name = $row['name'];
        $section = $row['section'];
        $payable = $row['payable'];
    }
    echo '<tr style="text-align:center;">';
    echo '<td>'.$date.'</td>';
    echo '<td>'.$name.'</td>';
    echo '<td>'.$section.'</td>';
    echo '<td>'.$payable.'</td>';
    echo '<td>'.$row['product'].'</td>';
    echo '<td>'.$row['qty'].'</td>';
    echo '</tr>';
}

答案 1 :(得分:0)

问题不在您的查询中。我检查了问题是否在您的逻辑中,您将其放入while循环中以打印值。 在某些情况下,多个用户的用户名相同。因此,您可以使用List<Integer> list1 = new ArrayList<>(); List<List<Integer> list2 = new ArrayList<>(); list2.add(list1); // list2 contains reference to where list1 points, not to list1 itself. // so any change on where list1 points, happen for list2 reference too. list1.add(1); // happen for list2 list1 = new ArrayList<>(); // doesn't happen for list2 because I change the address // saved in list1 but list2 contains last address and work with last address list1.add(5); System.out.println(list2.get(0).get(0)); // print 1 ,它将对每个订单都是唯一的。

confirmation

尝试一下,我认为这会对您有所帮助。