Main
Index
Search
Posts
Who's
Online
Log
In

Home: Products: CMS Builder: Plugins & Add-ons:
Membership: List CURRENT USER items only

 

 


Toledoh
Enthusiast

Mar 4, 2010, 6:27 PM

Post #1 of 25 (19836 views)
Shortcut
Membership: List CURRENT USER items only Can't Post

Hi Guys,

This should be straight forward, but I can't seem to get it.

I've got a "multi-record" section called "Client Area". Within this, I've got a field "account" with is a pick list from the user accounts table.

On the list page, I want to show only records that relate to the current user.

So at the top of the page, I need to have something like;


Code
list($l_client_areaRecords, $l_client_areaMetaData) = getRecords(array( 
'tableName' => 'l_client_area',
'where' =>'account = $CURRENT_USER['num']

));


but that doesn't work.... can you help?
Cheers,

Tim Forrest
Toledoh Enterprises
www.toledoh.com.au


gkornbluth
Veteran

Mar 7, 2010, 5:54 PM

Post #2 of 25 (19685 views)
Shortcut
Re: [Toledoh] Membership: List CURRENT USER items only [In reply to] Can't Post

Hi Tim,

I haven't really played with this a lot but will be in a few days.

For now, here are some notes (from Chris) that I found in the Plugin section of my CMSB Cookbook http://thecmsbcookbook.com

To determine if the current user is logged in, you can test with:


Code
<?php if (current_user): ?>  
only logged in user will see this...
<? php endif ?>


You could set up a separate (text or check box) field in the user accounts editor and assign it a value for that particular class of users. Then test with:


Code
<?php if ($CURRENT_USER['your_field'] == "your_value"): ?>Current user will be able to see this<?php endif ?>


Only user(s) who have that value entered/selected in their user record will be able to view the protected information on the web page.

To determine if the current user is the "author" of a record, you can test with:


Code
<?php if ($record['createdByUserNum'] == $CURRENT_USER['num']): ?>Only the author will be able to see this<?php endif ?>


You may want to also allow admins to see your protected information:


Code
<?php if ($record['createdByUserNum'] == $CURRENT_USER['num'] || $CURRENT_USER['isAdmin']): ?>Author and administrators will be able to see this<?php endif ?>


Hope that helps to gets you where you need to go.

Best,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!
http://www.thecmsbcookbook.com

(This post was edited by gkornbluth on Mar 7, 2010, 5:57 PM)


Dave
Staff / Moderator


Mar 8, 2010, 3:33 PM

Post #3 of 25 (19554 views)
Shortcut
Re: [Toledoh] Membership: List CURRENT USER items only [In reply to] Can't Post

Hi Tim,

For the where, make sure your 'account' field stores the users account number and not their name. I usually name the fields accountNum just so it's super clear what's stored in there.

Then try:

Code
list($l_client_areaRecords, $l_client_areaMetaData) = getRecords(array(  
'tableName' => 'l_client_area',
'where' => mysql_escapef(" account = ? ", $CURRENT_USER['num'] ),
));


You don't really need to escape the current user num as it's basically guaranteed to be a num. It's just a good practice though. That line is the same as the following, but uses our function:

'where' => " account = '" .mysql_real_escape_string($CURRENT_USER['num']). "'".

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com
 


Toledoh
Enthusiast

Mar 8, 2010, 4:22 PM

Post #4 of 25 (19548 views)
Shortcut
Re: [Dave] Membership: List CURRENT USER items only [In reply to] Can't Post

Not working yet Dave... maybe I'm doing something wrong?

I've got a demo login: username/password client1/client1

I've got 2 pages:
http://www.forrest.id.au/test/clientList.php
http://www.forrest.id.au/test/clientList_A.php

The only difference between the 2 is;
'where' => mysql_escapef(" client_list = ? ", $CURRENT_USER['num'] ),

and "A" has;
'where' => mysql_escapef(" client_list != ? ", $CURRENT_USER['num'] ),


You can see that the second record has the client_name as "4" and the current user as "4", so shouldn't the first version of the page show that result?
Cheers,

Tim Forrest
Toledoh Enterprises
www.toledoh.com.au


Chris
Staff / Moderator


Mar 9, 2010, 2:07 PM

