0

I need some PHP coding assistance in building an echo to display the results of a database query...(list friends of the logged-in user?) I have the majority of it built, but I don't know how to echo the results data and link it to his/her profile? Any help would be greatly appreciated...maybe a code snippet of how to write it out?

Here is my code:

$query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($query);
if ($numrows > 0){
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
    array_push($my_friends, $row["user2"]);
    array_push($my_friends, $row["user1"]);
}
//remove your id from array
$my_friends = array_diff($my_friends, array($u));
//reset the key values
$my_friends = array_values($my_friends);
mysqli_free_result($query);
} else {
    echo "You have no friends.";
}

// Build My friends From Results
     ****this is where I need help with*****

if (array_key_exists('0', $my_friends)){
    $sql = "SELECT user1, user2 
    FROM friends 
    WHERE (user1='$u' OR user2='$u')
    AND accepted='1'";      
    $result = mysqli_query($db_conx, $sql);
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

$my_friends = "<a href='user.php?u=".$result."'>". $row['avatar']. '&nbsp'. $row['firstname']. '&nbsp' .$row['lastname'] ."</a>"; }
}
?>

Here is my html code:
<?php
echo "<li>$my_friends</li>"; 
  ?>
2
  • How exactly do you want the links to look like on the HTML page? Commented Mar 15, 2017 at 18:40
  • I'm putting them into a unordered listview page that auto-divides alphabetically by last name, so I want it to basically expand the <li> inside the <ul> and then each item will link back to that user's profile which is user.php?u="userid"" Commented Mar 15, 2017 at 19:23

1 Answer 1

1

You are using the $result variable in the html link. Replace it with $row['id'] or the row attribute you use as GET parameter to see a user's detailed page

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.