Jump to content

will35010

Members
  • Posts

    276
  • Joined

  • Last visited

Posts posted by will35010

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

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

     

  3. 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.

  4. 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

     

  5. Assuming 'date' is the date when you inserted the record, you can use that to order.

     

    ORDER BY `date` DESC LIMIT 2

     

    I thought about that but I thought I would have to do that from the php side and was trying to avoid that. I didn't know you could do it from the mysql side. Thanks!!!

  6. When you say just the last two records you mean for example you have 10 rows in the database and you want to retrieve miles and gallons for entry 9 and 10?

     

    Correct. I just want to make sure that it will actually be the last two records. For instance if record 2 was edited, would it's number now become 10 since it was the last one edited? I only want to retrieve the last two records entered.

  7. I have a table to store fuel info. I want to retrieve the last two records entered to calculate miles-per-gallon.

     

    Here's my table:

    -- Table structure for table `fuel`
    --
    
    CREATE TABLE IF NOT EXISTS `fuel` (
      `vin` varchar(20) NOT NULL,
      `mileage` mediumint(9) NOT NULL,
      `gallons` float NOT NULL,
      `total` float NOT NULL,
      `date` date NOT NULL,
      `driver` varchar(20) NOT NULL
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
    

     

    I have this query but I don't know how to retrieve just the last two records. Or is there a better way to calculate it?

     

    SELECT mileage, gallons FROM fuel WHERE vin = '$vin'
    

  8. 1. You could have use a JOIN and use one SQL to get everything.

    2. In your second function var_dump $row['mileage']. What does that output?

     

    Thanks for the help. The problem was with my require('lib/functions.php'). It was declaring a variable twice and causing a fatal error.  ;D

     

  9. I'm having a problem calling the second function from the first function. I don't know where the problem is. Any direction would be helpful. Thanks.

     

    function vehicleinfo() {
    require('lib/opendb.php');
    require('lib/functions.php');
    $query = mysqli_query($conn, "SELECT vin, plate, expire, year, make, model FROM vehicles");
    while($row = mysqli_fetch_array($query)) {
    echo "<table width='900' border='1' align='center'>
            <tr>
              <td width='225' align='center' scope='col'>VIN: " . $row['vin'] . "</td>
              <td width='228' align='center' scope='col'>YEAR: " . $row['year'] . "</td>
              <td width='225' align='center' scope='col'>MAKE: " . $row['make'] . "</td>
            </tr>
            <tr>
              <td><div align='center'>PLATE: " . $row['plate'] . "</div></td>
              <td><div align='center'>EXPIRE: " . $row['expire'] . "</div></td>
              <td><div align='center'>MODEL: " . $row['model'] . "</div></td>
            </tr>
            <tr>
              <td><div align='center'>". currentMileage($row['vin']) ."</div></td>
              <td><div align='center'>miles until oil change</div></td>
              <td><div align='center'>Get MPG</div></td>
            </tr>
          </table>
      </tr>
    </table>";
    }	
    }
    
    function currentMileage($vin) {
    require('lib/opendb.php');
    $query = mysqli_query($conn, "SELECT mileage FROM cmileage WHERE vin = ".$vin."");
    $row = mysqli_fetch_array($query);
    echo $row['mileage'];
    }
    

  10. This works. I just thought there might be a better practice.

     

    <?php
    
    $vin = "11111111111111112";
    
    require('lib/opendb.php');
    $query = mysqli_query($conn, "SELECT mileage FROM cmileage WHERE vin = ".$vin."");
    $row = mysqli_fetch_array($query);
    echo $row['mileage'];
    
    ?>
    

  11. The query works CLI so I know it's my php that's bad. I'm trying to retrieve a result from the database and echo it. I normally use mysqli_fetch_array, but this is only one field so it would be overkill. What function do I use to display only one result?

     

    <?php
    
    $vin = "11111111111111112";
    
    require('lib/opendb.php');
    $query = mysqli_query($conn, "SELECT mileage FROM cmileage WHERE vin = ".$vin."");
    $row = mysqli_fetch_object($query);
    echo $row;
    
    ?>
    

  12. I replaced the html double quotes with single quotes, but I'm still getting an error.

    function vehicleinfo() {
    require('opendb.php');
    $query = mysqli_query($conn, "SELECT vin, plate, expire, year, make, model FROM vehicles");
    while($row = mysqli_fetch_array($query)) {
    echo "<table width='700' border='1' align='center'>
            <tr>
              <th width='225' scope='col'>" . $row['vin']"</th>
              <th width='228' scope='col'>" . $row['year']"</th>
              <th width='225' scope='col'>" . $row['make']"</th>
            </tr>
            <tr>
              <td><div align='center'>" . $row['plate']"</div></td>
              <td><div align='center'>" . $row['expire']"</div></td>
              <td><div align='center'>" . $row['model']"</div></td>
            </tr>
            <tr>
              <td><div align='center'>current mileage</div></td>
              <td><div align='center'>miles until oil change</div></td>
              <td><div align='center'>mpg</div></td>
            </tr>
          </table>
        <p> </p></th>
      </tr>
    </table>";
    }	
    }
    

  13. I'm trying to echo out a table. I think think my problem is with escaping between the html and php. Any help would be appreciated. Thanks!

     

    function vehicleinfo() {
    require('opendb.php');
    $query = mysqli_query($conn, "SELECT vin, plate, expire, year, make, model FROM vehicles");
    while($row = mysqli_fetch_array($query)) {
    echo "<table width="700" border="1" align="center">
            <tr>
              <th width="225" scope="col">" . $row['vin']"</th>
              <th width="228" scope="col">" . $row['year']"</th>
              <th width="225" scope="col">" . $row['make']"</th>
            </tr>
            <tr>
              <td><div align="center">" . $row['plate']"</div></td>
              <td><div align="center">" . $row['expire']"</div></td>
              <td><div align="center">" . $row['model']"</div></td>
            </tr>
            <tr>
              <td><div align="center">current mileage</div></td>
              <td><div align="center">miles until oil change</div></td>
              <td><div align="center">mpg</div></td>
            </tr>
          </table>
        <p> </p></th>
      </tr>
    </table>";
    }	
    }
    

  14. I have two php functions that generate options for select menus. The menu displays properly, but the value isn't being passed.

     

    The value is supposed to be $row['dl']

    function getDrivers() {
    require('opendb.php');
    $result = mysqli_query($conn, "SELECT fname, lname, dl FROM drivers");
    
    while($row = mysqli_fetch_array($result))
    {
      echo "<option value=\"" . $row['dl'] . "\">".$row['lname'].",".$row['fname']."</option>";
    }
    }
    

    The value is supposed to be $row1['vin']

    function getVehicles() {
    require('opendb.php');
    $result1 = mysqli_query($conn, "SELECT vin, plate, year FROM vehicles");
    while($row1 = mysqli_fetch_array($result1)) {
    echo "<option value=\"" . $row1['vin'] . "\">".$row1['plate'].",".$row1['year']."</option>";
    }
    }
    

×
×
  • 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.