I'm trying to make a notification system, so far I made the notification text (poor one) and the active notifications (again, poor one)
Notification system is based on 4 notifications, each on MySql database in INT mode. They can be set to 0 (No notification) or 1 (yes notification and it will show an already made text by me.)
ActiveNotification system is based on sum of notifications. Not1+Not2+Not3+No4. Exemple: If 3 active not it shows 3.
What I want to do and will do:
I will create an admin section where it will be a "notification1 / Notification 2 / Notification 3 / Notification 4" form and there I will put 1 or 0, depends which notification.
I will create (somehow) $Session user when click "a href" his notification to be set at 0, all of them.
But I want to do an easy and BETTER way if there exists on. But I don't know how or imagine, because never worked with things like that.
Is there (certainly is) a BETTER WAY how can I modify my script and achieve what I want? (I know that StackOverFlow doesn't talk about ideas, that's why I'm posting my code.)
Legend
Notificare 1 = First Notification row
Notificare 2 = Second Notification row
Notificare 4 = Last and fourth notification Row
Notificari = How many Notifications are set to 1. (it shows active notifications)
TN1 = Date of when first notification arrived
TN4 = Date of when fourth and last notification arrived
PHP
<?php
session_start(); //starting session
include("config.php"); //including our config.php
error_reporting(E_ALL);
if(isset($_SESSION['username'])) //if session is set, so if user is logged in...
{
$username = $_SESSION['username']; //setting variable username as one from session
$query = mysql_query("SELECT * FROM users WHERE username = '$username'");
while($row = mysql_fetch_assoc($query)) //looping thousgt table to get informations
{
$Notificare1 = $row['Notificare1'];
$Notificare2 = $row['Notificare2'];
$Notificare3 = $row['Notificare3'];
$Notificare4 = $row['Notificare4'];
$TN1 = $row['TN1'];
$TN2 = $row['TN2'];
$TN3 = $row['TN3'];
$TN4 = $row['TN4'];
if($Notificare1 == 1) {
$Notificare1 = "<li><a href='settings.php'>
<div class='user_img'><img src='images/logo.png' alt=''></div>
<div class='notification_desc'>
<h6>Title</h6>
<p>Trebuie să-ți schimbi setările contului!<br> Fă-o chiar acum apăsănd pe mine.</p>
<p><span> Primit la: $TN1 </span></p>
</div>";}
if($Notificare2 == 0) {
$Notificare2 = "<li></li>";}
if($Notificare2 == 1) {
$Notificare2 = "<li><a href='#subdomenii'>
<div class='user_img'><img src='images/logo.png' alt=''></div>
<div class='notification_desc'>
<h6>EminDesign.ro</h6>
<p>Cererea ta de subdomeniu a fost acti<br>vat! Click pe mine.</p>
<p><span> Primit la: $TN2 </span></p> </div>";}
($Notificare3 == 0) {
$Notificare3 = "<li></li>";}
if($Notificare3 == 1) {
$Notificare3 = "<li><a href='#facturi'>
<div class='user_img'><img src='images/logo.png' alt=''></div>
<div class='notification_desc'>
<h6>EminDesign.ro</h6>
<p>Cererea ta de design a fost acti<br>vat! Click pe mine..</p>
<p><span> Primit la: $TN3 </span></p>
</div>";}
if($Notificare4 == 0) {
$Notificare4 = "<li>/li>";}
if($Notificare4 == 1) {
$Notificare4 = "<li><a href='#restante'>
<div class='user_img'><img src='images/logo.png' alt=''></div>
<div class='notification_desc'>
<h6>Title</h6>
<p>Ai o factură neplătită ! Click pe mine.</p>
<p><span> Primit la: $TN4 </span></p>
</div>";}
$Notificari = $row['Notificare1'] + $row['Notificare2'] + $row['Notificare3']+ $row['Notificare4'];
}?>
and HTML
<li class="dropdown head-dpdn">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="true"><i class="fa fa-bell-o" aria-hidden="true"></i> <span class="badge blue"><?php echo $Notificari ?></span></a>
<ul class="dropdown-menu">
<li>
<div class="notification_header">
<h3>Your Notifications</h3>
</div>
</li>
<?php echo $Notificare1 ?>
<?php echo $Notificare2 ?>
<?php echo $Notificare3 ?>
<?php echo $Notificare4 ?>
<li>
<div class="notification_bottom">
<a href="#">Delete notifications</a>
</div>
</li>
</ul>
</li>
////
if($Text1 == 100) {
$TotalA == 1;}
if($Text2 == 100) {
$TotalB == 1;}
if($Text3 == 100) {
$TotalC == 1;}
if($Text4 == 100) {
$TotalD == 1;}
$Progres = $row['TotalA'] + $row['TotalB'] + $row['TotalC'] + $row['TotalD'];
TotalA,B,C,D are not defined.
session_startMUST be written before any output, most developers will recommend that it is your first line of code. Please stop usingmysql_functions; upgrade to mysqli. Please improve your code tabbing for improved readability and debugging. Rather than1and0values, you may enjoy the benefits of bitmask values: 1, 2, 4, 8. Then you can store all notification data as a single number in a single column. Please read the bottom of this answer: stackoverflow.com/a/49256146/2943403