Jump to content

displaying multiple times


esoteric

Recommended Posts

Hi guys, i got the following code i wrote which displays a small pop up on screen if the user has mail, and display a little icon. The problem is it displays a new icon for each new mail message. How can i make it so it only display 1 icon no matter how many unread mail the user has. Or even better if not too complicated display a number next too the image?

 

<?
require'xxxxxxxxxxxxxxxxx.php';
mysql_select_db("membership") or die( "Unable to select database");	
$sql=("SELECT * FROM user_alerts WHERE user_id = '".($_SESSION['user_id'])."'") or die (mysql_error());
if ($query=@mysql_query($sql)) {
if (mysql_num_rows($query) > 0) {
while ($req=mysql_fetch_array($query)) {

if ($req['messageRead'] == 'NO') 
{ 
?>
<div>
<img src="xxxxxxxxxxxxxx.png" width="32" height="32" alt="unread mail" /> <a href="http://www.xxxxxxxxxxxx/account=account">You have Mail!</a>
<div id="boxes">
<div style="top: 199.5px; left: 551.5px; display: none;" id="dialog" class="window">
<h2>You have new mail!</h2>
<br />
<p>Visit your account page to read it.</p>
<br>
<a href="#" class="close">Close Window</a>
</div>
<!-- Mask to cover the whole screen -->
<div style="width: 1478px; height: 602px; display: none; opacity: 0.8;" id="mask"></div>
</div>
</div>
<?
}
}
}
} 

 

You can ignore most of the div stuff but that's pretty much it. It should check my database for any entries with the users session id in the column called 'user_id'. and only show the image if it then finds an entry with 'NO' in the 'messageRead' column. It currently shows the images for each entry it finds.

 

Thanks for any help

Link to comment
https://forums.phpfreaks.com/topic/249350-displaying-multiple-times/
Share on other sites

If this is only to check if the user has NEW mail, I would run a COUNT() query, specifying the user and messageRead = 'NO'.  If the count is more than 0, return the message (and the count holds the amount).

 

PS> get rid of the while statement.

Don't use short tags.

 

This line is completely wrong

$sql=("SELECT * FROM user_alerts WHERE user_id = '".($_SESSION['user_id'])."'") or die (mysql_error());

That is simply assigning a string to the variable $sql and if that fails would attempt to show the mysql error. You need to be checking if the execution of the query fails!

 

Also, do not use 'NO' for your database value. Because then you have to do comparisons on the value to see if it is true or not. For boolean values use a 1 (true) or 0 (false). Then you don't need to do string comparisons. I wrote the code below using the 'NO' value - but it is really poor implementation.

 

Lastly there is no reason to do a query of all the records for the user when you are only interested in the unread ones. SO, include additional criteria on the WHERE clause so you don't have to process unnecessary records.

 

 

Not tested

require'xxxxxxxxxxxxxxxxx.php';
mysql_select_db("membership") or die( "Unable to select database");	

$query = "SELECT COUNT(user_id)
          FROM user_alerts
          WHERE user_id = '{$_SESSION['user_id']}'
            AND messageRead = 'NO'";
$result = mysql_query($query);
if(!$result)
{
    echo "Error running query: $query<br>Error: " . mysql_error();
}
else
{
    $mail_count = mysql_result($result, 0);
    if($mail_count>0)
    {
        echo "<div>\n";
        echo "<img src='xxxxxxxxxxxxxx.png' width='32' height='32' alt='unread mail' />\n";
        echo "<a href='http://www.xxxxxxxxxxxx/account=account'>You have {$mail_count} Messages!</a>\n";
        echo "<div id='boxes'>\n";
        echo "<div style='top: 199.5px; left: 551.5px; display: none;' id='dialog' class='window'>\n";
        echo "<h2>You have new mail!</h2>\n";
        echo "<br />\n";
        echo "<p>Visit your account page to read it.</p>\n";
        echo "<br>\n";
        echo "<a href='#' class='close'>Close Window</a>\n";
        echo "</div>\n";
        echo "<!-- Mask to cover the whole screen -->\n";
        echo "<div style='width: 1478px; height: 602px; display: none; opacity: 0.8;' id='mask'></div>\n";
        echo "</div>\n";
        echo "</div>\n";
    }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.