calculated columns

calculated columns

vbierschwalevbierschwale Posts: 3Questions: 1Answers: 0

https://version8.guestworkervisas.com/tablea7.php

My table has the following columns.
Month varchar(255) DEFAULT NULL,
Year varchar(255) DEFAULT NULL,
Foreign Born Employed varchar(255) DEFAULT NULL,
Foreign Born Unemployed varchar(255) DEFAULT NULL,
Native Born Employed varchar(255) DEFAULT NULL,
Native Born Unemployed varchar(255) DEFAULT NULL,
id int(11) NOT NULL AUTO_INCREMENT,

From these, it calculates the rest of the columns.

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<tr>";
$myr = $row['Month'] . ' ' . $row['Year'];
echo "<td>" . $myr . "</td>";
echo "<td align='right'>" . number_format($row['Foreign Born Employed']) . "</td>";
echo "<td align='right'>" . number_format($row['Foreign Born Unemployed']) . "</td>";
echo "<td align='right'>" . number_format($row['Foreign Born Unemployed'] + $row['Foreign Born Employed']) . "</td>";

      $currfb = $row['Foreign Born Unemployed'] + $row['Foreign Born Employed'];

      echo "<td align='right'>" . number_format($row['Native Born Employed']) . "</td>";
      echo "<td align='right'>" . number_format($row['Native Born Unemployed']) . "</td>";
      echo "<td align='right'>" . number_format($row['Native Born Unemployed'] + $row['Native Born Employed']) . "</td>";

      $currnb = $row['Native Born Unemployed'] + $row['Native Born Employed'];

      echo "<td align='right'>" . number_format($row['Foreign Born Employed'] + $row['Foreign Born Unemployed'] + $row['Native Born Employed'] + $row['Native Born Unemployed']) . "</td>";
      $twf = $row['Foreign Born Employed'] + $row['Foreign Born Unemployed'] + $row['Native Born Employed'] + $row['Native Born Unemployed'];
      echo "<td align='right'>" . round(($row['Foreign Born Employed'] + $row['Foreign Born Unemployed']) / $twf,3) . "</td>";
      echo "<td align='right'>" . round(($row['Native Born Employed'] + $row['Native Born Unemployed']) / $twf,3) . "</td>";

//foreign born gain
if (($currfb - $fboldest) > ($currnb - $nboldest)) {
echo "<td align='right' style='background-color:red; color:white'>" . $currfb - $fboldest . "</td>";
} else {
echo "<td align='right'>" . $currfb - $fboldest . "</td>";
}

//native born gain
if (($currnb - $nboldest) > ($currfb - $fboldest)) {
echo "<td align='right' style='background-color:green; color:white'>" . $currnb - $nboldest . "</td>";
} else {
echo "<td align='right'>" . $currnb - $nboldest . "</td>";
}
//echo "<td align='right'>" . $currnb - $nboldest . "</td>";
$wftotal = ($currfb - $fboldest) + ($currnb - $nboldest);
//combined gain
echo "<td align='right'>" . ($currfb - $fboldest) + ($currnb - $nboldest) . "</td>";

      //echo $myr;
      //echo "<td align='right' style='background-color:green'>" . round((($currnb - $nboldest) / $wftotal),3) . "</td>";
      if ($myr <> 'Jan 2007') {
        if ((($currfb - $fboldest) / $wftotal) > (($currnb - $nboldest) / $wftotal)) {
          echo "<td align='right' style='background-color:red; color:white'>" . round((($currfb - $fboldest) / $wftotal),3) . "</td>";
        } else {
          echo "<td align='right'>" . round((($currfb - $fboldest) / $wftotal),3) . "</td>";
        }
        if ((($currnb - $nboldest) / $wftotal) > (($currfb - $fboldest) / $wftotal)) {
          echo "<td align='right' style='background-color:green; color:white'>" . round((($currnb - $nboldest) / $wftotal),3) . "</td>";
        } else {
          echo "<td align='right'>" . round((($currnb - $nboldest) / $wftotal),3) . "</td>";
        }
      } else {

        echo "<td align='right'>&nbsp;</td>";
        echo "<td align='right'>&nbsp;</td>";


      }

I simply want to export this to excel so that people can download the data.
Works fine on all my other tables.
Just not this one with the calculated columns.

Thanks,

Virgil

Answers

  • kthorngrenkthorngren Posts: 21,289Questions: 26Answers: 4,943

    There are a few issues on your page.

    The buttons aren't showing because you are loading datatables.js multiple times on lines 12, 17 and 40. Remove the lines 12 and 40 to load Datatables from the the concatenated JS on line 17. This should display allow the buttons to display.

    You are getting this error in the browser's console:

    jquery.min.js:2 Uncaught TypeError: Cannot read properties of undefined (reading 'mData')
        at HTMLTableCellElement.<anonymous> (jquery.dataTables.min.js:105:173)
        at Function.each (jquery.min.js:2:2976)
        at S.fn.init.each (jquery.min.js:2:1454)
        at HTMLTableElement.<anonymous> (jquery.dataTables.min.js:105:126)
        at Function.each (jquery.min.js:2:2976)
        at S.fn.init.each (jquery.min.js:2:1454)
        at S.fn.init.u [as dataTable] (jquery.dataTables.min.js:98:53)
        at l.fn.DataTable (jquery.dataTables.min.js:187:103)
        at HTMLDocument.<anonymous> (tablea7.php:235:31)
        at e (jquery.min.js:2:30005)
    

    ablea7.php:235:31 is pointing to the summary table. The table doesn't meet Datatables requirements for the HTML table. See the HTML docs for details. First there isn't a thead. Second issue is the table has 5 columns but you have one row with only 4. Datatables requires each row to have the same number of columns as the header row. Here is your HTML table:

    <table id="summary" class="table table-striped dataTable" style="width:100%">
       <tbody>
          <tr>
             <th>Year</th>
             <th>Month</th>
             <th>Foreign Born</th>
             <th>Native Born</th>
             <th>Total Workforce</th>
          </tr>
          <tr>
             <td>2024</td>
             <td>Mar</td>
             <td>32,292</td>
             <td>135,668</td>
             <td>167,960</td>
          </tr>
          <tr>
             <td>2007</td>
             <td>Jan</td>
             <td>23,494</td>
             <td>128,430</td>
             <td>151,924</td>
          </tr>
          <tr>
             <td></td>
             <td>------</td>
             <td>------</td>
             <td>------</td>
          </tr>
          <tr>
             <td>Jobs Created Since Jan 2007</td>
             <td>&nbsp;</td>
             <td>8,798</td>
             <td>7,238</td>
             <td>16,036</td>
          </tr>
       </tbody>
    </table>
    

    Kevin

  • vbierschwalevbierschwale Posts: 3Questions: 1Answers: 0

    Thanks, yes I added the labor force participation rate column recently.
    Appreciate the advice.

  • vbierschwalevbierschwale Posts: 3Questions: 1Answers: 0

    I have removed lines 17 and 40.
    the datatable I'm having a problem with is example.
    for some reason, the excel button will not show up.

    Thanks,

    Virgil

  • kthorngrenkthorngren Posts: 21,289Questions: 26Answers: 4,943

    I don't see those updates in your test case. I cleared my browser's cash but still see the same web page source. Please update the test case to show the issues you are currently having.

    Kevin

Sign In or Register to comment.