Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/161535-solved-logic-problem/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/161535-solved-logic-problem/#findComment-852450
Share on other sites

<?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;
?>

Link to comment
https://forums.phpfreaks.com/topic/161535-solved-logic-problem/#findComment-852459
Share on other sites

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;

?>

 

Link to comment
https://forums.phpfreaks.com/topic/161535-solved-logic-problem/#findComment-852462
Share on other sites

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;

?>

Link to comment
https://forums.phpfreaks.com/topic/161535-solved-logic-problem/#findComment-852473
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.