This topic is locked

Ownership Issues

10/14/2008 2:28:31 PM
PHPRunner General questions
I
illi author

Hello,
I'm having an issue with "Users can see and edit only their own data" in my database:
classrooms

"ClassroomID" (PK)

"TeacherID" (FK)

"ClassroomName"
students

"StudentID" (PK)

"ClassroomID" (FK)

(and additional columns of student data)
teachers

"TeacherID" (PK)

"SchoolID" (FK)

(and additional columns of teacher data)
I'm trying to control the students table so teachers can only access their own classrooms/students. Short of adding an additional "TeacherID" column to students, how can this be accomplished?
Thanks!

J
Jane 10/15/2008

Hi,
you can add where clause for students table in the List page: Before SQL query event on the Events tab.

Select all ClassroomID values from classrooms table for logged in teacher and construct where clause manually.

I
illi author 10/15/2008

This is what I have come up with, but it's not working...can you help me out? <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=33965&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' />
global $conn;

$str = "select ClassroomID from classrooms where TeacherID = '".$_SESSION["OwnerID"]."'";

$rs = db_query($str,$conn);

$data = db_fetch_array($rs);
$_SESSION["ClassromID"] = $data["ClassroomID"];
$strSQL = "select * from students where ClassroomID = '".$_SESSION["ClassroomID"]."'";

T
thesofa 10/15/2008

check spelling as you have once spelled $Classroom as $Classrom.

I
illi author 10/15/2008

Thanks sofa...apparently it's too early here, it's working now. This event seems to have messed up my data being displayed in the list. I was concatenating First and Last name and now nothing is showing up in my "Name" field. Hmmm.

I
illi author 10/15/2008

I figured it out. Thanks for your help Jane and Sofa.
global $conn;

$str = "select ClassroomID from classrooms where TeacherID = '".$_SESSION["OwnerID"]."'";

$rs = db_query($str,$conn);

$data = db_fetch_array($rs);
$_SESSION["ClassroomID"] = $data["ClassroomID"];
$strWhereClause = whereAdd($strWhereClause,"students.ClassroomID = '".$_SESSION["ClassroomID"]."'");

I
illi author 10/15/2008

And I spoke too soon...I have a teacher with two classrooms (thus two ClassroomID's) and only students from her first ClassroomID are being displayed on the list page. <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=33971&image=1&table=forumreplies' class='bbc_emoticon' alt=':(' />

Sergey Kornilov admin 10/15/2008

If one teacher has multiple classrooms assigned you need to go through the list building the following WHERE clause:

students.ClassroomID = 335 or students.ClassroomID = 413 or ...


In my understanding you don't need to use session variable in this scenario.

I
illi author 10/15/2008

This is what I have right now. I'm not quite sure how to implement your solution as $data[ClassroomID] is only returning one of of the two values (which I verified by echoing it). If I paste the query into the query editor and set the TeacherID it returns the two ClassroomID's. Sorry, this is a bit beyond me.
global $conn;

$str = "select ClassroomID from classrooms where TeacherID = '".$_SESSION["OwnerID"]."'";

$rs = db_query($str,$conn);

$data = db_fetch_array($rs);
$strWhereClause = whereAdd($strWhereClause,"students.ClassroomID = '".$data["ClassroomID"]."'");
echo "'".$data["ClassroomID"]."'";

Sergey Kornilov admin 10/15/2008

You can post your application to Demo Account and contact support team directly at support@xlinesoft.com

I
illi author 10/15/2008

I think I'm getting closer, but it still only returns (students.ClassroomID='1') in the echo. The TeacherID is also associated with ClassroomID's 3 and 4.
global $conn;

$str = "select ClassroomID from classrooms where TeacherID='".$_SESSION["OwnerID"]."'";

$rs = db_query($str,$conn);

$data = db_fetch_array($rs);

$arr = explode(",",$data["ClassroomID"]);//parse value

$where = "";

for ($i=0; $i<count($arr); $i++)

$where.= "students.ClassroomID='".$arr[$i]."' or ";//form where clause

$where = substr($where,0,-3);

$strWhereClause = whereAdd($strWhereClause,"(".$where.")");//add where clause to the SQL query
echo "$strWhereClause";