This topic is locked
[SOLVED]

 Issue with code snippet

11/15/2020 4:29:27 PM
PHPRunner General questions
D
david22585 author

So I'm having a weird issue that I just can't quite figure out. I have a code snippet, that renders properly on some pages, but completely different on other pages. What even makes it more weird is that on the same page that it renders properly for one user, it renders completely different for another logged in user.



My mind is boggled. I can't quite figure out why it would show properly for one user, but not for another on the exact same page. When the code is viewed on desktop, is when this happens. If viewed on mobile, it works as it should. Here is the code snippet:



echo "<div class='col-xs-6'>";

$rs = DB::Query("SELECT count(id) AS cnt FROM notifications WHERE (user_id=".$_SESSION["user_id"]." AND status = '0')");

$data = $rs->fetchAssoc();

if($data["cnt"]>0){

echo "<div class='dropdown'>";

echo "<a class='dropdown-toggle' data-toggle='dropdown' href='#'>";

echo "<span class='fa fa-bell fa-2x' style='color:#01a9ac;' id='span_alert_notify'></span>";

echo "<span style='color:white; position:relative;font-size:9px;top:-18px;left:-12px;background-color:red;padding:1px 5px'>".$data["cnt"]."</span>";

echo "</a>";

echo "<ul class='dropdown-menu' style='margin-top:10px'>";

$rs2 = DB::Query("SELECT * FROM notifications LEFT OUTER JOIN website_users ON notifications.posted_by = website_users.id LEFT OUTER JOIN notifications_data ON notifications.`type` = notifications_data.id WHERE (notifications.user_id = ".$_SESSION["user_id"]." AND notifications.status = '0') ORDER BY posted DESC LIMIT 10");

while($notifications = $rs2->fetchAssoc())

echo "<li ><a role='menuitem' href='".$notifications["url"]."".$notifications["id_key"]."&viewed=1' style='font-size:12px'><b>".$notifications["displayname"]."</b>".$notifications["text"]."</a></li>";

echo "<center><b><a role='menuitem' href='Notifications_list.php'>View All Notifications</a></b></center>";

echo "</ul></div>";

} else {

echo "<div class='dropdown'>";

echo "<a class='dropdown-toggle' data-toggle='dropdown' href='#'>";

echo "<span class='fa fa-bell fa-2x' style='color:#01a9ac;' id='span_alert_notify'></span>";

echo "</a>";

echo "<ul class='dropdown-menu' style='margin-top:10px'>";

echo "<center><b><a role='menuitem' href='Notifications_list.php'>View All Notifications</a></b></center>";

echo "</ul></div>";

}

echo "</div>";
echo "<div class='col-xs-6'>";

$rs3 = DB::Query("SELECT count(id) AS unread FROM messages_notifications WHERE (recipient = ".$_SESSION["user_id"]." AND viewed = '0')");

$messages = $rs3->fetchAssoc();

if($messages["unread"]>0){

echo "<div class='dropdown'>";

echo "<a class='dropdown-toggle' data-toggle='dropdown' href='#'>";

echo "<span class='fa fa-envelope-o fa-2x' style='color:#01a9ac;' id='span_alert_notify'></span>";

echo "<span style='color:white; position:relative;font-size:9px;top:-18px;left:-12px;background-color:red;padding:1px 5px'>".$messages["unread"]."</span>";

echo "</a>";

echo "<ul class='dropdown-menu' style='margin-top:10px'>";

$rs4 = DB::Query("SELECT * FROM messages_notifications LEFT OUTER JOIN website_users ON messages_notifications.sender = website_users.id LEFT OUTER JOIN notifications_data ON messages_notifications.`type` = notifications_data.`type` WHERE (messages_notifications.recipient = ".$_SESSION["user_id"]." AND messages_notifications.viewed = '0') ORDER BY received DESC LIMIT 10");

while($unread = $rs4->fetchAssoc())

echo "<li><a role='menuitem' href='".$unread["url"]."".$unread["message_id"]."&viewed=1' style='font-size:12px'><b>".$unread["displayname"]."</b>".$unread["text"]."</a></li>";

echo "<center><b><a role='menuitem' href='Inbox_list.php'>Go To Inbox</a></b></center>";

echo "</ul></div>";

} else {

echo "<div class='dropdown'>";

echo "<a class='dropdown-toggle' data-toggle='dropdown' href='#'>";

echo "<span class='fa fa-envelope-o fa-2x' style='color:#01a9ac;' id='span_alert_notify'></span>";

echo "</a>";

echo "<ul class='dropdown-menu' style='margin-top:10px'>";

echo "<center><b>No Unread Messages</b></center>";

echo "<center><b><a role='menuitem' href='Inbox_list.php'>Go To Inbox</a></b></center>";

echo "</ul></div>";

}

echo "</div>";
D
DealerModulesDevClub member 11/15/2020

Hi David,

I noticed your line that starts with echo “<li> contains quotes in the middle.
In the past I have seen errors in my code anytime you have an echo statement and there are double quotes in the middle. I was able to get the code to work correctly by changing the double quotes to single quotes between the echo “ “;
Might be worth a try.
Paul

D
david22585 author 11/15/2020



Hi David,