Post #5 of 25 (19403 views)
Shortcut
Re: [Toledoh] Membership: List CURRENT USER items only [In reply to] Can't Post

Hi Tim,

Can you please post the complete PHP source code for clientList.php? Also, how exactly is your "client_list" field configured? Is it a Multi-Value list field, perchance?
Chris


Toledoh
Enthusiast

Mar 9, 2010, 2:16 PM

Post #6 of 25 (19396 views)
Shortcut
Re: [chris] Membership: List CURRENT USER items only [In reply to] Can't Post

Hi Chris.


client_list is a pick list from the user table. So basically, as soon as I have someone sign-up, they are added to the list for a potential "client area".

Attached is the file.
Cheers,

Tim Forrest
Toledoh Enterprises
www.toledoh.com.au

(This post was edited by Toledoh on Mar 9, 2010, 2:16 PM)
Attachments: clientList.php (3.24 KB)


Chris
Staff / Moderator


Mar 9, 2010, 3:27 PM

Post #7 of 25 (19383 views)
Shortcut
Re: [Toledoh] Membership: List CURRENT USER items only [In reply to] Can't Post

Hi Tim,

Please try changing this:


Code
  'where' => mysql_escapef(" client_list = ? ", $CURRENT_USER['num'] ),


to this:


Code
  'where' => mysql_escapef(" client_list LIKE ? ", "%\t".$CURRENT_USER['num']."\t%" ),


Does that solve the problem? Please let me know.
Chris


(This post was edited by chris on Mar 9, 2010, 4:08 PM)


Toledoh
Enthusiast

Mar 9, 2010, 3:33 PM

Post #8 of 25 (19379 views)
Shortcut
Re: [chris] Membership: List CURRENT USER items only [In reply to] Can't Post

Sorry Chris,

I'm now getting an error:

Quote
MySQL Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' %' ) ORDER BY client_list' at line 3

Cheers,

Tim Forrest
Toledoh Enterprises
www.toledoh.com.au


Chris
Staff / Moderator


Mar 9, 2010, 4:09 PM

Post #9 of 25 (19375 views)
Shortcut
Re: [Toledoh] Membership: List CURRENT USER items only [In reply to] Can't Post

Hi Tim,

Sorry about that. Please try this instead:


Code
  'where' => mysql_escapef(" client_list LIKE ? ", "%\t".$CURRENT_USER['num']."\t%" ),


