<?php

include ('includes/header.html');


# Only execute this block of code if the page ID is a number, then store it as a variable.
if (is_numeric($_GET['id'])) {
    $id = $_GET['id'];

    # Execute this block of code if the logged in user is viewing their own profile.
    if ($_SESSION['user'] == $id) {

        # This query is just to retrieve some pieces of information for public viewing.
        $q = "SELECT username, email, date FROM users WHERE user_id='$id'";
        $r = mysqli_query($dbc, $q);
        $userinfo = mysqli_fetch_array($r);

        # Echo a nav bar at the top.
        echo "<div id=\"nav\"><p><a href=\"/myboard/\">Index</a> > <a href=\"user.php?id=$id\">" .
            ucwords($userinfo['username']) . " 's Profile</a></p></div>";

        # Echo a profile with an edit box so the user can change their information.
        echo "	
	<div id=\"title\">
	<h2>" . ucwords($userinfo['username']) . "</h2>
	</div>
	<div id=\"box\">
	<p>Registered Since:" . formatdate(null, $userinfo['date']) . "</p>
	<p>Email Address: {$userinfo['email']}</p>
	<div id=\"editbutton\">
	<p><a href=\"edit.php?id=$id\">Edit</a></p>
	</div>
	</div>
	";

        # Execute this block of code if the user is viewing any profile other than their own.
    } else {

        # This query is just to retrieve some pieces of information for public viewing.
        $q = "SELECT username, email, date FROM users WHERE user_id='$id'";
        $r = mysqli_query($dbc, $q);
        $userinfo = mysqli_fetch_array($r);

        # Echo a nav bar at the top.
        echo "<div id=\"nav\"><p><a href=\"/myboard/\">Index</a> > <a href=\"user.php?id=$id\">" .
            ucwords($userinfo['username']) . " 's Profile</a></p></div>";

        # Echo a profile with no edit box, just for standard viewing.
        echo "	
	<div id=\"title\">
	<h2>" . ucwords($userinfo['username']) . "</h2>
	</div>
	<div id=\"box\">
	<p>Registered Since:" . formatdate(null, $userinfo['date']) . "</p>
	<p>Email Address: {$userinfo['email']}</p>
	</div>
	";

    }

    # Echo an error if the page ID is not a number.
} else {
    echo "<p>User does not exist!</p>";
}

include ('includes/footer.html');

?>