drobson
Posts: 4
|
| Posted: 04/19/2005, 1:13 PM |
|
I can not figure out how to work with custom session variables. (I am relatively new to CC but experienced with PHP.) I have created a custom session variable, but do not know how to display it (that is my best guess).
Background: I am working on a PHP application for nurses. Each time a nurse creates a patient visit record we want the application to automatically stamp the record with the logged-in nurse initials, such as JMB. The table nurses controls login; it contains these fields:
nurseID
first_name
last_name
initials
login_name
login_password
security_group
I want to set the field initials as a session variable. I realize there are other (probably better) ways to implement this but I want to stick with this approach until I grok CodeCharge session variables.
I followed the CC Help document Create Custom Session Variables and entered the following code on the login page, login button, Server, On Click, Custom Code…
//Custom Code @11-ADD652EE
// -------------------------
global $Login;
// Write your own code here.
function Login_DoLogin_OnClick()
{
if ($Login_DoLogin_OnClick == true) {
$db = new clsDBConnection1();
CCSetSession("NurseInit", CCDLookUp("initials","nurses","nurseID=".$db->ToSQL(CCGetUserID(),ccsInteger), $db) );
$db->close();
}
}
// -------------------------
//End Custom Code
Is this successful? I do not know because I have not been able to display the session value in the application. After login, the user is directed to the page main.php On that page, as a test, I created a label which displays the value of the standard session variable UserID. This displays OK on the web page. I tried to use exactly similar construction to create a label to display NurseInit, but nothing shows on the web page. BTW, I also tried modifying the above code by omitting ,ccsInteger because the value is text, not integer: no improvement.
The two labels are set up like this:
Successful Label
-- Data tab --
Name: Label1
Control Source Type: Database Column
Control Source:
Data Type: Integer
-- Events tab --
Server, Before Show, Add Action, Retrieve Value for Control
Control Name: Label1
Source Type: Session
Source Name: UserID
Unsuccessful Label
-- Data tab --
Name: Label2
Control Source Type: Database Column
Control Source:
Data Type: Text
-- Events tab --
Server, Before Show, Add Action, Retrieve Value for Control
Control Name: Label2
Source Type: Session
Source Name: NurseInit
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Any idea what I am doing wrong?
Any suggestions greatly appreciated.
Dave
|
 |
 |
peterr
Posts: 5971
|
| Posted: 04/19/2005, 5:45 PM |
|
Maybe try adding:
echo "Session Value: " . Session("NurseInit")
right after the CCSetSession/CCDLookup. This way you should see the value of that session and verify if it is correct.
Or use echo to display the value of the CCDLookup without creating session.
_________________
Peter R.
YesSoftware Forums Moderator
For product support please visit http://support.yessoftware.com |
 |
 |
drobson
Posts: 4
|
| Posted: 04/23/2005, 10:18 AM |
|
The custom session variable is now working!
Peter, thanks for that suggestion. I used it to diagnose the problem. On the page main, I added (page properties, Events, Server, Before Show) three lines of code to display session variables (2 standard vars, 1 custom var), like this:
//End Page_BeforeShow
//Custom Code @85-22867C81
// -------------------------
global $main;
// Write your own code here.
echo "<br>Session Value UserID: " . CCGetSession("UserID");
echo "<br>Session Value UserLogin: " . CCGetSession("UserLogin");
echo "<br>Session Value NurseInitials: " . CCGetSession("NurseInit");
// -------------------------
//End Custom Code
Initially, it displayed the first two vars OK but not the custom var. This confirmed that my problem was with the Set function, not Get.
I tried several (unsuccessful) code hacks to Set the custom session variable; ended up with this successful approach:
On the page login, select Login button properties, add custom code (Events, Server, On Click, Add Code) as shown below. Note that the added code begins with $db = and ends with $db->close();
Also, I edited the first added line to change from clsDBconnection1() to clsDBhealthtest() because my ODBC database connection is called healthtest.
//End Login
//Custom Code @12-ADD652EE
// -------------------------
global $Login;
// Write your own code here. -----------
// CCSetSession("NurseInitials", "DPR");
$db = new clsDBhealthtest();
CCSetSession("NurseInit", CCDLookUp("initials","nurses","nurseID=".$db->ToSQL(CCGetUserID(),ccsInteger), $db) );
$db->close();
// -------------------------
//End Custom Code
//Close Login_Button_DoLogin_OnClick @3-0EB5DCFE
This works fine, and I can now retrieve the session variable on the page main by setting a Label to Retrieve value for Control and then setting its properties to LabelName, Session, NurseInit.
-=-=-=-=-=-=-=-=
Questions about db connections:
Is it always necessary to edit the clsDBconnection to match the actual server connection, or can the generic string clsDBconnection1 work?
The CC Help file Lookup Single Value from DB shows this construction:
global $DBConnection1;
$Header->UserName->SetValue(CCDLookUp("user_name","users","user_id=". $DBConnection1->ToSQL(CCGetUserID(), ccsInteger), $DBConnection1) );
}
Is there an advantage to declaring the connection as a global? Where can I read up on how code db connection string relates to the CC project-wide connection name?
Thanks!
David
|
 |
 |
Nicole
Posts: 586
|
| Posted: 04/25/2005, 2:31 AM |
|
David,
Login action is also assigned to onClick event of DoLogin button:
//Login @12-C826ABD6
global $Login;
if(!CCLoginUser($Login->login->Value, $Login->password->Value))
{
...
Your custom code should come after login action code, in this case UserID session variable is not empty. In your case it looks like you put your code above login action code, when UserID is empty.
_________________
Regards,
Nicole |
 |
 |
custom session var
|
| Posted: 05/09/2005, 3:36 PM |
|
David and Nicole:
Can you help me with declare session var, I want pass the login to main page from login form.
I did yours steps, but didn't work for me.
My connection called sarep
My table called plantilla
And field called id_empleado.
Thank you very much.
Hector
|
|
|
 |
Nicole
Posts: 586
|
| Posted: 05/10/2005, 7:05 AM |
|
Hector,
You can store user login in session without custom coding. Open Project Settings on Security tab and click on Advanced button. There assign a name to User Login Variable and regenerate the project or common files.
Now you can get user login using CCGetUserLogin() function.
_________________
Regards,
Nicole |
 |
 |
|