(I've edited my above post to be correct also.)
Chris


Toledoh
Enthusiast

Mar 9, 2010, 8:28 PM

Post #10 of 25 (19339 views)
Shortcut
Re: [chris] Membership: List CURRENT USER items only [In reply to] Can't Post

Excellent! Thanks heaps.
Cheers,

Tim Forrest
Toledoh Enterprises
www.toledoh.com.au


s2smedia
User

Mar 24, 2010, 12:04 PM

Post #11 of 25 (17051 views)
Shortcut
Re: [Toledoh] Membership: List CURRENT USER items only [In reply to] Can't Post

Im in need of the same functionality...

heres my scenerio...

When a project is created, I have a drop down of Employees to assign it to.. the drop down pulls in a field "id" which is a field I added to the Accounts section.

so I just need to adjust this code accordingly:

(I added the code you suggested to the other person in this thread, I also have another where clause i need to keep in there "project_status = 'Active'")

<?php
/* STEP 1: LOAD RECORDS - Copy this PHP code block to the TOP of your page BEFORE anything else. */
require_once "admin/lib/viewer_functions.php";

list($projectsRecords, $projectsMetaData) = getRecords(array(
'tableName' => 'projects',
'where' => " project_status = 'Active' " "mysql_escapef(" project_manager LIKE ? ", "%\t".$CURRENT_USER['id']."\t%" )",
));

?>


Jason
Staff / Moderator


Mar 24, 2010, 3:42 PM

Post #12 of 25 (17035 views)
Shortcut
Re: [s2smedia] Membership: List CURRENT USER items only [In reply to] Can't Post

Hi,

I think all you'll need to do put those two statements together with an AND statement:

'where' => " project_status = 'Active' AND mysql_escapef(" project_manager LIKE ? ", "%\t".$CURRENT_USER['id']."\t%" )",

Give that a try.
---------------------------------------------------
Jason Sauchuk - Programmer 
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/ 


s2smedia
User

Mar 24, 2010, 4:30 PM

Post #13 of 25 (17029 views)
Shortcut
Re: [Jason] Membership: List CURRENT USER items only [In reply to] Can't Post

hmm im getting this error:

Parse error: syntax error, unexpected T_STRING, expecting ')' in /hermes/bosweb/web255/b2559/ipw.s2smedia/frontdesk/index.php on line 18


Jason
Staff / Moderator


Mar 25, 2010, 8:47 AM

Post #14 of 25 (16890 views)
Shortcut
Re: [s2smedia] Membership: List CURRENT USER items only [In reply to] Can't Post

Sorry about that. This should work better:

Code
list($projectsRecords, $projectsMetaData) = getRecords(array( 
'tableName' => 'projects',
'where' => 'project_status=\'Active\' AND'. mysql_escapef(" project_manager LIKE ? ", "%\t".$CURRENT_USER['id']."\t%" ) ,
));


Let me know if that works.
---------------------------------------------------
Jason Sauchuk - Programmer 
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/ 


s2smedia
User

Mar 25, 2010, 10:10 AM

Post #15 of 25 (16860 views)
Shortcut
Re: [Jason] Membership: List CURRENT USER items only [In reply to] Can't Post

ok... error is gone.

But now the areas where the content should display, is blank.

not sure if maybe I need to flip the code or something..

so basically when I create a "Project"

I select from a drop down (project_manager) which pulls in values "id" from "accounts"

it seems like the code should work... not sure what maybe happening...


Jason
Staff / Moderator


Mar 25, 2010, 11:09 AM

Post #16 of 25 (16834 views)
Shortcut
Re: [s2smedia] Membership: List CURRENT USER items only [In reply to] Can't Post

Hi,

I need some clarification on the problem.
So, you want to populate a drop down with a list of employees, and this list of employees are stored in the "accounts" table?

If you could post your .php file, I could take a look at it for you.

Thanks,
---------------------------------------------------
Jason Sauchuk - Programmer 
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/ 


s2smedia
User

Mar 25, 2010, 11:29 AM

Post #17 of 25 (16823 views)
Shortcut
Re: [Jason] Membership: List CURRENT USER items only [In reply to] Can't Post

no.. sorry for confusion.

I attached a PDF that will explain... its fairly simple what I need.
Attachments: IT_pdf.pdf (557 KB)


Jason
Staff / Moderator


Mar 25, 2010, 3:29 PM

Post #18 of 25 (16684 views)
Shortcut
Re: [s2smedia] Membership: List CURRENT USER items only [In reply to] Can't Post

Hi,

Okay, we're going to try a couple of things. First, we're going to change our "where" statement to the following:

Code
'where' => 'project_manager= "'. $CURRENT_USER['id'] .'"' ,


See if that gives you the records you're looking for. If not add this line right underneath the "where" statement:

Code
'debugSql' => true,


This will display the SQL query. Send me that information along with the php file you are using and I'll take a look.

Let's give that a try. Let me know what happens.
---------------------------------------------------
Jason Sauchuk - Programmer 
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/ 


s2smedia
User

Mar 25, 2010, 4:47 PM

Post #19 of 25 (16607 views)
Shortcut
Re: [Jason] Membership: List CURRENT USER items only [In reply to] Can't Post

PERFECTO!

now I just need i the where=> project_status "active"


Dave
Staff / Moderator


Mar 26, 2010, 1:29 AM

Post #20 of 25 (16342 views)
Shortcut
Re: [s2smedia] Membership: List CURRENT USER items only [In reply to] Can't Post

Hi s2smedia,

You can add that back with this:
'where' => 'project_status="Active" AND project_manager= "'. $CURRENT_USER['id'] .'"' ,

If that works, great! If not, and it makes all your results disappear it probably means "Active" isn't the actual value you're wanting to match (or that you don't have any active records).

If that's the case you can remove it again and examine the records being returned by temporarily adding this line:
<?php showme($projectsRecords); ?>

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com
 


s2smedia
User

Mar 26, 2010, 6:16 AM

Post #21 of 25 (16192 views)
Shortcut
Re: [Dave] Membership: List CURRENT USER items only [In reply to] Can't Post

Works like a charm!!

thanks a mill Jason&Dave


Toledoh
Enthusiast

Aug 2, 2011, 4:52 PM

Post #22 of 25 (4002 views)
Shortcut
Re: [Toledoh] Membership: List CURRENT USER items only [In reply to] Can't Post

Hi Guys.

I have a client area where;

1. Users table identifies which projects users have access to:
Table: project_list
Value: num
Lable: title

2. Project List Table, which has a number of projects (project_list)

3. An "Articles" table that has a number of fields including a multi-select called "project";
Table: project_list
Value: num
Lable: title

I want the page to display only articles where the user is identified as part of that project;


Code
  // load records 
list($articlesRecords, $articlesMetaData) = getRecords(array(
'tableName' => 'articles',
'where' => mysql_escapef(" project LIKE ? ", "%\t".$CURRENT_USER['project']."\t%" ),
'debugSql' => true,
));


But this gives no results... the debug shows:

Code
SELECT SQL_CALC_FOUND_ROWS `articles`.* FROM `cms_articles` as `articles` WHERE ( project LIKE '% 1 2 %' ) ORDER BY createdDate DESC


and when I remove the where statement totally, I get all projects, and the the lables are "Project A, Project B" etc I've tried changing the articles "project" field to
Table: project_list
Value: num
Lable: num

but this doesn't help...

any advice?
Cheers,

Tim Forrest
Toledoh Enterprises
www.toledoh.com.au


Jason
Staff / Moderator


Aug 3, 2011, 9:58 AM

Post #23 of 25 (3995 views)
Shortcut
Re: [Toledoh] Membership: List CURRENT USER items only [In reply to] Can't Post

Hi Tim,

What we need to do here is to take the tab (\t) separated list stored in $CURRENT_USER['project'] and turn it into an array of numbers. We can then use this array to create a custom WHERE clause looking for each number individually.

Try this:


Code
  $where = ""; 
$projectNumArray = explode("\t", trim($CURRENT_USER['project'], "\t"));

foreach ($projectNumArray as $projectNum) {
$where .= " project LIKE '%\t".intval($projectNum)."\t%' OR";
}

// remove last OR from $where
$where = rtrim($where, "OR");

// if where is empty, return no records
if (!$where) { $where = "num = '0'"; }

// load records
list($articlesRecords, $articlesMetaData) = getRecords(array(
'tableName' => 'articles',
'where' => $where,
));


Hope this helps
---------------------------------------------------
Jason Sauchuk - Programmer 
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/ 


Toledoh
Enthusiast

Aug 3, 2011, 7:30 PM

Post #24 of 25 (3926 views)
Shortcut
Re: [Jason] Membership: List CURRENT USER items only [In reply to] Can't Post

Hi Jason.

Thanks Great... works a treat.

In another area, I need to do a similar thing, however I need to dispay the label, rather than the value...

$projectNumArray = explode("\t", trim($CURRENT_USER['project'], "\t"));

needs to be something like;

$projectNumArray = explode("\t", trim($CURRENT_USER['project:label'], "\t"));

Can you help?
Cheers,

Tim Forrest
Toledoh Enterprises
www.toledoh.com.au


Jason
Staff / Moderator


Aug 4, 2011, 10:13 AM

Post #25 of 25 (3863 views)
Shortcut
Re: [Toledoh] Membership: List CURRENT USER items only [In reply to] Can't Post

Hi Tim,

Unfortunately, $CURRENT_USER doesn't use the :labels pseudo field on list fields. However, if you want to get an array of the selected list labels, you can try this:


Code
 $projectLables  = getListLabels('accounts', 'project', $CURRENT_USER['project']);


Taking this one step further, if you wanted an array where the index was the selected values, and the and the value was the label, you can combine the two arrays into 1 like this:


Code
  $projectNums      = explode("\t", trim($CURRENT_USER['project'], "\t")); 
$projectLables = getListLabels('accounts', 'project', $CURRENT_USER['project']);
$selectedProjects = array_combine($projectNums, $projectLabels);


Hope this helps
---------------------------------------------------
Jason Sauchuk - Programmer 
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/