mardi 26 janvier 2016

Combing a checkbox auto sum and a sortable javascript table

I have 2 separate set of codes. One that auto sums multiple values of a database row. Another is a simple javascript table that allows me to sort by clicking header.

This is my code for the auto sum checkbox

<?php
include('connect1.php');
$retrieve = $db->prepare("SELECT count(*) FROM Asset");
    $retrieve->execute();
    $fetchrow = $retrieve->fetch(PDO::FETCH_NUM);
    $calculated=$fetchrow[0];
?>
<script type="text/javascript">

function UpdateCost() {
  var sum = 0;
  var gn, elem;
  for (i=0; i<<?php echo $calculated ?>; i++) {
    gn = 'sum_m_'+i;
    elem = document.getElementById(gn);
    if (elem.checked == true) { sum += Number(elem.value); }
  }
  document.getElementById('totalcost' ).value = sum.toFixed(0);
}
window.onload=UpdateCost

</script>


<?php
include('connect1.php');
$result = $db->prepare("SELECT * FROM Asset");
        $result->bindParam(':userid', $res);
        $result->execute();
        for($i=0; $row = $result->fetch(); $i++){
    ?>






        <INPUT TYPE="checkbox" NAME="items[]" value="<?php echo $row['Asset_Cost'] ?>" id="sum_m_<?php echo $i ?>" onclick="UpdateCost()">


        <td>

        <?php echo $row['Asset_ID'] ?>
        <?php echo $row['Vendor_Name'] ?>
        <?php echo $row['Hardware_ID'] ?>
        <?php echo $row['Asset_Cost'] ?>



        </td>

        <br>


    <?php
    }
?>
<br>

Total Cost : <input type="text" name="sen" id="totalcost" value="">

This is my code for the sortable table

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Costing</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
    <table cellpadding="0" cellspacing="0" border="0" id="table" class="sortable">
        <thead>
            <tr>


                <th><h3>Asset ID</h3></th>
                <th><h3>Vendor</h3></th>
                <th><h3>Hardware </h3></th>
                <th><h3>Cost</h3></th>

            </tr>
        </thead>
        <tbody>

        <?php

        mysql_connect("host","user","pass");
mysql_select_db("db name");

$result = mysql_query("SELECT * FROM Asset");

        while($row = mysql_fetch_array($result)) 
 { 


    echo "<tr>";

    echo "<td>" . $row['Asset_ID']."</td>"; 
    echo "<td>" . $row['Vendor_Name'] . "</td>"; 
    echo "<td>" . $row['Hardware_ID'] . "</td>"; 
    echo "<td>" . $row['Asset_Cost'] . "</td>"; 

    echo "</tr>";
 } 
echo "</table>";
            ?>

        </tbody>
  </table>
    <div id="controls">
        <div id="perpage">
            <select onchange="sorter.size(this.value)">
            <option value="5">5</option>
                <option value="10" selected="selected">10</option>
                <option value="20">20</option>
                <option value="50">50</option>
                <option value="100">100</option>
            </select>
            <span>Entries Per Page</span>
        </div>
        <div id="navigation">
            <img src="images/first.gif" width="16" height="16" alt="First Page" onclick="sorter.move(-1,true)" />
            <img src="images/previous.gif" width="16" height="16" alt="First Page" onclick="sorter.move(-1)" />
            <img src="images/next.gif" width="16" height="16" alt="First Page" onclick="sorter.move(1)" />
            <img src="images/last.gif" width="16" height="16" alt="Last Page" onclick="sorter.move(1,true)" />
        </div>
        <div id="text">Displaying Page <span id="currentpage"></span> of <span id="pagelimit"></span></div>
    </div>
    <script type="text/javascript" src="script.js"></script>
    <script type="text/javascript">
  var sorter = new TINY.table.sorter("sorter");
    sorter.head = "head";
    sorter.asc = "asc";
    sorter.desc = "desc";
    sorter.even = "evenrow";
    sorter.odd = "oddrow";
    sorter.evensel = "evenselected";
    sorter.oddsel = "oddselected";
    sorter.paginate = true;
    sorter.currentid = "currentpage";
    sorter.limitid = "pagelimit";
    sorter.init("table",1);
  </script>

  <script>
  function UpdateCost() {
  var sum = 0;
  var gn, elem;
  for (i=0; i<<?php echo $calculated ?>; i++) {
    gn = 'sum_m_'+i;
    elem = document.getElementById(gn);
    if (elem.checked == true) { sum += Number(elem.value); }
  }
  document.getElementById('totalcost' ).value = sum.toFixed(0);
}
window.onload=UpdateCost
</script>
</body>
</html>




Aucun commentaire:

Enregistrer un commentaire