简体   繁体   English

从用户表中获取朋友的个人资料图片

[英]Get friends profile picture(s) from user table

I have 2 table's:我有2张桌子:

Users (id, username, email, avatar, etc...);用户(ID、用户名、电子邮件、头像等...);

Friends (id, user1, user2, status);好友(id、user1、user2、status);

Now I want to build on my profile page an list of my friends with there avatar(s).现在我想在我的个人资料页面上建立一个我有头像的朋友列表。 I'm trying for like 4 hours by myself but i don't get it... :(我自己尝试了大约 4 个小时,但我不明白...... :(

BTW: this is an error i got!顺便说一句:这是我得到的错误!

Notice: Array to string conversion in /home/reduaqi158/domains/reduankurtaj.eu/public_html/snapfriends/vrienden.php on line 26注意:第 26 行 /home/reduaqi158/domains/reduankurtaj.eu/public_html/snapfriends/vrienden.php 中的数组到字符串转换

This is what i have right now:这就是我现在所拥有的:

<?php

    error_reporting(E_ALL);
    session_start();
    $username = $_SESSION['username'];
    $status = 2;

    include "includes/conn.php";

    $vrienden=mysqli_query($server,"SELECT * FROM vrienden WHERE status='$status' && vriend1='$username' || vriend2='$username' ");

    $vriend_list = array();
        while($row = mysqli_fetch_array($vrienden))
        {
            if ($row['vriend1'] == $username) {
                $vriend_list[] = $row['vriend2'];
            }
            else {
                $vriend_list[] = $row['vriend1'];
            }
        }

        echo json_encode($vriend_list);


    $foto=mysqli_query($server,"SELECT prof_pic FROM users WHERE username='$vriend_list['vriend1''vriend2']' ");
        while($row2 = mysqli_fetch_array($foto)) {

            echo "<img class='img-rounded' src=assets/profiel/".$row2['prof_pic']." alt='Card image cap'>";
        }

?>

json_encode output: json_encode 输出:

["ja","amando"] ["ja","amando"]

Someone who can help me pls :)有人可以帮助我请:)

Your initial approach is very confusing.您最初的方法非常令人困惑。

Almost everything in your code can be substituted by single SQL query.代码中的几乎所有内容都可以由单个SQL查询替换。

You can use JOIN to get all your friends with their avatars in one go:您可以使用JOIN一次性让您所有的朋友都拥有他们的头像:

SELECT u.username as username, u.avatar as avatar,.... <== all columns which you need
    FROM `friends_table` f  <== your friends table
    JOIN `users_table` u    <== your users table
        ON (f.user1 = u.id) <== notice that i join on user1 column
    WHERE u.username = '$username' && f.status = '$status'
UNION
SELECT u.username as username, u.avatar as avatar,.... <== same columns
    FROM `friends_table` f  <== your friends table
    JOIN `users_table` u    <== your users table
        ON (f.user2 = u.id) <== notice that i join on user2 column
    WHERE u.username = '$username' && f.status = '$status'

By this query you select all users who are in a friendship with your $username.通过此查询,您可以选择与您的 $username 为好友的所有用户。 You need union because you don't know in which field ( user1 or user2 ) your $username is located.您需要union因为您不知道您的$username位于哪个字段( user1user2 )。

NOTE: I strongly suggest using prepared statements instead of just putting '$var' inside SQL query to prevent SQL Injection.注意:我强烈建议使用准备好的语句,而不是仅仅将 '$var' 放在SQL查询中以防止 SQL 注入。

After executing this query you can parse results and display avatars in such a way:执行此查询后,您可以通过以下方式解析结果并显示头像:

while($row = mysqli_fetch_array($vrienden, MYSQLI_ASSOC))
{
    echo "<img class='img-rounded' src=assets/profiel/".$row['avatar']." alt='Card image cap'>";
}

I hope you got the idea.我希望你有这个想法。

in your while statement you have to declare a value for the array.在您的 while 语句中,您必须为数组声明一个值。 like array[0] = value.像数组[0] = 值。 so that you know that array position 0 has a certain value.以便您知道数组位置 0 具有特定值。 Like what I did here below.就像我在下面所做的一样。 Don't know if it's in PHP like this but certain in .net you have to declare the location of a value in an array.不知道它是否像这样在 PHP 中,但在 .net 中肯定你必须声明一个值在数组中的位置。

while($row = mysqli_fetch_array($vrienden))
    {
        if ($row['vriend1'] == $username) {
            $vriend_list[0] = $row['vriend2'];
        }
        else {
            $vriend_list[1] = $row['vriend1'];
        }
    }

and the following和以下

$foto=mysqli_query($server,"SELECT prof_pic FROM users WHERE username='$vriend_list['vriend1''vriend2']' ");

shouldn't it be $vriend_list['vriend1'] .不应该是 $vriend_list['vriend1'] 。 $vriend_list['vriend2']' $vriend_list['vriend2']'

you have to use a connect character (the . in PHP)您必须使用连接字符(PHP 中的 .)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM