dtjo87679 2016-07-11 02:46
浏览 31

如何循环不同的数据?

Let's Say I have Data... ID... 1, 2, 3, 4, 5, 6
FirstName... Donald, Tabatha, Jeremy, Samuel, Donald, Tabatha
LastName... Faulknor, Kolasa, Jones, Jackson, Faulknor, Kolasa

In this example, I would want to display Donald Faulknor, Tabatha Kolasa, Jeremy Jones, Samuel Jackson

I can either display all data (repeating the duplicate entries) or, if I use

SELECT DISTINCT
then I will only display the first instance, which in this case would be Donald Faulknor. Here's my code...
$sql55 = "SELECT * FROM messages WHERE lowerID = :lowerIDb || higherID = :higherIDb";
    $stmt55 = $pdo->prepare($sql55);
    $stmt55->bindValue(':lowerIDb',$user_id);
    $stmt55->bindValue(':higherIDb',$user_id);
    $stmt55->execute();
    while($row55 = $stmt55->fetch(PDO::FETCH_ASSOC)) {
        $lowerID55 = $row55['lowerID'];
        $higherID55 = $row55['higherID'];
        if($lowerID55 == $user_id) { $user_id55 = $higherID55; } elseif($higherID55 == $user_id) { $user_id55 = $lowerID55; }
        $sql56 = "SELECT DISTINCT user_id FROM users WHERE user_id = :user_id55";
        $stmt56 = $pdo->prepare($sql56);
        $stmt56->bindValue(':user_id55',$user_id55);
        $stmt56->execute();
        while($row56 = $stmt56->fetch(PDO::FETCH_ASSOC)) {
            $id56 = $row56['user_id'];
            $sql58 = "SELECT * FROM users WHERE user_id = :user_id58";
            $stmt58 = $pdo->prepare($sql58);
            $stmt58->bindValue(':user_id58',$id56);
            $stmt58->execute();
            while($row58 = $stmt58->fetch(PDO::FETCH_ASSOC)) {
            $firstname56 = $row58['firstname'];
            $lastname56 = $row58['lastname'];
            $profilePhoto56 = $row58['profilePhoto'];
            $sql57 = "SELECT * from photos WHERE id = :profilePhotoa";
            $stmt57 = $pdo->prepare($sql57);
            $stmt57->bindValue(':profilePhotoa',$profilePhoto56);
            $stmt57->execute();
            while($row57 = $stmt57->fetch(PDO::FETCH_ASSOC)) {
                $profilePhotoPath57 = $row57['path'];
            echo '';
            echo ' '.$firstname56.' '.$lastname56.'';
        }
    }
    }}

To better understand the above code... I have messages to and from users, of course, there will be many to and from the same users. I'm trying to get their name (the recipient) to show once in a side bar (indicating messages from/to that person. Let's say for example, I have 20 messages to and from one person. I can either get the name to repeat twenty times (not using DISTINCT) or I can only get one person's name to show (using DISTINCT). So if there's 8 people to send this user an email, only one name will show.

Much help is greatly appreciated. For questions or more elaboration, please feel free to ask questions. Thank You! :)

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 BP神经网络控制倒立摆
    • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
    • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
    • ¥30 Unity接入微信SDK 无法开启摄像头
    • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
    • ¥20 cad图纸,chx-3六轴码垛机器人
    • ¥15 移动摄像头专网需要解vlan
    • ¥20 access多表提取相同字段数据并合并
    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算