Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /************************************
- FILENAME : personalities.php
- AUTHOR : CAHYA DSN
- CREATED DATE : 2015-01-06
- UPDATE DATE : 2015-01-07
- *************************************
- CREATE DATABASE IF NOT EXISTS test;
- USE test;
- DROP TABLE IF EXISTS personalities;
- CREATE TABLE IF NOT EXISTS personalities (
- id tinyint(2) NOT NULL AUTO_INCREMENT,
- no tinyint(2) NOT NULL,
- term varchar(100) NOT NULL,
- most char(1) NOT NULL,
- least char(1) NOT NULL,
- PRIMARY KEY (id)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- //-- DATA NOT AVAILABLE ON FREE VERSION
- */
- //-- database configuration
- $dbhost='localhost';
- $dbuser='root';
- $dbpass='';
- $dbname='test';
- //-- database connection
- $db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
- //-- query data from database
- $sql='SELECT * FROM personalities ORDER BY no ASC';
- $result=$db->query($sql);
- $data=array();
- while($row=$result->fetch_object()) $data[]=$row;
- $terms=json_encode($data);
- $cols=4; //<-- number of columns
- $rows=count($data)/(4*$cols);
- ?>
- <doctype html>
- <html>
- <head>
- <title>DISC Personality Test</title>
- <style>
- body,table {font-family: verdana,arial,sans-serif;font-size: 1em;}
- input {background-color: #eee;line-height:1.5em;}
- thead {background-color: #666;color: #fff;line-height: 2em; padding:0.2em;}
- tfoot {background-color: #999;color: #fff;}
- td {padding:0.2em;}
- caption {font-size: 2em;}
- .btn {background-color: #eee;line-height:2em;padding:0.2em 0.5em;
- margin:0.2em;font-size:1.5em;font-weight:bold;}
- .dark {background-color: #eee;}
- .first {border-top: solid 0.2em #000; }
- </style>
- </head>
- <body>
- <form method='post'>
- <table>
- <caption>DISC Personality Test</caption>
- <thead>
- <tr>
- <?php for($i=0;$i<$cols;++$i):?>
- <th>No</th>
- <th>M</th>
- <th>L</th>
- <th>term</th>
- <?php endfor;?>
- </tr>
- </thead>
- <tbody>
- <?php
- for($i=0;$i<$rows;++$i){
- echo "<tr".($i%2==0?" class='dark'":"").">";
- for($j=0;$j<$cols;++$j){
- for($n=0;$n<4;++$n){
- if($j>0 && $n==0){
- echo "<tr".($i%2==0?" class='dark'":"").">";
- }elseif($j==0){
- echo "<th rowspan='$cols'"
- .($j==0?" class='first'":"").">"
- .($i+$n*$rows+1)
- ."</th>";
- }
- echo "<td".($j==0?" class='first'":"").">
- <input type='radio'
- name='m[".($i+$n*$rows)."]'
- value='{$data[$cols*($i+$n*$rows)+$j]->most}'
- required />
- {$data[$cols*($i+$n*$rows)+$j]->most}
- </td>
- <td".($j==0?" class='first'":"").">
- <input type='radio'
- name='l[".($i+$n*$rows)."]'
- value='{$data[$cols*($i+$n*$rows)+$j]->least}'
- required />
- {$data[$cols*($i+$n*$rows)+$j]->least}
- </td>
- <td".($j==0?" class='first'":"").">
- {$data[$cols*($i+$n*$rows)+$j]->term}
- </td>";
- }
- echo "</tr>";
- }
- }
- ?>
- </tbody>
- <tfoot>
- <tr>
- <th colspan='16'>
- <input type='submit' value='kirim' class='btn'/>
- </th>
- </tr>
- </tfoot>
- </table>
- </form>
- <div>
- <pre>
- <?php
- if(isset($_POST['m']) && isset($_POST['l'])){
- $most=array_count_values($_POST['m']);
- $least=array_count_values($_POST['l']);
- $result=array();
- $aspect=array('D','I','S','C','#');
- foreach($aspect as $a){
- $result[$a]['most']=isset($most[$a])?$most[$a]:0;
- $result[$a]['least']=isset($least[$a])?$least[$a]:0;
- $result[$a]['change']=$result[$a]['most']-$result[$a]['least'];
- }
- print_r($result);
- }
- ?>
- </pre>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment