1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Suggestion for PHP-mysql website-based quiz?

Discussion in 'PHP' started by rybrns, Mar 1, 2020.

  1. #1
    In an old site (from 6 years ago) I had several quizzes which were in PHP with scores and names saved to a small database. The app I used for the quizzes was freely available on line. The PHP version I used is now way obsolete and my current hosting provider no longer supports that version. It's been years since I did anything in PHP and I'm so rusty even looking at PHP 4 that I can't figure out how to fix/update the deprecated and unsupported code.
    SEMrush
    I'm looking for quiz software that will allow mult choice, scoring after the test is taken, display of the user's answers with the correct choices shown (and reasons if needed) and storage to a database so users can see their scores compared to other test takers.

    Any suggestions?
     
    rybrns, Mar 1, 2020 IP
    SEMrush
  2. malky66

    malky66 Acclaimed Member

    Messages:
    3,824
    Likes Received:
    2,182
    Best Answers:
    85
    Trophy Points:
    515
    #2
    malky66, Mar 1, 2020 IP
    JEET likes this.
  3. JEET

    JEET Notable Member

    Messages:
    3,356
    Likes Received:
    371
    Best Answers:
    16
    Trophy Points:
    235
    #3
    JEET, Mar 1, 2020 IP
  4. rybrns

    rybrns Active Member

    Messages:
    15
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    88
    #4
    Those all require a fee. My application is a hobby, not supported by an enterprise.

    =====================

    Most of those are very obsolete or dead links. I wish I remembered enough PHP to update the version which used to work.
     
    rybrns, Mar 2, 2020 IP
  5. malky66

    malky66 Acclaimed Member

    Messages:
    3,824
    Likes Received:
    2,182
    Best Answers:
    85
    Trophy Points:
    515
    #5
    lol...they're not exactly expensive, but whatever.
     
    malky66, Mar 2, 2020 IP
  6. sarahk

    sarahk iTamer Staff

    Messages:
    26,216
    Likes Received:
    3,856
    Best Answers:
    108
    Trophy Points:
    665
    #6
    Get your old one and upgrade it. It'll be a few commands, change the database calls so that they're pdo. Will be an interesting learning project.
     
    sarahk, Mar 2, 2020 IP
    JEET likes this.
  7. JEET

    JEET Notable Member

    Messages:
    3,356
    Likes Received:
    371
    Best Answers:
    16
    Trophy Points:
    235
    #7
    What are your requirements, list one by one, as clearly as you can.
     
    JEET, Mar 2, 2020 IP
  8. rybrns

    rybrns Active Member

    Messages:
    15
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    88
    #8
    1. Run locally on my hosted web server
    2. Current php/mysql versions (most of the ones I reviewed use obsolete php)
    3. Ideally allow me to format page how I want (like use html <form> tag)
    4. When test completes, give score and link to answer key page
    5. Allow quiz taker to visit score page showing all takers' scores

    I've been using Quiz-o-matic '76 By Matt Hughes (http://www.ebto.org.tr/test/) which has all these features. But I think it's in php 4 and my host is using 7.2 now so the code won't run any longer.
     
    rybrns, Mar 5, 2020 IP
    JEET likes this.
  9. sarahk

    sarahk iTamer Staff

    Messages:
    26,216
    Likes Received:
    3,856
    Best Answers:
    108
    Trophy Points:
    665
    #9
    What about these options:
    • manage quiz in your existing admin section
    • have more than one quiz at a time
    • have a start date & time, end date & time for each quiz
    • show visitors the results after voting
    • show visitors the results without voting
    • allow visitors to change their vote
    • limit votes to logged-in visitors
    • limit votes to one per IP address
    • limit votes to one per email address
    • show old quiz results
     
    sarahk, Mar 5, 2020 IP
    JEET likes this.
  10. rybrns

    rybrns Active Member

    Messages:
    15
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    88
    #10
    The old php code did all of that except these:
    • allow visitors to change their vote
    • limit votes to logged-in visitors
    • limit votes to one per IP address
    • limit votes to one per email address
    which I don't need. I'm trying to update the v. 4 php myself but since I haven't used it in over 15 years, it's not easy.
     
    rybrns, Mar 5, 2020 IP
    JEET likes this.
  11. sarahk

    sarahk iTamer Staff

    Messages:
    26,216
    Likes Received:
    3,856
    Best Answers:
    108
    Trophy Points:
    665
    #11
    Let us know which bits are tripping you up
     
    sarahk, Mar 5, 2020 IP
    JEET likes this.
  12. JEET

    JEET Notable Member

    Messages:
    3,356
    Likes Received:
    371
    Best Answers:
    16
    Trophy Points:
    235
    #12
    Let me see if I can find something modern with the requirements you mentioned.
     
    JEET, Mar 5, 2020 IP
  13. rybrns

    rybrns Active Member

    Messages:
    15
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    88
    #13
    Here's one. Can't seem to get this to work to make a table in a db.

    From a function.inc file:
    
    ...
    function Create_Table($testname, $answerarray) {
        $sql = "CREATE TABLE $testname (
            id SMALLINT(5) NOT NULL AUTO_INCREMENT, 
            name varchar(30),
            score int,
            date DATETIME DEFAULT NULL,
            PRIMARY KEY (id),
            UNIQUE id (id)
            )";
        $result = mysqli_query($sql) or die("Cannot create table: ".mysqli_error());
        $numberofquestions = count($answerarray);
        for($i=1 ;$i<=$numberofquestions ; $i++) {
            $newfieldname="q$i";
            $sql = "alter table $testname add column $newfieldname tinyint(1)";
            $result = mysqli_query($sql) or die("Cannot update table: ".mysqli_error());
        }
    }
    ...
    
    PHP:
    Used to store quiz name and details in the db with this code:

    <?php
    
    ...
    include ("functions.inc");
    
    $db = Open_Database ($server, $user, $password, $database);
    
    if (!Table_Exists($_POST['testname'])) {
        Create_Table(($_POST['testname']), $answerarray);
    }
    
    $testname = $_POST['testname'];
    
    Create_Response_Array ($responses, $questions, $answerarray);
    
    Create_CorrectAnswer_Array ($responses, $answerarray, $correctanswers);
    
    ...
    ?>
    
    PHP:
    No errors thrown but the table isn't created. All the db access specs are checked and correct. Can't see why this code won't work. I updated all the quiz mechanics to php 7.2 and the quiz itself now works ok, scoring and all. But can't get it to write the table to the db.
     
    rybrns, Mar 5, 2020 IP
  14. sarahk

    sarahk iTamer Staff

    Messages:
    26,216
    Likes Received:
    3,856
    Best Answers:
    108
    Trophy Points:
    665
    #14
    Because you're not distributing the script just scrap the whole function making the table, open up phpMyAdmin and make it manually.

    So in the second script snippet you don't need the Table_Exists function call.
     
    sarahk, Mar 5, 2020 IP
  15. rybrns

    rybrns Active Member

    Messages:
    15
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    88
    #15
    I have dozens of quizzes so making tables for all of them, with variable numbers of questions, would be a huge job. That's why I'm trying to use the old script which used to work with no problem.
     
    rybrns, Mar 5, 2020 IP
  16. sarahk

    sarahk iTamer Staff

    Messages:
    26,216
    Likes Received:
    3,856
    Best Answers:
    108
    Trophy Points:
    665
    #16
    you don't have one table per quiz

    If your script is any good you'll have
    • quiz (`id`,`name`,`questions`, `options`) I'd have options as a text field where each new line is a choice
    • answers (`id`, `quiz_id`, `answer`, `created`, `ip`)
     
    sarahk, Mar 5, 2020 IP
  17. JEET

    JEET Notable Member

    Messages:
    3,356
    Likes Received:
    371
    Best Answers:
    16
    Trophy Points:
    235
    #17
    Modify the function like this and try again.


    function Create_Table($testname, $answerarray) {
    global $db;
        $sql = "CREATE TABLE $testname (
            id SMALLINT(5) NOT NULL AUTO_INCREMENT,
            name varchar(30),
            score int,
            date DATETIME DEFAULT NULL,
            PRIMARY KEY (id),
            UNIQUE id (id)
            )";
        $result = mysqli_query($db, $sql) or die("Cannot create table: ".mysqli_error());
        $numberofquestions = count($answerarray);
        for($i=1 ;$i<=$numberofquestions ; $i++) {
            $newfieldname="q$i";
            $sql = "alter table $testname add column $newfieldname tinyint(1)";
            $result = mysqli_query($db, $sql) or die("Cannot update table: ".mysqli_error());
        }
    }
    Code (php):
    ...
     
    Last edited by a moderator: Mar 5, 2020
    JEET, Mar 5, 2020 IP
  18. rybrns

    rybrns Active Member

    Messages:
    15
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    88
    #18
    Tried and no go. Table not created. The only difference between what I was trying and what you suggested is the global statement. I had already modified the query statements to include the $link ($db). I accidentally picked an older code version to put here instead of the one with the $link that mysqli needs. No error is shown when I run the script.
     
    rybrns, Mar 5, 2020 IP
    JEET likes this.
  19. JEET

    JEET Notable Member

    Messages:
    3,356
    Likes Received:
    371
    Best Answers:
    16
    Trophy Points:
    235
    #19
    Post the code of your "Open_Database" function please.

    Make sure Error_reporting is not turned off.
    I am thinking error_reporting is off, that is why script is not showing errors.

    What are you seeing on the webpage when you run the script?
    You must be seeing something, if not errors...
     
    JEET, Mar 5, 2020 IP
  20. rybrns

    rybrns Active Member

    Messages:
    15
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    88
    #20
    function Open_Database($server, $user, $password, $database) {
        $db = mysqli_connect($server,$user,$password,$database) or die("Cannot connect to database: ".mysqli_connect_error());
        return $db;
    }
    
    PHP:
    I do see errors that stop the script from running. Don't know how to check error reporting. I don't have access to the server's php.ini.

    I get the full results of the scoring and answer display with no errors shown. But nothing gets written to the mysql database.
     
    rybrns, Mar 5, 2020 IP
    JEET likes this.