I noticed your line that starts with echo "<li> contains quotes in the middle.
In the past I have seen errors in my code anytime you have an echo statement and there are double quotes in the middle. I was able to get the code to work correctly by changing the double quotes to single quotes between the echo " ";
Might be worth a try.
Paul


Without the double quotes, the variables in the string won't work at all, so those are needed. If I separate the 2 pieces of code in half, and make 2 code snippets, it works fine. But the minute I throw 2 together, it misbehaves. I could do a work around to make it work, but I would rather eliminate having multiple code snippets if I can.

D
david22585 author 11/18/2020




Just want to say that after a week of working on this, I figured out a solution. I added another class within the font awesome span for the badge, and moved the CSS styling to the editor page with the general CSS. Here is what I have for the code on the snippet now:



echo "<div class='col-xs-6' id='notifications'>";

$rs = DB::Query("SELECT count(id) AS cnt FROM notifications WHERE (user_id=".$_SESSION["user_id"]." AND status = '0')");

$data = $rs->fetchAssoc();

if($data["cnt"]>0){

echo "<div class='dropdown'>";

echo "<a class='dropdown-toggle' data-toggle='dropdown' href='#'>";

**[b] echo "<span class='far fa-bell fa-2x' style='color:#01a9ac;'>";

echo "<span class='badge badge-danger' style='background-color:red'>".$data["cnt"]."</span>";

echo "</span>";

**[/b] echo "</a>";

echo "<ul class='dropdown-menu' style='margin-top:10px'>";

$rs2 = DB::Query("SELECT * FROM notifications LEFT OUTER JOIN website_users ON notifications.posted_by = website_users.id LEFT OUTER JOIN notifications_data ON notifications.`type` = notifications_data.id WHERE (notifications.user_id = ".$_SESSION["user_id"]." AND notifications.status = '0') ORDER BY posted DESC LIMIT 10");

while($notifications = $rs2->fetchAssoc())

echo "<li ><a role='menuitem' href='".$notifications["url"]."".$notifications["id_key"]."&viewed=1' style='font-size:12px'><b>".$notifications["displayname"]."</b>".$notifications["text"]."</a></li>";

echo "<center><b><a role='menuitem' href='Notifications_list.php'>View All Notifications</a></b></center>";

echo "</ul>";

echo "</div>";

} else {

echo "<div class='dropdown'>";

echo "<a class='dropdown-toggle' data-toggle='dropdown' href='#'>";

echo "<span class='fa fa-bell fa-2x' style='color:#01a9ac;'></span>";

echo "</a>";

echo "<ul class='dropdown-menu' style='margin-top:10px'>";

echo "<center><b><a role='menuitem' href='Notifications_list.php'>View All Notifications</a></b></center>";

echo "</ul>";

echo "</div>";

}

echo "</div>";
echo "<div class='col-xs-6' id='notifications'>";

$rs3 = DB::Query("SELECT count(id) AS unread FROM messages_notifications WHERE (recipient = ".$_SESSION["user_id"]." AND viewed = '0')");

$messages = $rs3->fetchAssoc();

if($messages["unread"]>0){

echo "<div class='dropdown'>";

echo "<a class='dropdown-toggle' data-toggle='dropdown' href='#'>";

echo "<span class='fal fa-envelope-open-text fa-2x' style='color:#01a9ac;'>";

echo "<span class='badge badge-danger' style='background-color:red'>".$data["cnt"]."</span>";

echo "</span>";

echo "</a>";

echo "<ul class='dropdown-menu' style='margin-top:10px'>";

$rs4 = DB::Query("SELECT * FROM messages_notifications LEFT OUTER JOIN website_users ON messages_notifications.sender = website_users.id LEFT OUTER JOIN notifications_data ON messages_notifications.`type` = notifications_data.`type` WHERE (messages_notifications.recipient = ".$_SESSION["user_id"]." AND messages_notifications.viewed = '0') ORDER BY received DESC LIMIT 10");

while($unread = $rs4->fetchAssoc())

echo "<li><a role='menuitem' href='".$unread["url"]."".$unread["message_id"]."&viewed=1' style='font-size:12px'><b>".$unread["displayname"]."</b>".$unread["text"]."</a></li>";

echo "<center><b><a role='menuitem' href='Inbox_list.php'>Go To Inbox</a></b></center>";

echo "</ul>";

echo "</div>";

} else {

echo "<div class='dropdown'>";

echo "<a class='dropdown-toggle' data-toggle='dropdown' href='#'>";

echo "<span class='fal fa-envelope fa-2x' style='color:#01a9ac;'></span>";

echo "</a>";

echo "<ul class='dropdown-menu' style='margin-top:10px'>";

echo "<center><b>No Unread Messages</b></center>";

echo "<center><b><a role='menuitem' href='Inbox_list.php'>Go To Inbox</a></b></center>";

echo "</ul>";

echo "</div>";

}

echo "</div>";


CSS as follows:

.badge {

position: absolute;

padding-left: .25em;

padding-right: .25em;

font-size: 15px;

font-family: Arial, Helvetica, sans-serif;

top: 15px;

margin-left: -10px;

margin-top: 0px;

color: white;

border-radius: 4px;

}