Home > Article > Backend Development > php counts questionnaire results
Background:
Due to specific work reasons, I made a paper questionnaire. The main content of the questionnaire is to let users prioritize 10 requirements (numbered A, B...) , so the results I got were hundreds of results similar to A>I>H>G>D....etc.
Goal:
Conduct a quantitative evaluation of these 10 requirements based on the user's ranking results. The final result is expected to be A:78, B:68, C:70... to find out which element is relatively important. , while others are relatively unimportant.
How to do it:
Give different weights according to the ranking of the numbers, count all the results, and summarize these weights. For example: The result "ABCDEFGHIJ" means that item A gets 10 points, item J gets 1 point, and item D gets 7 points.
Knowledge points:
File reading; Loop; Associative array; Array sorting.
php code:
<span> 1</span><span>$rs</span> =<span>array</span>("A"=>0,"B"=>0,"C"=>0,"D"=>0,"E"=>0,"F"=>0,"G"=>0,"H"=>0,"I"=>0,"J"=>0<span>); </span><span> 2</span><span>$handle</span> = <span>fopen</span>('./file.txt', 'r'<span>); </span><span> 3</span><span>while</span>(!<span>feof</span>(<span>$handle</span><span>)) </span><span> 4</span><span>{ </span><span> 5</span><span>$string</span> = <span>fgets</span>(<span>$handle</span>, 1024<span>); </span><span> 6</span><span>for</span>(<span>$i</span>=0;<span>$i</span><<span>strlen</span>(<span>$string</span>);<span>$i</span>++<span>) </span><span> 7</span><span> { </span><span> 8</span><span>$t</span> = <span>strtoupper</span>(<span>$string</span>[<span>$i</span><span>]); </span><span> 9</span><span>if</span>(<span>isset</span>(<span>$rs</span>[<span>$t</span><span>])) </span><span>10</span><span>$rs</span>[<span>$t</span>] = <span>$rs</span>[<span>$t</span>]+ <span>strlen</span>(<span>$string</span>) - <span>$i</span><span>; </span><span>11</span><span> } </span><span>12</span><span>} </span><span>13</span><span>fclose</span>(<span>$handle</span><span>); </span><span>14</span><span>arsort</span>(<span>$rs</span><span>); </span><span>15</span><span>var_dump</span>(<span>$rs</span>);
Description: file.txt is a text file, each line of which represents the results of a questionnaire, similar to something like "ABCDEFGHIJ". How did you get this file? Okay, I admit that I didn’t enter it manually. I asked some people to help me (why not do an online questionnaire? Save me so much trouble)
The above introduces PHP to collect statistics on questionnaire results, including loop content. I hope it will be helpful to friends who are interested in PHP tutorials.