cahyadsn

DISC Personality Test prototype

Jan 6th, 2016
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.94 KB | None | 0 0
  1. <?php
  2. /************************************
  3. FILENAME     : personalities.php
  4. AUTHOR       : CAHYA DSN
  5. CREATED DATE : 2015-01-06
  6. UPDATE DATE  : 2015-01-07
  7. *************************************
  8.  
  9. CREATE DATABASE IF NOT EXISTS test;
  10. USE test;
  11.  
  12. DROP TABLE IF EXISTS personalities;
  13. CREATE TABLE IF NOT EXISTS personalities (
  14.   id tinyint(2) NOT NULL AUTO_INCREMENT,
  15.   no tinyint(2) NOT NULL,
  16.   term varchar(100) NOT NULL,
  17.   most char(1) NOT NULL,
  18.   least char(1) NOT NULL,
  19.   PRIMARY KEY (id)
  20. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  21.  
  22. //-- DATA NOT AVAILABLE ON FREE VERSION
  23.  
  24. */
  25. //-- database configuration
  26. $dbhost='localhost';
  27. $dbuser='root';
  28. $dbpass='';
  29. $dbname='test';
  30. //-- database connection
  31. $db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
  32. //-- query data from database
  33. $sql='SELECT * FROM personalities ORDER BY no ASC';
  34. $result=$db->query($sql);
  35. $data=array();
  36. while($row=$result->fetch_object()) $data[]=$row;
  37. $terms=json_encode($data);
  38. $cols=4; //<-- number of columns
  39. $rows=count($data)/(4*$cols);
  40. ?>
  41. <doctype html>
  42. <html>
  43.   <head>
  44.     <title>DISC Personality Test</title>
  45.     <style>
  46.     body,table {font-family: verdana,arial,sans-serif;font-size: 1em;}
  47.     input {background-color: #eee;line-height:1.5em;}
  48.     thead {background-color: #666;color: #fff;line-height: 2em; padding:0.2em;}
  49.     tfoot {background-color: #999;color: #fff;}
  50.     td {padding:0.2em;}
  51.     caption {font-size: 2em;}
  52.     .btn {background-color: #eee;line-height:2em;padding:0.2em 0.5em;
  53.           margin:0.2em;font-size:1.5em;font-weight:bold;}
  54.     .dark {background-color: #eee;}
  55.     .first {border-top: solid 0.2em #000; }
  56.     </style>
  57.   </head>
  58.   <body>
  59.     <form method='post'>
  60.     <table>
  61.       <caption>DISC Personality Test</caption>
  62.       <thead>
  63.         <tr>
  64.         <?php for($i=0;$i<$cols;++$i):?>
  65.           <th>No</th>
  66.           <th>M</th>
  67.           <th>L</th>
  68.           <th>term</th>
  69.         <?php endfor;?>
  70.         </tr>
  71.       </thead>
  72.       <tbody>
  73.       <?php
  74.       for($i=0;$i<$rows;++$i){
  75.         echo "<tr".($i%2==0?" class='dark'":"").">";
  76.         for($j=0;$j<$cols;++$j){
  77.             for($n=0;$n<4;++$n){
  78.                 if($j>0 && $n==0){
  79.                     echo "<tr".($i%2==0?" class='dark'":"").">";
  80.                 }elseif($j==0){
  81.                     echo "<th rowspan='$cols'"
  82.                         .($j==0?" class='first'":"").">"
  83.                         .($i+$n*$rows+1)
  84.                         ."</th>";
  85.                 }
  86.                 echo "<td".($j==0?" class='first'":"").">
  87.                         <input type='radio'
  88.                                name='m[".($i+$n*$rows)."]'
  89.                                value='{$data[$cols*($i+$n*$rows)+$j]->most}'
  90.                                required />
  91.                         {$data[$cols*($i+$n*$rows)+$j]->most}
  92.                       </td>
  93.                       <td".($j==0?" class='first'":"").">
  94.                         <input type='radio'
  95.                                name='l[".($i+$n*$rows)."]'
  96.                                value='{$data[$cols*($i+$n*$rows)+$j]->least}'
  97.                                required />
  98.                         {$data[$cols*($i+$n*$rows)+$j]->least}
  99.                       </td>
  100.                       <td".($j==0?" class='first'":"").">
  101.                         {$data[$cols*($i+$n*$rows)+$j]->term}
  102.                       </td>";
  103.             }
  104.           echo "</tr>";
  105.         }
  106.       }
  107.       ?>
  108.       </tbody>
  109.       <tfoot>
  110.         <tr>
  111.             <th colspan='16'>
  112.                 <input type='submit' value='kirim' class='btn'/>
  113.             </th>
  114.         </tr>
  115.       </tfoot>
  116.     </table>
  117.     </form>
  118.     <div>
  119.     <pre>
  120.     <?php
  121.     if(isset($_POST['m']) && isset($_POST['l'])){
  122.       $most=array_count_values($_POST['m']);
  123.       $least=array_count_values($_POST['l']);
  124.       $result=array();
  125.       $aspect=array('D','I','S','C','#');
  126.       foreach($aspect as $a){
  127.         $result[$a]['most']=isset($most[$a])?$most[$a]:0;
  128.         $result[$a]['least']=isset($least[$a])?$least[$a]:0;
  129.         $result[$a]['change']=$result[$a]['most']-$result[$a]['least'];
  130.       }
  131.       print_r($result);
  132.     }
  133.     ?>
  134.     </pre>
  135.     </div>
  136.   </body>
  137. </html>
Advertisement
Add Comment
Please, Sign In to add comment