title: PHP Datahandling
body: <p>I've been searching and trying to find a way to handle data in a smart way with php.</p>
  
  <p>example: <code>{"number":"test","first_name":"test","last_name":"test","company":"tes123321D323","call_result":"test","call_duration":"500","comment":"test"}</code></p>
  
  <p>I have this json string and I want to get the data from it and echo it in a table.
  What data is taken form the json is configurable by the user.
  So lets say The user only wants to display a sum of which companys have called. </p>
  
  <p>I could create a function per time I handle the data differently but I want my code to be short and well it should adapt what kind of string / paramaters it gets.</p>
  
  <p>How should I display this data after I've json decoded it?
  Whats the best way to display an object multiple times with 1 function</p>
  
  <h2>Code</h2>
  
  <p>$component_data[0] is a mysql query with multiple json strings.</p>
  
  <pre><code>    if($component_data[0]-&gt;datatype !== "text"){
          foreach($component_data as $val){
              $isJira = ($val-&gt;datatype == "jirajson") ? true : false;
  
              $comp_objarr[] = json_decode(trim($val-&gt;data));
          }
  
          if($isJira){
              foreach($comp_objarr as $object){
                  $comp_objarrnew[$object-&gt;_displayName][] = $object;
              }
          }else{
              $comp_objarrnew[] = $comp_objarr;
          }
  
          $uid = 0;
          if(isset($_GET['uid'])){
              $uid = $_GET['uid'];
          }
  
          $var = "";
          $i = 0;
          $i2 = 0;
          echo "&lt;table&gt;";
          foreach($comp_objarrnew as $key =&gt; $object){
              if($isJira){
                  if($i == $uid){
                      usort($comp_objarr, 'cmp');
                      echo "&lt;tr&gt;&lt;th colspan=\"7\"&gt;$key&lt;/th&gt;&lt;/tr&gt;";
                      echo "&lt;tr&gt;";
                      foreach($object as $value){
                          $var .= "&lt;tr&gt;";
                          foreach($value as $key =&gt; $item){
                              if($i2 == 0){
                                  echo "&lt;th&gt;$key&lt;/th&gt;";
                              }
                              $var .= "&lt;td&gt;".$item."&lt;/th&gt;";
                          }
                          $i2++;
                          if($i2 &gt; 0){
                              echo "&lt;/tr&gt;";
                          }
                          $var .= "&lt;/tr&gt;";
                      }
                  }
                  $i++;
              }else{
                  foreach($object as $value){
                      if($i == 0){
                          $var .= "&lt;tr&gt;";
                          foreach($value as $key =&gt; $item){
                              $var .= "&lt;th&gt;".$key."&lt;/th&gt;";
                          }
                          $var .= "&lt;/tr&gt;";
                          $i++;
                      }
  
                      $var .= "&lt;tr&gt;";
                      foreach($value as $item){
                          $var .= "&lt;td&gt;".$item."&lt;/td&gt;";
                      }
                      $var .= "&lt;/tr&gt;";
                  }
              }
          }
          echo $var;
          echo "&lt;/table&gt;";
      }else{
          $i = 0;
          $var = "";
  
          echo "&lt;table&gt;";
          foreach($component_data as $val){
              if($i == 0){
                  echo "&lt;tr&gt;";
              }
  
              $var .= "&lt;tr&gt;";
              foreach($val as $key =&gt; $info){
                  if($i == 0){
                      echo "&lt;th&gt;$key&lt;/th&gt;";
                  }
                  $var .= "&lt;td&gt;$info&lt;/td&gt;";
              }
              $i++;
              $var .= "&lt;/tr&gt;";
          }
          echo $var;
          echo "&lt;/table&gt;";
      }
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/hSiZG.png" alt="">
  This is how it's currently being displayed</p>
  
  <p>and this is one of the displays that a user could add:</p>
  
  <p><img src="https://i.stack.imgur.com/njvkQ.png" alt="">
  like grouping the data per day.</p>
  
  <p>Users can add an unlimited amount of displays.
  such as only display company name
  group by count company name
  and so on.</p>
  
  <p>How should I edit my code if I want it to easily display something else? without creating lots of if statements.</p>