<?php

require_once 'header.php';
if (!$loggedin) {
    die;
}
$text = handleText($conn, $user);
handleImage($user);
showAvatar($user);
echo <<<_END
<form method='post' action='editProfile.php' enctype='multipart/form-data'>
<h3>Enter or edit your details and/or upload an image</h3>
<textarea name='text' cols='50' rows='3'>{$text}</textarea><br>
Image: <input type='file' name='image' size='14'>
<input type='submit' value='Save Profile'>
_END;
//********Functions*************
function handleText($conn, $user)
{
    $query = "SELECT * FROM profiles WHERE user='******'";
    $result = queryMysql($conn, $query);
    if (isset($_POST['text'])) {
        $text = $_POST['text'];
        $text = $conn->real_escape_string($text);
        if ($result->num_rows) {
            $query = "UPDATE profiles SET text='{$text}' WHERE user='******'";
        } else {
            $query = "INSERT INTO profiles VALUES('{$user}', '{$text}')";
        }
        queryMysql($conn, $query);
    } else {
<?php

require_once 'header.php';
echo "<h2>Members List</h2>";
$query = "SELECT user FROM members";
$result = queryMysql($conn, $query);
if (($s = $result->num_rows) > 1) {
    for ($i = 0; $i < $s; ++$i) {
        $row = $result->fetch_array(MYSQLI_ASSOC);
        if (($view = $row['user']) != $user) {
            showAvatar($view);
            echo "<p><a href='profile.php?view={$view}'>" . $view . "</a></p>";
            echo "<br>";
        }
    }
}
function checkAvatar($url, $array, $position)
{
    $img_profile = $url . "/public/img/avatar/" . $_SESSION['user_name'] . "/" . $array[0]['u_pic'];
    $img_profile_default = $url . "/public/img/default_avatar.jpg";
    if ($position == "Header") {
        if (!empty($array[0]['u_pic'])) {
            showAvatar($img_profile, 30, $position);
        } else {
            showAvatar($img_profile_default, 30, $position);
        }
    } elseif ($position == "Body") {
        if (!empty($array[0]['u_pic'])) {
            showAvatar($img_profile, 100, $position, 1);
        } else {
            showAvatar($img_profile_default, 100, $position, 0);
        }
    }
}