Results 1 to 9 of 9

Thread: unique question

  1. #1
    Join Date
    Dec 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile unique question

    I tried doing this with javascript and cookies, but started thinking that it would work better with php and a database.
    Let me know what you think?

    I use a nice program to create some flash quizzes. The only bad thing is the amount of control I have after a quiz is complete. I can pop up a window - which is when I want it recorded (in the database) that the user completed quiz 1.

    On the users main page and only if quiz 1 is complete, the link to quiz 2 will appear.

    There are 7 quizzes in total that I want links to show for but only if the prior quiz has been completed.

    Any help or direction in this would be grealy appreciated.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    If your "nice program" does not allow this, you may need to find a new way to approach the whole setup. Since you did not give a link to it, I don't know.

    Here is the basic method for using PHP:
    1. When a quiz is completed, store that value in the database.
    2. When generating the links for the quizzes, only generate quizzes that are valid based on the stored value.

    If your system stays this simple, here is an easy way to implement it:
    Each time a quiz is completed, store the NUMBER of that quiz as a value for each user. If you have user accounts*, just add a new column in your database for "quiznumber" or something.
    To generate the links to the quizzes, use a for loop:
    for($x=0;$x<$QUIZNUMBER;$x++) { //display quiz$x here; }


    The only way to reliably track users serverside is to use a database with user accounts (password, etc). If you don't want to set all of that up or think it would be inconvenient for the user, then you will have to use cookies or sessions. In that case, just use a cookie or session variable "quiznumber" and do exactly the same thing as above.
    You could also use the IP address to store the number permanently in the database, but IPs change every once in a while and they are very unreliable in some ways (what if two users are on the same computer, or if one user has two computers).

    So the best way for this is either user accounts or probably session variables. (Use start_session(); at the top of your page; then just use $_SESSION['whatever'] to store and retrieve information for the whole time they are one your site, but only for that visit.)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Dec 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    Daniel I thank you in advance. Teacher conferences tonight, but I will attempt to implement tomorrow and will follow up then.

  4. #4
    Join Date
    Dec 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    ok, so I set up a php login system and got it to work fairly quick. I created user accounts for all of my groups and added quiznumber to the database.

    I have it set to redirect all users to a page where I will list the quizzes. Not sure how to do the following:
    1. display username from database
    2. show a hyperlink of available quizzes only
    3. add more quizzes to the database after they finish the previous.

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    The hard part might be integrating it with the login system. If you wrote it yourself it will be simpler, and because of that, much easier. If you are using a prebuilt package (something you downloaded/installed) it may be a bit harder.

    1. Log in systems usually work based on sessions. The session stores some sort of info: a session key is stored on the user's computer as a cookie-- this then coordinates to stored info on the server that you can see in the array $_SESSION (and store there, too). In that array, when the user is logged in, you will get some sort of indication about their identity: a username like $_SESSION['user'] or an id like $_SESSION['user_id'], or some other similar information.
    To get the username, if that is not already in the session array, then you just need to do a database query: SELECT 'USERNAME' FROM `table` WHERE 'INFO' = $_SESSION['info']

    2. As I showed above, do a loop based on the level they reached: get the level they reached from the database (search by username, user id, etc), then do the loop, repeating the link until you get to that level and stop there. That is exactly what a for loop is supposed to do. You could write a lot of ifs (if(1), if(2), ....) but there's certainly no advantage.

    3. To store the level in the database: I) create a column called "quizlevel" or something like that. II) The default should be 0 or 1 (depending on how you are counting). III) When a quiz is completed, change that value to the number of the quiz-- if the user completes quiz #3, then store "3" as the value of "quizlevel" for that user.


    If the user system gets too complex (and you don't need it--- you might, if your website is complex and you want more features), then you can always revert back to sessions:
    The session will last only as long as the user is on your site (so this is a problem, but depending on the user interaction it might work).
    Then just store all the information like above in $_SESSION. But session then will be specific to the user so you don't need to worry about searching the database and all that. Just, for example, use $_SESSION['quiznumber'] for the quiz they are on and $_SESSION['quiznumber'] = 3; to store it.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Dec 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    so this is the code I have so far. I have a simple login system that works and sets a session that I use to match the same database field. The middle part s me just checking to see if I could pull info from the database. Now I'm stuck with trying to pull the quiz number from the row for the session that is logged in. thanks for any help.



    Code:
    <?php
    include_once("config.php");
    
    // Check user logged in already:
    checkLoggedIn("yes");
    doCSS();
    print("Welcome to the members page <b>".$_SESSION["login"]."</b><br>\n");
    print("<a href=\"logout.php"."\">Logout</a><br><br>");
    
    $data = mysql_query("SELECT * FROM users") 
    or die(mysql_error()); 
    $info = mysql_fetch_array( $data );
    Print "<b>Name:</b> ".$info['login'] . " "; 
    Print "<b>Pass:</b> ".$info['password'] . " <br>"; 
    
    
     if (isset($_SESSION[login])) {  
         $details = mysql_query("SELECT * FROM users WHERE login='".$_SESSION["login"]."'") or die ("Could not select info.<br>".mysql_error());   
    
    while ( $row = mysql_fetch_array($details) ) { 
    echo("Welcome " . $row['login'] . " from " . $row[quiznum] . "!"); 
    }  
    echo $details; 
     } 
    
    ?>

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    $row['quiznum'] should have the quiznumber they were on, right?

    Then just use that to control what quizzes you allow links for.

    You can use a for loop, or you can use even just if statements for each quiz. The information should be in the previous posts.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Dec 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    quiznum does have the quiz number that they are on, but my code is giving me an error.

    Code:
    Welcome to the members page phrozen1
    Your password is: twismed
    Logout
    
    Name: phrozen1 Pass: password 
    
    Notice: Use of undefined constant login - assumed 'login' in /home/phrozeno/public_html/mummy/go/members.php on line 40
    
    Notice: Use of undefined constant id - assumed 'id' in /home/phrozeno/public_html/mummy/go/members.php on line 41
    
    Notice: Use of undefined constant password - assumed 'password' in /home/phrozeno/public_html/mummy/go/members.php on line 42
    
    Notice: Use of undefined constant login - assumed 'login' in /home/phrozeno/public_html/mummy/go/members.php on line 46
    
    Notice: Use of undefined constant password - assumed 'password' in /home/phrozeno/public_html/mummy/go/members.php on line 50
    Welcome phrozen1 from password!Resource id #6
    Notice: Use of undefined constant login - assumed 'login' in /home/phrozeno/public_html/mummy/go/members.php on line 60

    I am not sure where I messed this up. Thanks for all of you help to get me to this point. I had no clue until you guided me in the right direction.

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    The mysql queries don't make much sense to me. The first one just grabs any random records from the database then displays the info from the first one.
    The next query does grab the right info, but I don't see why you are using the while loop like that.
    Instead, you should just use a while loop or if statements to display the right quizzes-- what is the code you are using now to display the links to the quizzes? It is not included above.

    Those aren't errors exactly. They are notices that you are using undefined constants instead of strings. $array[index] instead of $array['index']. PHP will consider an undefined constant as the name of the constant, and give you an error. This is lazy coding, but not actually something incorrect. You should add quotes to have (completely) proper code, but this is not causing anything to run incorrectly.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. The Following User Says Thank You to djr33 For This Useful Post:

    kuau (12-17-2009)

Bookmarks

Posting Permissions

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