Page 1 of 1

Variable CurrentUserID in query

Posted: Tue Oct 01, 2013 4:14 am
by thalassa3000

Hi,

I'am making a selection query for an export on a custom PHP page.

For this I query the view "myproducts". This view contains a column 'userID'.
I only want to select the records where the userID is the same as the userID of the currently login user. I used the advanced security, where I selected the tblusers and the id collum as userid.
So it would look something like:

$tablename = "myproducts";
$sql = "SELECT * from $tablename WHERE UserID = $CurrentUserID"

But CurrentUserID is not a known variable. So my questions are:

  • how do I obtain the current userID variable?
  • how can I integrate this in the sql statement?

Please help me in the right direction!


Re: Variable CurrentUserID in query

Posted: Tue Oct 01, 2013 9:03 am
by mobhar

Try this:
$sql = "SELECT * from $tablename WHERE UserID = ".CurrentUserID()."";

If it doesn't work, then try the following one instead:
$sql = "SELECT * from $tablename WHERE UserID = '".CurrentUserID()."'";


Re: Variable CurrentUserID in query

Posted: Tue Oct 01, 2013 6:03 pm
by thalassa3000

What is did was

  1. added variable in code of the table specific list page / List Page / Page_Datarendering ([viewname] > Code (Server Events, Client Scripts and Custom Templates)
    ===========================================================================
    // Page Data Rendering event
    function Page_DataRendering(&$header) {

    $uID=currentUserID();

  2. added a button with the variable containing current userID in the url
    =============================================================

// Page Data Rendering event
function Page_DataRendering(&$header) {
// Example:
//$header = "your header";

$uID=currentUserID();
$var = '<head>
</head>
<body>
<div>
<a class="btn btn-primary ewButton" href="export_to_excel.php?userID='.$uID.'">Download</a>
</div>
<br />
</body>' ;
// echo $var ;
$header = $var ;
}

  1. Used the variable in the select query
    ==================================
    $sql = "SELECT * from $tablename WHERE approvedByUser = 1 AND userID ='".$uID."'";

I could not use a standard 'custom page' thats includes all the relevant code (by creating 'Blank Page'), because that code seemed to conflict with the custom code. So I created a completely new php page.
I hope this may help others. Any additional suggestions are welcome ofcourse.


Re: Variable CurrentUserID in query

Posted: Tue Oct 01, 2013 6:38 pm
by mobhar

CurrentUserID() is a global function, hence you don't have to assign it into a variable first. Just use it at anywhere you want.