will35010 Posted June 9, 2009 Share Posted June 9, 2009 I'm trying to write a script to calculate miles-per-gallon. Seems easy enough...lol. I have this code so far and I'm having trouble calling the results from the query. I want it to run the query and put the results into an array for calculating the mpg of a specific vehicle. Any direction would be helpful. Thanks. <?php //script to calculate fuel mileage //retrieve value from web form $vin = $_GET['vin']; // formula to calculate mpg // mpg = miles / gallons //db connection info require('../config.php'); require('../opendb.php'); //Query to pull fuel data $sql = "SELECT mileage, gallons FROM fuel WHERE vin = '$vin' ORDER BY `date` DESC LIMIT 2"; //send query to database mysqli_query($conn, $sql) or die('Error: ' . mysql_error()); $data = mysqli_fetch_array($sql); echo $data[mileage]; ?> When the query is ran it displays this: mileage gallons 100050 14.26 100003 14.26 Quote Link to comment https://forums.phpfreaks.com/topic/161535-solved-logic-problem/ Share on other sites More sharing options...
RussellReal Posted June 9, 2009 Share Posted June 9, 2009 first question, is mileage only increasing? or could some be like 95 100 92 85 and does gallons represent the gallons left in the tank, or the gallons used? Quote Link to comment https://forums.phpfreaks.com/topic/161535-solved-logic-problem/#findComment-852445 Share on other sites More sharing options...
will35010 Posted June 9, 2009 Author Share Posted June 9, 2009 first question, is mileage only increasing? or could some be like 95 100 92 85 and does gallons represent the gallons left in the tank, or the gallons used? It should only increase. The gallons represent the fuel added to the tank at time of fueling. I think my problem is in sorting the data from the query. If I could figure out how to sort it, I could then assign variables and do the math. Quote Link to comment https://forums.phpfreaks.com/topic/161535-solved-logic-problem/#findComment-852450 Share on other sites More sharing options...
RussellReal Posted June 9, 2009 Share Posted June 9, 2009 ok so from 100003 to 100050 you used 14.26 gallons of gas, is what thats saying? Quote Link to comment https://forums.phpfreaks.com/topic/161535-solved-logic-problem/#findComment-852453 Share on other sites More sharing options...
will35010 Posted June 9, 2009 Author Share Posted June 9, 2009 ok so from 100003 to 100050 you used 14.26 gallons of gas, is what thats saying? Correct. Quote Link to comment https://forums.phpfreaks.com/topic/161535-solved-logic-problem/#findComment-852454 Share on other sites More sharing options...
RussellReal Posted June 9, 2009 Share Posted June 9, 2009 <?php $miles = ($gallons = array()); while ($r = mysql_fetch_assoc()) { $miles[] = $r['mileagle']; $gallons[] = $r['gallons']; } $mileGauge = array_pop($miles) - $miles[0]; array_shift($gallons); $tg = 0; foreach ($gallons as $v) { $tg += $v; } $mpg = $mileGauge / $tg; ?> Quote Link to comment https://forums.phpfreaks.com/topic/161535-solved-logic-problem/#findComment-852459 Share on other sites More sharing options...
will35010 Posted June 9, 2009 Author Share Posted June 9, 2009 So my code should be this? It gave me a blank result. <?php //script to calculate fuel mileage //retrieve value from web form $vin = $_GET['vin']; //db connection info require('../config.php'); require('../opendb.php'); //Query to pull fuel data $sql = "SELECT mileage, gallons FROM fuel WHERE vin = '$vin' ORDER BY `date` DESC LIMIT 2"; //send query to database mysqli_query($conn, $sql) or die('Error: ' . mysql_error()); $miles = ($gallons = array()); while ($r = mysql_fetch_assoc()) { $miles[] = $r['mileage']; $gallons[] = $r['gallons']; } $mileGauge = array_pop($miles) - $miles[0]; array_shift($gallons); $tg = 0; foreach ($gallons as $v) { $tg += $v; } $mpg = $mileGauge / $tg; echo $mpg; ?> Quote Link to comment https://forums.phpfreaks.com/topic/161535-solved-logic-problem/#findComment-852462 Share on other sites More sharing options...
RussellReal Posted June 9, 2009 Share Posted June 9, 2009 I'm not sure you're pulling the data correctly, I did a quick check and 'mysqli_query' doesn't exist Quote Link to comment https://forums.phpfreaks.com/topic/161535-solved-logic-problem/#findComment-852466 Share on other sites More sharing options...
RussellReal Posted June 9, 2009 Share Posted June 9, 2009 sorry for double post.. if you want to add me to MSN add me, I hafta sleep. sorry RussellonMSN@hotmail.com Quote Link to comment https://forums.phpfreaks.com/topic/161535-solved-logic-problem/#findComment-852468 Share on other sites More sharing options...
will35010 Posted June 9, 2009 Author Share Posted June 9, 2009 I'm not sure you're pulling the data correctly, I did a quick check and 'mysqli_query' doesn't exist It does. http://us2.php.net/manual/en/mysqli.query.php Quote Link to comment https://forums.phpfreaks.com/topic/161535-solved-logic-problem/#findComment-852471 Share on other sites More sharing options...
will35010 Posted June 9, 2009 Author Share Posted June 9, 2009 sorry for double post.. if you want to add me to MSN add me, I hafta sleep. sorry RussellonMSN@hotmail.com I got it working like this. Thanks for all your help!!! <?php //script to calculate fuel mileage //retrieve value from web form $vin = $_GET['vin']; //db connection info require('../config.php'); require('../opendb.php'); //Query to pull fuel data $sql = "SELECT mileage, gallons FROM fuel WHERE vin = '$vin' ORDER BY `date` DESC LIMIT 2"; //send query to database $result = mysqli_query($conn, $sql) or die('Error: ' . mysql_error()); $miles = ($gallons = array()); while ($r = mysqli_fetch_assoc($result)) { $miles[] = $r['mileage']; $gallons[] = $r['gallons']; } $mileGauge = array_pop($miles) - $miles[0]; array_shift($gallons); $tg = 0; foreach ($gallons as $v) { $tg += $v; } $mpg = $mileGauge / $tg; echo $mpg; ?> Quote Link to comment https://forums.phpfreaks.com/topic/161535-solved-logic-problem/#findComment-852473 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.