title: PHP format table in number format
body: <p>I'm quite new to PHP, so forgive me for asking this simple question, but I can't get a simple table formatted properly.</p>
  
  <p>I use PHP to get results from MYSQL and show that in a HTML page. The table has ID-labels left and on top, the body is numerical. </p>
  
  <p>Here is the PHP code:</p>
  
  <pre><code>$sql = "CALL pivot('DATA', 'amount', 'scen_id', 'dim2_id', 'where ent_id=''abc1''')";
  $res = mysqli_query($mysqli, $sql) or die('Query failed: ' . mysqli_error($mysqli));
  
  $row = mysqli_fetch_assoc($res);
  
  echo '&lt;table&gt;';
  
  // printing table header
  foreach($row as $name =&gt; $value) {
      echo "&lt;th&gt;$name&lt;/th&gt;";
  }
  // printing table rows
  while($row = mysqli_fetch_row($res))
  {
      echo "&lt;tr&gt;";
  
      foreach($row as $cell) 
          echo "&lt;td&gt;$cell&lt;/td&gt;\n";
  
      echo "&lt;/tr&gt;\n";
  }
  echo '&lt;/table&gt;';
  </code></pre>
  
  <p>This is what I get:</p>
  
  <pre><code>scen_id ProdA       ProdB       ProdC       Total
  BUD     59903423    66278016    55422083    181603522
  FORC    37195845    52753807    41339980    131289632
  Total   157331578   185154736   164437401   506923715
  </code></pre>
  
  <p>While this is better than an empty page, the first line with data (containing ACT... etc) is missing. It does show up when I run the query in Mysql. I want everything to align right, and the numbers to be formatted with thousands separators. I understand that you can't use CSS for that. </p>
  
  <p>I tried anything already, like this one:</p>
  
  <pre><code>echo "&lt;td&gt;".number_format($cell)."&lt;/td&gt;\n";
  </code></pre>
  
  <p>but then the result is this:</p>
  
  <pre><code>scen_id ProdA       ProdB       ProdC       Total
          59,903,423  66,278,016  55,422,083  181,603,522
          37,195,845  52,753,807  41,339,980  131,289,632
          157,331,578 185,154,736 164,437,401 506,923,715
  </code></pre>
  
  <p>No more row labels.... And also still not aligned right. And still the first line is missing. I hope somebody can tell me how to get this to work. I suppose it's such a standard task that nobody asks this question. I could not find much on the web.</p>
  
  <p>edit: Okay problem solved, thanks guys! For the sake of completeness: here is my working code;</p>
  
  <pre><code>$sql="CALL pivot('DATA', 'amount', 'scen_id', 'dim2_id', 'where ent_id=''abc1''')";
  $res=mysqli_query($mysqli, $sql) or die('Query failed: ' . mysqli_error($mysqli));
  
  echo '&lt;table&gt;';
  
  $first = true;
  while($row = mysqli_fetch_assoc($res)) {
     if ($first) {
         foreach(array_keys($row) as $name) {
              echo "&lt;th&gt;$name&lt;/th&gt;";
         }
         $first = false;
     }
      echo "&lt;tr&gt;";
          foreach($row as $cell) 
          if (!is_numeric($cell)) 
              {echo "&lt;td&gt;$cell&lt;/td&gt;\n";} 
          else 
              {echo "&lt;td&gt;".number_format($cell)."&lt;/td&gt;\n";}
  
      echo "&lt;/tr&gt;\n";
  }
  
  echo '&lt;/table&gt;';
  
  ?&gt;
  </code></pre>
  
  <p>and here the CSS code:</p>
  
  <pre><code>table tr td, table tr th {
      text-align: right;
      font-size: 10px;
      font-family: Verdana, 'Lucida Grande', Helvetica, Arial, Sans-Serif;
      padding: 0 4px;
  }
  </code></pre>