User Info at top of page on every page

Hey guys,

When a user is logged in to my website (PHP / MySQL), I run the following code on every page he or she visits via PHP:

<?php
if(isset($_COOKIE['myid']))
{
$username = $_COOKIE['myid'];
echo "<div id='loggedin'>";
//Find the picture


$query = mysql_query("SELECT SUM(value) FROM ((badge LEFT JOIN userbadge ON badge.badgeid = userbadge.badgeid) LEFT JOIN account ON userbadge.accountid = account.accountid) WHERE account.username = '$username'");
while($queryier = mysql_fetch_array($query))
{
$p = $queryier['SUM(value)'];
}
$check = mysql_query("SELECT * FROM account LEFT JOIN userbadge ON account.accountid = userbadge.accountid WHERE username = '$username' GROUP BY username");
while($response = mysql_fetch_array($check))
{

    $i = $response['picture'];
    if($i <= 35)
    $picture = 'portrait_1.jpg';
    if($i >= 36 && $i <= 71)
    $picture = 'portrait_2.jpg';
    if($i >= 72 && $i <= 107)
    $picture = 'portrait_3.jpg';
    if($i >= 108 && $i <= 143)
    $picture = 'portrait_4.jpg';
    
    switch($i % 6)
    {
        case 0:
            $x = 0;
            break;
        case 1:
            $x = -90;
            break;
        case 2:
            $x = -180;
            break;
        case 3:
            $x = -270;
            break;
        case 4:
            $x = -360;
            break;
        case 5:
            $x = -450;
            break;
    }
    switch(($i / 6) % 6)
    {
        case 0:
            $y = 0;
            break;
        case 1:
            $y = -90;
            break;
        case 2:
            $y = -180;
            break;
        case 3:
            $y = -270;
            break;
        case 4:
            $y = -360;
            break;
        case 5:
            $y = -450;
            break;
    }

        echo "<a href='/badge/pictures.php'><div style='background:url(/img/$picture);background-position:$x $y; float:left;margin-top:8px;margin-left:9px;height:90px;width:90px;'></div></a>";
        echo "<p style='float:right;'>$username <br />";
          echo "<img style='margin-top:3px;' src='/img/rank_icon.png'>" . " " . " ";
        echo "<span style='color:black'>" . $p . "</span>";
        if($username == "svcghost")
        echo "<br />Site Owner<br /><br /><a href='/logout.php'>Log Out</a></p>";
        else
        echo "<br /><br /><br /><a href='/logout.php'>Log Out</a></p>";
}
echo "</div>";
}
?>

It displays the user’s name, picture, and score of badges.

Do you guys see any problems with this? Is there any other way perhaps more efficient use of the web server’s resources, to do this? I’m kind of new to the idea of PHP I guess.

I actually do a php include of a file that includes this code, on every page of the website.

I hate doing the same queries on every page of a site so I would probably store the result in a session so I can just print it out on every page rather than hitting the database over and over again.