Display User Name / Login Name in Page Header

Tips submitted by PHPMaker users
Post Reply
SRanade
User
Posts: 95

Display User Name / Login Name in Page Header

Post by SRanade »

Here is the complete code you need to make a fancy user name display in page header. Updated to the latest PHPMaker 2021 version. Hope it saves you much time and effort! :D

1 . In Server Events / Global / All Pages / Page_Rendering event add:

	$curUserId = CurrentUserID();
	if ($curUserId>0) $curUserFullname = ExecuteScalar("SELECT CONCAT(first_name,' ',last_name) FROM users WHERE user_id = $curUserId;");
	else $curUserFullname = "Please login"; // message to show when user is not logged in

	//pass variables to client-side code:
	SetClientVar("curUserId", $curUserId);
	SetClientVar("curUserName", CurrentUserName());
	SetClientVar("curUserFullname", $curUserFullname);
	SetClientVar("curUserLevel", CurrentUserLevel());

In all references to curUserId in your code, make sure to check for null or check for IsLoggedIn() on server-side code.
Remember to change curUserFullname line to match your table lookup setting. I have my users login with their email ids. So I lookup and get their full name from the table.

2 . In Server Events / Client Scripts / Global / Pages with header footer / Client Script event add:

//get variables from server-side code:
var curUserId = ew.vars.curUserId;
var curUserName = ew.vars.curUserName;
var curUserFullname = ew.vars.curUserFullname;
var curUserLevel = ew.vars.curUserLevel;

//insert new HMTL element
jQuery(document).ready(function() {
	if (curUserFullname!="") {
		$("#ew-navbar-right").prepend('<div id="loggedinUser" style="height: 40px; width:100%; line-height: 40px;color: #FFFFFF;font-size: 12px;z-index: 999;background-color: violet;font-weight: bold;margin-top: 0px;margin-right: auto;margin-bottom: 0px;margin-left: auto;padding-right: 10px;padding-left: 10px;float: left;padding-top: 0px;padding-bottom: 0px;position: relative;">Bienvenue! <strong>'+curUserFullname+'!</strong></div>');
	}
});

That's it! You have a beautiful welcome message with user name!

Remember to customise colours to your theme.
You can change the location of the display message by changing "#ew-navbar-right". Currently "#ew-navbar-right" is the user icon and "#ew-navbar" is the menu hamburger.
You can also use "prepend" or "append" or "before" or "after" functions to change position of the message relative to the element. Experiment with each and see what you like best.

Enjoy!


saleh
User
Posts: 463

Post by saleh »

hello
It was working on the previous version
But now it is not working on PHPMaker v2022.12.0 Released
please help


mobhar
User
Posts: 11472

Post by mobhar »

Press [F12] from your browser, and check whether any Javascript error message from Console section.


Post Reply