В моем текущем коде есть функция суммарной суммы, которая принимает значения из базы данных.
Я также пытаюсь применить к моей таблице другой код Javascript, который позволяет сортировать данные в строках.
Например, после сортировки строк для отображения всех 5 активов ACER сверху. я хочу поставить галочку в верхних 5 отображаемых активах ACER и суммировать их.
Это мой текущий код:
<?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> <link rel="stylesheet" href="style.css" /> Total Cost : <input type="text" name="sen" id="totalcost" value="" /> <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> <th><h3>Date Of Purchase</h3></th> <th><h3>Select Values</h3></th> </tr> </thead> <tbody> <?php // include('connect1.php'); you already included this file $result = $db->prepare("SELECT * FROM Asset"); $result->bindParam(':userid', $res); $result->execute(); for($i=0; $row = $result->fetch(); $i++){ ?> <tr> <td><?php echo $row['Asset_ID']; ?></td> <td><?php echo $row['Vendor_Name']; ?></td> <td><?php echo $row['Hardware_ID']; ?></td> <td><?php echo $row['Asset_Cost']; ?></td> <td><?php echo $row['DateOfPurchase']; ?></td> <th><h3><INPUT TYPE="checkbox" NAME="items[]" value="<?php echo $row['Asset_Cost'] ?>" id="sum_m_<?php echo $i ?>" onclick="UpdateCost()"></h3></th> </tr> <?php } ?> </tbody> </table> <br /> <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>
Это Javascript:
var TINY={}; function T$(i){return document.getElementById(i)} function T$$(e,p){return p.getElementsByTagName(e)} TINY.table=function(){ function sorter(n){this.n=n; this.pagesize=20; this.paginate=0} sorter.prototype.init=function(e,f){ var t=ge(e), i=0; this.e=e; this.l=trlength; ta=[]; th=T$$('thead',T$(e))[0].rows[0]; tw=thcells.length; for(i;i<tw;i++){ var c=thcells[i]; if(c.className!='nosort'){ c.className=this.head; c.onclick=new Function(this.n+'.wk(this.cellIndex)') } } for(i=0;i<this.l;i++){ta[i]={}} if(f!=null){var a=new Function(this.n+'.wk('+f+')'); a()} if(this.paginate){this.g=1; this.pages()} }; sorter.prototype.wk=function(y){ var t=ge(this.e), x=thcells[y], i=0; for(i;i<this.l;i++){ ta[i].o=i; var v=tr[i].cells[y]; tr[i].style.display=''; while(v.hasChildNodes()){v=v.firstChild} ta[i].v=v.nodeValue?v.nodeValue:'' } for(i=0;i<tw;i++){var c=thcells[i]; if(c.className!='nosort'){c.className=this.head}} if(tp==y){tareverse(); x.className=td?this.asc:this.desc; td=td?0:1} else{tp=y; tasort(cp); td=0; x.className=this.asc} var n=document.createElement('tbody'); for(i=0;i<this.l;i++){ var r=tr[ta[i].o].cloneNode(true); n.appendChild(r); r.className=i%2==0?this.even:this.odd; var cells=T$$('td',r); for(var z=0;z<tw;z++){cells[z].className=y==z?i%2==0?this.evensel:this.oddsel:''} } t.replaceChild(n,tb); if(this.paginate){this.size(this.pagesize)} }; sorter.prototype.page=function(s){ var t=ge(this.e), i=0, l=s+parseInt(this.pagesize); if(this.currentid&&this.limitid){T$(this.currentid).innerHTML=this.g} for(i;i<this.l;i++){tr[i].style.display=i>=s&&i<l?'':'none'} }; sorter.prototype.move=function(d,m){ var s=d==1?(m?this.d:this.g+1):(m?1:this.g-1); if(s<=this.d&&s>0){this.g=s; this.page((s-1)*this.pagesize)} }; sorter.prototype.size=function(s){ this.pagesize=s; this.g=1; this.pages(); this.page(0); if(this.currentid&&this.limitid){T$(this.limitid).innerHTML=this.d} }; sorter.prototype.pages=function(){this.d=Math.ceil(this.l/this.pagesize)}; function ge(e){var t=T$(e); tb=T$$('tbody',t)[0]; tr=tbrows; return t}; function cp(f,c){ var g,h; f=g=fvtoLowerCase(), c=h=cvtoLowerCase(); var i=parseFloat(f.replace(/(\$|\,)/g,'')), n=parseFloat(c.replace(/(\$|\,)/g,'')); if(!isNaN(i)&&!isNaN(n)){g=i,h=n} i=Date.parse(f); n=Date.parse(c); if(!isNaN(i)&&!isNaN(n)){g=i; h=n} return g>h?1:(g<h?-1:0) }; return{sorter:sorter} }();
вvar TINY={}; function T$(i){return document.getElementById(i)} function T$$(e,p){return p.getElementsByTagName(e)} TINY.table=function(){ function sorter(n){this.n=n; this.pagesize=20; this.paginate=0} sorter.prototype.init=function(e,f){ var t=ge(e), i=0; this.e=e; this.l=trlength; ta=[]; th=T$$('thead',T$(e))[0].rows[0]; tw=thcells.length; for(i;i<tw;i++){ var c=thcells[i]; if(c.className!='nosort'){ c.className=this.head; c.onclick=new Function(this.n+'.wk(this.cellIndex)') } } for(i=0;i<this.l;i++){ta[i]={}} if(f!=null){var a=new Function(this.n+'.wk('+f+')'); a()} if(this.paginate){this.g=1; this.pages()} }; sorter.prototype.wk=function(y){ var t=ge(this.e), x=thcells[y], i=0; for(i;i<this.l;i++){ ta[i].o=i; var v=tr[i].cells[y]; tr[i].style.display=''; while(v.hasChildNodes()){v=v.firstChild} ta[i].v=v.nodeValue?v.nodeValue:'' } for(i=0;i<tw;i++){var c=thcells[i]; if(c.className!='nosort'){c.className=this.head}} if(tp==y){tareverse(); x.className=td?this.asc:this.desc; td=td?0:1} else{tp=y; tasort(cp); td=0; x.className=this.asc} var n=document.createElement('tbody'); for(i=0;i<this.l;i++){ var r=tr[ta[i].o].cloneNode(true); n.appendChild(r); r.className=i%2==0?this.even:this.odd; var cells=T$$('td',r); for(var z=0;z<tw;z++){cells[z].className=y==z?i%2==0?this.evensel:this.oddsel:''} } t.replaceChild(n,tb); if(this.paginate){this.size(this.pagesize)} }; sorter.prototype.page=function(s){ var t=ge(this.e), i=0, l=s+parseInt(this.pagesize); if(this.currentid&&this.limitid){T$(this.currentid).innerHTML=this.g} for(i;i<this.l;i++){tr[i].style.display=i>=s&&i<l?'':'none'} }; sorter.prototype.move=function(d,m){ var s=d==1?(m?this.d:this.g+1):(m?1:this.g-1); if(s<=this.d&&s>0){this.g=s; this.page((s-1)*this.pagesize)} }; sorter.prototype.size=function(s){ this.pagesize=s; this.g=1; this.pages(); this.page(0); if(this.currentid&&this.limitid){T$(this.limitid).innerHTML=this.d} }; sorter.prototype.pages=function(){this.d=Math.ceil(this.l/this.pagesize)}; function ge(e){var t=T$(e); tb=T$$('tbody',t)[0]; tr=tbrows; return t}; function cp(f,c){ var g,h; f=g=fvtoLowerCase(), c=h=cvtoLowerCase(); var i=parseFloat(f.replace(/(\$|\,)/g,'')), n=parseFloat(c.replace(/(\$|\,)/g,'')); if(!isNaN(i)&&!isNaN(n)){g=i,h=n} i=Date.parse(f); n=Date.parse(c); if(!isNaN(i)&&!isNaN(n)){g=i; h=n} return g>h?1:(g<h?-1:0) }; return{sorter:sorter} }();
это фактическая рабочая таблица сортировки:
<?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> <input type="button" value="Search" onclick="window.location.href='http://mp04.bit-mp.biz/Ashvin/SearchACost.php'" /> <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> <th><h3><INPUT TYPE="checkbox" NAME="items[]" value="<?php echo $row['Asset_Cost'] ?>" id="sum_m_<?php echo $i ?>" onclick="UpdateCost()"></h3></th> </tr> </thead> <tbody> <?php mysql_connect("localhost","user","pass"); mysql_select_db("db"); $result = mysql_query("SELECT * FROM Asset"); while($row = mysql_fetch_array($result)) for($i=0; $row = $result->fetch(); $i++){ { 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> </body> </html>
в<?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> <input type="button" value="Search" onclick="window.location.href='http://mp04.bit-mp.biz/Ashvin/SearchACost.php'" /> <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> <th><h3><INPUT TYPE="checkbox" NAME="items[]" value="<?php echo $row['Asset_Cost'] ?>" id="sum_m_<?php echo $i ?>" onclick="UpdateCost()"></h3></th> </tr> </thead> <tbody> <?php mysql_connect("localhost","user","pass"); mysql_select_db("db"); $result = mysql_query("SELECT * FROM Asset"); while($row = mysql_fetch_array($result)) for($i=0; $row = $result->fetch(); $i++){ { 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> </body> </html>