next up previous contents index
Next: A 2-player coordination game Up: Examples Previous: Viewing SQL-Tables (PHP, MySQL)   Contents   Index


Several Questions and Treatments (PHP, MySQL)

The following example is more complex. It combines several treatments with several questions in each treatment. It also shows how to react adaptively to a particpants responses.

The script first checks the presence of a connection to the SQL-Server, the existence of a database and table. If database or table do not exist, they will be created.

The srcipt randomly selects one of four treatments. In the example the treatments differ in the background color. A set of background colours is definied in the first few lines of the script.

For each participant a set of four questions is asked. In the first few lines of the script a set of topics is defined. The sequence of the questions is determined randomly. For each call the script determines which question to ask next, checking which have already been asked.

After having asked all four questions the scripts finds the topic that was answered most negatively during the first stage. If there is a tie among several questions random numbers break the tie.

<HTML>
<?
    $treatments=array("springgreen","plum","wheat","skyblue");
    $questions=array("Beethoven","Mozart","Lessing","DeLillo");
    srand((double)microtime()*1000000);

    require ("header.inc"); // this defines $user and $pass

    // connect to the SQL server:
    if (! mysql_connect("localhost",$user,$pass)) {
        echo "could not connect to SQL-server";
        exit;
    }

    // open the database
    if (! mysql_select_db ("expCrash")) {
        mysql_create_db ("expCrash");
        if (! mysql_select_db ("expCrash")) {
            echo "failed to create database";
            exit;
        }
    }

    // verify existence of people-table:
    if (! mysql_query ("SELECT COUNT(*) FROM people")) {
        mysql_query ("CREATE TABLE people (
            vp_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
            treat INT,
            random DOUBLE)");
    }

    // verify existence of obs-table:
    if (! mysql_query ("SELECT COUNT(*) FROM obs")) {
        mysql_query ("CREATE TABLE obs (
            vp_id INT,
            treat INT,
            question INT,
            answer INT,
            random DOUBLE)");
    }

    if (! $VP_ID) {
        $TREAT = rand(0,sizeof($treatments)-1);
        mysql_query ("INSERT INTO people SET treat=$TREAT");
        $VP_ID = mysql_insert_id ();
    }
    

    // do we have to store something?
    if ( $ANSWER ) 
        mysql_query ("INSERT INTO obs SET vp_id=$VP_ID, 
            treat=$TREAT, question=$QUESTION, answer=$ANSWER, random=RAND()");

    // are we finished?
    $result=mysql_query ("SELECT answer FROM obs WHERE vp_id=$VP_ID");
    if (mysql_num_rows ($result)>=sizeof($questions)) {
        echo "<BODY BGCOLOR=$treatments[$TREAT]>";
        $result=mysql_query ("SELECT question FROM obs WHERE vp_id=$VP_ID ORDER BY answer, random");
        // if several answers are equally bad, random breaks the tie
        if ($row = mysql_fetch_object($result)) {
            $bad= $questions[$row->question];
            echo "You do not seem to like $bad...<P>";
        }
            </BODY></HTML>";
        exit;
    }

    // ok, we are not finished yet
    // find a new question
    for (;;) {
        $QUESTION=rand(0,sizeof($questions)-1);
        $result=mysql_query ("SELECT answer FROM obs WHERE vp_id=$VP_ID
            AND question=$QUESTION");
        if (mysql_num_rows ($result)==0) 
            break;
    }

    echo "<BODY BGCOLOR=$treatments[$TREAT]>
          <FORM METHOD=POST ACTION=$SCRIPT_NAME><h1>
          How do you find $questions[$QUESTION]?";

    echo "</h1><CENTER>(old-fashioned)\n";
    for ($i=1;$i<6;++$i) {
        echo "    $i";
        echo "<INPUT TYPE=RADIO VALUE=$i NAME=ANSWER>&nbsp;&nbsp;\n";
    }
    echo "(still modern)<P>
        <INPUT TYPE=HIDDEN NAME=VP_ID VALUE=$VP_ID>
        <INPUT TYPE=HIDDEN NAME=QUESTION VALUE=$QUESTION>
        <INPUT TYPE=HIDDEN NAME=TREAT VALUE=$TREAT>
        <INPUT TYPE=SUBMIT VALUE=Continue></CENTER></FORM>\n";
?>
</BODY>
</HTML>


next up previous contents index
Next: A 2-player coordination game Up: Examples Previous: Viewing SQL-Tables (PHP, MySQL)   Contents   Index
Oliver Kirchkamp