I am trying to create an admin notification system where on my database table named 'siteconfig' I have a row called setting='adminalerts' in this row will be certain alert columns such as:
nauseralert (non-activated user alert) 0 or 1 mmsitealert (maintenance mode site alert) 0 or 1
and so on... I am trying to code something that will count the number of 0's or 1's in a row and display the value as a number. So if both of the above are set to 1, the value would be (2) and if there are only 1 alerts active it would be (1). If no alerts are active then it would show as (0) etc... Here is the code I have attempted to try but it doesn't seem to work? Was hoping for a little help here! Thanks!
<?
$sitealerts = mysql_query("SELECT * FROM siteconfig WHERE setting='adminalerts'");
$adminalerts = mysql_num_rows($sitealerts);
if ($nonactivated < 0){
echo 'Admin Alerts';
} else {
echo 'No Alerts';
}
echo $adminalerts;
?>
I know this is probably totally wrong, but pretty new to this stuff and have tried to find about it online but not too sure I really understand how to do this?
You have to access the columns to calculate the right value:
$sitealerts = mysql_query("SELECT * FROM siteconfig WHERE setting='adminalerts'");
//if the query was OK
if($sitealerts) {
//loop through all result rows
while(FALSE !== ($row = mysql_fetch_assoc($sitealerts))) {
//do the calculation
$count = 0;
$count += intval($row['nauseralert']);
$count += intval($row['mmsitealert']);
if($count > 0) {
print $count;
}
print '<br />';
}
mysql_free_result($sitealerts);
}
mysql is dprecated in php 5.5 http://php.net/manual/en/changelog.mysql.php it is better to use mysqli.
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
// checking connection
if (mysqli_connect_errno()) {
exit();
}
if ($result = $mysqli->query("SELECT (nauseralert+mmsitealert) as alerts_count FROM siteconfig WHERE setting='adminalerts'")) {
while ($row = $result->fetch_row()) {
if($row['alerts_count'])
echo $row['alerts_count'];
else echo ' ';
}
$result->close();
}
$mysqli->close();