wright67uk Posted December 12, 2012 Share Posted December 12, 2012 I have a php file which grabs data from my MySQL db, and displays the results in a javascript graph. I'm using the $year variable in an sql query, and give my users an option to select a years worth of results at a time. When the user selects a different year, in the code below, my browser refreshes and a new sql query is run. Is it possible to use Ajax rather than php, to change the year? If so... How much code would I need to change? $year = (isset($_GET['year'])) ? (int)$_GET['year'] : date('Y');$year_links = '';$_GET['year'] = $year-1; $year_links .= "<a href='?" . http_build_query($_GET, '', '&') . "'>-</a> ";$year_links .= " $year ";$_GET['year'] = $year+1; $year_links .= " <a href='?" . http_build_query($_GET, '', '&') . "'>+</a>";[Code/] My full file... [Code] <!DOCTYPE HTML><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title> Ranges</title> <?php $connect_solning = mysql_connect($hostname_connect, $username_connect, $password_connect) or trigger_error(mysql_error(),E_USER_ERROR); @mysql_select_db($database_connect) or die (mysql_error()); $tbl_name = "snag";$user_id = $_POST["user_id"]; $year = (isset($_GET['year'])) ? (int)$_GET['year'] : date('Y');$year_links = '';$_GET['year'] = $year-1; $year_links .= "<a href='?" . http_build_query($_GET, '', '&') . "'>-</a> ";$year_links .= " $year ";$_GET['year'] = $year+1; $year_links .= " <a href='?" . http_build_query($_GET, '', '&') . "'>+</a>"; $sql = "SELECT user_id, fname, lname FROM registration"; $result = mysql_query($sql); $selected_id = isset($_POST['user_id']) ? intval($_POST['user_id']) : false; $who = ''; $options = ''; while ($row=mysql_fetch_array($result)) { if($selected_id==$row['user_id']) { $who = "{$row['fname']} {$row['lname']}"; $selected = ' selected="selected"'; } else { $selected = ''; } $options .= "<option value=\"{$row['user_id']}\"{$selected}>{$row['fname']} {$row['lname']}</option>\n"; } ?> <style type="text/css">a:link {text-decoration:none;}</style> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> <script type="text/javascript">$(function () { var chart; $(document).ready(function() { chart = new Highcharts.Chart({ chart: { renderTo: 'container', type: 'line', marginRight: 130, marginBottom: 25 }, title: { text: '<?php echo $year ?> Ranges by Club for <?php echo $who ?>', x: -20 //center }, subtitle: { text: 'Averages for month', x: -20 }, xAxis: { categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] }, yAxis: { title: { text: 'Score' }, plotLines: [{ value: 0, width: 1, color: '#808080' }] }, tooltip: { formatter: function() { return '<b>'+ this.series.name +'</b><br/>'+ this.x +': '+ this.y +''; } }, legend: { layout: 'vertical', align: 'right', verticalAlign: 'top', x: -10, y: 100, borderWidth: 0 }, series: [{ name: 'Driver', data: [<?php $sql= "SELECT month_table.month , IFNULL(AVG( yard ), 0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month = MONTH( STR_TO_DATE( sdate, '%d/%m/%y' ) ) AND club = 'Driver' AND user_id = $user_id AND YEAR = $year GROUP BY `month`"; $result=mysql_query($sql); while($row = mysql_fetch_array($result) ){ echo $row['avscore'].',';} ?>] }, { name: '3 Wood', data: [<?php $sql= "SELECT month_table.month , IFNULL(AVG( yard ), 0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month = MONTH( STR_TO_DATE( sdate, '%d/%m/%y' ) ) AND club = '3 wood' AND user_id = $user_id AND YEAR = $year GROUP BY `month`"; $result=mysql_query($sql); while($row = mysql_fetch_array($result) ){ echo $row['avscore'].',';} ?>] }, { name: '5 Wood', data: [<?php $sql= "SELECT month_table.month , IFNULL(AVG( yard ), 0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month = MONTH( STR_TO_DATE( sdate, '%d/%m/%y' ) ) AND club = '5 wood' AND user_id = $user_id AND YEAR = $year GROUP BY `month`"; $result=mysql_query($sql); while($row = mysql_fetch_array($result) ){ echo $row['avscore'].',';} ?>] /* SELECT yard FROM `snag` WHERE club = '5 wood' AND user_id = 1 */ }, { name: 'Hybrid', data: [<?php $sql= "SELECT month_table.month , IFNULL(AVG( yard ), 0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month = MONTH( STR_TO_DATE( sdate, '%d/%m/%y' ) ) AND club = 'Hybrid' AND user_id = $user_id AND YEAR = $year GROUP BY `month`"; $result=mysql_query($sql); while($row = mysql_fetch_array($result) ){ echo $row['avscore'].',';} ?>] }, { name: '3 iron', data:[<?php $sql= "SELECT month_table.month , IFNULL(AVG( yard ), 0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month = MONTH( STR_TO_DATE( sdate, '%d/%m/%y' ) ) AND club = '3 iron' AND user_id = $user_id AND YEAR = $year GROUP BY `month`"; $result=mysql_query($sql); while($row = mysql_fetch_array($result) ){ echo $row['avscore'].',';} ?>] }, { name: '4 iron', data: [<?php $sql= "SELECT month_table.month , IFNULL(AVG( yard ), 0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month = MONTH( STR_TO_DATE( sdate, '%d/%m/%y' ) ) AND club = '4 iron' AND user_id = $user_id AND YEAR = $year GROUP BY `month`"; $result=mysql_query($sql); while($row = mysql_fetch_array($result) ){ echo $row['avscore'].',';} ?>] }, { name: '5 iron', data: [<?php $sql= "SELECT month_table.month , IFNULL(AVG( yard ), 0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month = MONTH( STR_TO_DATE( sdate, '%d/%m/%y' ) ) AND club = '5 iron' AND user_id = $user_id AND YEAR = $year GROUP BY `month`"; $result=mysql_query($sql); while($row = mysql_fetch_array($result) ){ echo $row['avscore'].',';} ?>] }, { name: '6 iron', data: [<?php $sql= "SELECT month_table.month , IFNULL(AVG( yard ), 0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month = MONTH( STR_TO_DATE( sdate, '%d/%m/%y' ) ) AND club = '6 iron' AND user_id = $user_id AND YEAR = $year GROUP BY `month`"; $result=mysql_query($sql); while($row = mysql_fetch_array($result) ){ echo $row['avscore'].',';} ?>] }, { name: '7 iron', data: [<?php $sql= "SELECT month_table.month , IFNULL(AVG( yard ), 0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month = MONTH( STR_TO_DATE( sdate, '%d/%m/%y' ) ) AND club = '7 iron' AND user_id = $user_id AND YEAR = $year GROUP BY `month`"; $result=mysql_query($sql); while($row = mysql_fetch_array($result) ){ echo $row['avscore'].',';} ?>] }, { name: '8 iron', data: [<?php $sql= "SELECT month_table.month , IFNULL(AVG( yard ), 0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month = MONTH( STR_TO_DATE( sdate, '%d/%m/%y' ) ) AND club = '8 iron' AND user_id = $user_id AND YEAR = $year GROUP BY `month`"; $result=mysql_query($sql); while($row = mysql_fetch_array($result) ){ echo $row['avscore'].',';} ?>] }, { name: '9 iron', data: [<?php $sql= "SELECT month_table.month , IFNULL(AVG( yard ), 0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month = MONTH( STR_TO_DATE( sdate, '%d/%m/%y' ) ) AND club = '9 iron' AND user_id = $user_id AND YEAR = $year GROUP BY `month`"; $result=mysql_query($sql); while($row = mysql_fetch_array($result) ){ echo $row['avscore'].',';} ?>] }, { name: 'P Wedge', data: [<?php $sql= "SELECT month_table.month , IFNULL(AVG( yard ), 0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month = MONTH( STR_TO_DATE( sdate, '%d/%m/%y' ) ) AND club = 'P wedge' AND user_id = $user_id AND YEAR = $year GROUP BY `month`"; $result=mysql_query($sql); while($row = mysql_fetch_array($result) ){ echo $row['avscore'].',';} ?>] }, { name: 'S Wedge', data: [<?php $sql= "SELECT month_table.month , IFNULL(AVG( yard ), 0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month = MONTH( STR_TO_DATE( sdate, '%d/%m/%y' ) ) AND club = 'S wedge' AND user_id = $user_id AND YEAR = $year GROUP BY `month`"; $result=mysql_query($sql); while($row = mysql_fetch_array($result) ){ echo $row['avscore'].',';} ?>] }] }); }); }); </script> </head> <body><script src="../../js/highcharts.js"></script><script src="../../js/modules/exporting.js"></script> <form method="post" action="<?php echo $PHP_SELF;?>"><select name="user_id"> <option>Select User</option> <?php echo $options ?> </select> <input type="submit" value="Submit"> </form> <?php echo $year_links;?> <div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"> </div> </body></html>[code/] Link to comment https://forums.phpfreaks.com/topic/271922-how-can-i-best-adapt-a-php-file-to-prevent-page-refresh/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.