Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2006
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts

    This will be complicated

    Alright, I'm making a web test for the English department at school. Currently it works with each student being able to login by using their first and last name, teacher's last name and a unique pin (to help with duplicate names, less chance of overlap)

    This way it can run without having to create user names and passwords. I also choose this style because I don't have access to any database. Each student's records are saved in a text file with the format: teacher_lastname.firstname_pin.txt

    I'm rambeling. I have no problem creating the test, I've already done it.

    What I'm doing now is trying to create an Administrator's login so the tests can be administered once i'm gone.

    I'm having problems trying to figure out a way to store all students in a teachers class. It would be easy if I knew the teachers last name but I can't hard code it as it may change.


    This is what I'm doing now (outlined)


    I'm able to read the directory with all the students files in it. Strip the data from them, and be able to get the teacher's name, last name, and first name (and pin if I wanted but no reason for that)

    I'm storing all of it in a 3D array.

    PHP Code:
    $students["$teacher"]["$lname"]["$fname"

    It works great when I know the teachers name and students full name. But the problem is I dont know how to list all of the students with on teacher.

    Is there a way I can do this? I'm able to use other forms of storage (meaning other arrays, i guess) But I can't really change the use of text-files. I'm currently seeing if I can get a mySQL DB to use, because then this would all be easier.

    Thanks for all the help, any input would be much appreciated, sorry for the long winded story.

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    I think your array structure is a bit broken.

    It doesn't make much sense both because a variable called $students is keyed by the name of teachers and because it has a false hierarchy (namely the last name being nested inside the first name when they should both be properties of the same parent).

    Since you use data for the keys instead of, er, keys, it's not possible to get directly to an element without either knowing its value already or iterating the whole thing. It's going to get ugly and confusing very quickly.

    You can relate data in different arrays by their keys in the same way you can relate table in a rdms, and I'd recommend doing so with two arrays that reference eachother's keys like so:

    PHP Code:
    $teachers = array(
        
    'Smith' => array(
            
    'students' => array( 'fbar''fmcname' // keys of students in this teacher's class
        
    ),
        
    'Crabapple' => array(
            
    'students' => array( 'bjsimpson''mprince')
        )
    );

    $students = array(
        
    'bjsimpson' => array(
            
    'fname'   => 'Bart',
            
    'lname'   => 'Simpson',
            
    'teacher' => 'Crabapple' // You can include a reference back if you expect to have to find out the teacher given the student's key 
        
    ),
        
    'mprince' => array(
            
    'fname'   => 'Martin',
            
    'lname'   => 'Prince',
            
    'teacher' => 'Crabapple'
        
    )
        
    // ... more ...
    ); 
    You can use whatever scheme you already have in place to key the student array (if you've got unique pins that's ideal), I just went with initials + full last name for simplicity.

    The loop you asked for with this structure would look like this:

    PHP Code:
    $teacher $_POST['teacher']; // or whatever
    foreach ($teachers[$teacher]['students'] as $student_id)
    {
        echo 
    $students[$student_id]['fname'] . ' ' $students[$student_id]['lname'] . '<br />';

    for the record, I think you could iterate your current array with something like

    PHP Code:
    foreach ($students[$teacher] as $fname=>$v)
    {
        
    $keys array_keys($v);
        echo 
    $students[$teacher][$fname] . ' ' $keys[0] . '<br />';

    But that's awful code.

  • #3
    New Coder
    Join Date
    Aug 2006
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for explaining in depth how to go about it. I got confused as all hell but managed to scrounge up some code from yours that was able to work.

    here it is:
    PHP Code:
    $teachers["$teacher"]['students'][]=$lname.'.'.$fname.'_'.$pin
    That way I can make a nice loop to spit out each students "code" name, which will be riped before being displayed and all I'll have to know is the teacher's name.

    Now, I just though of this as I was typing, and I know you did it in your example, I'm just not sure how to pull it off.

    I want to be able to keep the above, but add this for each student:

    PHP Code:
    'bjsimpson' => array(
            
    'fname'   => 'Bart',
            
    'lname'   => 'Simpson',
            
    'teacher' => 'Crabapple' 
    So would the correct way be to do this (I'm writing this live, so I have no idea if it works):

    PHP Code:
    $teachers["$teacher"]['students'][]=array('fname' => $fname,
    'lname' => $lname
    'pin' => $pin); 
    The above would be in a loop that examines each file and gets the first name, last name, teacher's name, and pin.

    so I could list all students like this (saying I know the teachers name)

    PHP Code:
    $teacher='jones';
    $a 0;

    while(
    $a count($teachers[$teacher]['students'])){

    echo 
    'student: '.$teachers[$teacher]['students'][$a]['lname'].', '.$teachers[$teacher]['students'][$a]['fname']."\n<br />";

    $a++;


    I haven't tested it, but that should work, right? Or am I still going about it the wrong way?

    Again, thanks for the help, it's really pushed me in the right direction. Its always nice to get a fresh mind on a project that you've over analyzed.

  • #4
    New Coder
    Join Date
    Aug 2006
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Yes, that seems to do EXACTLY what I need although, it seems slower. Regardless, it still works and works exactly as I needed it.

    I was able to make it a tid bit faster by eliminating 'students' as I don't believe I'll need to store anything more complex than what I have.

    Heres what it is now:
    PHP Code:
    $teachers["$teacher"][]=array('fname' => $fname,'lname' => $lname,'pin' => $pin,'teacher' => $teacher); 
    Thanks again!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •