UPPREVIOUSNEXT  Technologies Internet et Education, © TECFA
  4. Questionnaires on-line et récupération dans un fichier

4. Questionnaires on-line et récupération dans un fichier

Exemple 4-1: Questionnaire et résultats dans fichier

A. le formulaire

$scales = array("food", "work", "love", "leisure", "sports");function scale ($thing) {   echo "<TR> <TD align=right>Importance of <STRONG>$thing</STRONG>:</TD>";   echo "<TD><select name=$thing>";   echo "<option value=1>1 - totally unimportant";   echo "<option value=2>2 - not important";   echo "<option value=3 selected>3 - rather not important";   echo "<option value=4>4 - slightly important";   echo "<option value=5>5 - rather important";   echo "<option value=6>6 - very important";   echo "</select>";   echo "</TD></TR>";   }


function dump_scales ()  { global $scales; reset($scales); do {    $scale = scale(current($scales));    echo "$scale\n";    }    while (next($scales)); } ?><form> <table>......dump_scales();......</table> </form>Ecrire dans un fichier  // check existance of file (or try to create it)  // a better alternative to touch() would be is_file, is_writable and so on.  $try = touch($file_name);  if (!$try) {    echo "<p>Sorry I can’t open a file, something is wrong";    exit;  }  // this is the stuff we get from the form, we insert it into an array    $input = array ($login, $password, $fullname, $url, $food, $work, $love, $leisure, $sports);    // so we can make a big string with tabs between the elements    // note that we add a \n (line break) to the end of the string.    $output_line = implode ($input, 



// Now open the file (get a file pointer)
// We will append to it and therefore use the "a" option
$output_stream = fopen($file_name, "a");
// and dump the string into the file
$result = fputs ($output_stream, $output_line);

// give feedback
if ($result) {
echo "<p>Your data have successfully been registered.";
else {
echo "<p>Too bad, the db did not want your data.";
// close the file pointer

// EXIT here ... we don’t want to see the form again. If you do, kill the exit
A retenir:

B. Afficher le contenu d’un fichier

.... on ne se fatigue pas: le tout dans un <pre> avec un “include”

<BODY>    <H1>Dump Database Contents</H1><?   /* Daniel.Schneider@tecfa.unige.ch  Will dump the contents of the results file  */?>  <strong>Results registered so far:</strong><pre><? readfile("results/result.text"); ?></pre>..........</BODY>