Toggling variable between jquery and php -
i've seen questions on board, none matches situation, please don't mark me down until you've read problem.
i have table. each th of table has id matches field in mysql database. when click th, want conduct query of database order direction of desc. if click same field again, want query have order direction of asc.
the problem maintaining value of direction across states. far, direction isn't changing when click table header.
can give me idea please of i'm doing wrong?
html:
<div id="content"></div>
jquery:
$(document).ready(function() { loaddata(); }); function loaddata(orderby) { $.ajax({ url: 'display.php', type: "post", data: ({orderby: orderby, direction: 'desc'}), success: function(data){ $("#content").hide(); $("#content").html(data); $("#content").show(); $('th').click(function(){ var orderby = $(this).attr('id'); loaddata(orderby); }); } }); }
php:
$db = new mysqli(db_host, db_user, db_password, db_name); if (mysqli_connect_errno()) { printf("connect failed: %s", mysqli_connect_error()); exit; } if(isset($by) , $by==" asc"){ $by=" desc"; }else{ $by=" asc"; } if (isset($_post["orderby"])) { $orderby = $_post["orderby"]; //echo $orderby; } //build query on database $q = "select * uncompensated"; if (isset($_post["orderby"])) { $q .= " order " . $orderby; } else { $q .= ' order year2013'; } if (isset($by)) { $q .= $by; echo $q; } else { $by = ' desc'; $q .= $by; echo $q; } //echo $q; $result = $db->query($q); echo "<table cellspacing='0' >"; echo "<tr id='header'><th id='name'>name</th><th id='year2013'>2013</th><th id='year2012'>2012</th><th id='year2011'>2011</th><th id='year2010'>2010</th><th id='year2009'>2009</th><th id='year2008'>2008</th><th id='year2007'>2007</th><th id='year2006'>2006</th><th id='year2005'>2005</th><th id='year2004'>2004</th><th id='year2003note'>* 2003</th><th id='year2002'>2002</th><th id='year2001'>2001</th><th id='year2000'>2000</th><th id='year1999'>1999</th><th id='year1998'>1998</th><th id='year1997'>1997</th></tr>"; while($row = $result->fetch_array(mysqli_assoc)) { (code) }
Comments
Post a Comment