Advertisement
xah

function create_contingency_table

xah
Sep 28th, 2020 (edited)
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.52 KB | None | 0 0
  1. <?php
  2.  
  3. function create_table_rows($arr, $main_arr){
  4.     // Dunno what code it is, but ignore it. // TODO: Change to create_general_table()
  5. }
  6.  
  7. /**EXAMPLE
  8.  
  9. $fields = array('Condition', 'Product')
  10. create_contingency_table('Condition', 'Product', $all_stock_arr, 'Product');
  11.  
  12. **/
  13.  
  14. function create_contingency_table($var1_name, $var2_name, $main_arr, $header = ''){
  15.     $cont_arry = array();
  16.  
  17.     foreach($main as $m){
  18.         $item_code = $m['ItemCode'];
  19.         $var1_code = $m[$var1_name];
  20.         $var2_code = $m[$var2_name];
  21.         $cont_code = $var1_code . $var2_code;
  22.  
  23.         if(! isset($cont_test) || $cont_test != $cont_code ){
  24.             $cont_test = $cont_code;
  25.         }
  26.  
  27.         $cont_arry[$item_code]['var1_code'] = $var1_code;
  28.         $cont_arry[$item_code]['var2_code'] = $var2_code;
  29.         $cont_arry[$item_code]['var_1_n_2'] = $cont_code;
  30.     }
  31.  
  32.     echo '<table><tr><th>' . $header . '</th>';
  33.  
  34.     $var1_arry = array_count_values(array_column($main_arr, 'var1_code'));
  35.     $var2_arry = array_count_values(array_column($main_arr, 'var2_code'));
  36.     $var12_arr = array_count_values(array_column($main_arr, 'var_1_n_2'));
  37.  
  38.     foreach($var1_arry as $var1_key => $var1_val){
  39.         echo '<th>' . $var1_key . '</th>';
  40.     }
  41.  
  42.     echo '</tr>';
  43.  
  44.     foreach($var2_arry as $var2_key => $var2_val){
  45.         echo '<tr><td>' . $var2_key . '</td>';
  46.  
  47.         foreach($var1_arry as $var1_key => $var1_val){
  48.             $key_code = $var1_key . $var2_key;
  49.  
  50.             if(isset($var12_arr[$key_code])){
  51.                 echo '<td>' . $var12_arr[$key_code] . '</td>';}
  52.             else{
  53.                 echo '<td></td>';
  54.             }
  55.         }
  56.         echo '</tr>';
  57.     }
  58.     echo '</table>';
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement