0

i put limit in this code but how can i calculate the totalmin in this page?

here is my code

<?php
$id = $_GET['id'];
$sql = "SELECT * FROM time WHERE id = $id ORDER BY id DESC LIMIT 15";
$result = mysql_query($sql);
?>

<table border="0" style="width:50%">
<tr>
    <th>Time in</th>
    <th>Time Out</th>
  </tr>
<?php

while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td><center>".$row['totalmin']."</center></td>";
echo "</tr>";
}
</table>

mysql_close();

i want to know how to total all the mins in 15 row with php code

user3793272
  • 111
  • 8
  • 1
    Store `$row['totalmin']` then add it, or do you just want the sum? You aren't closing that `while` loop in this code and `` will cause an error. http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_sum – chris85 Mar 17 '16 at 21:23
  • 1
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Mar 17 '16 at 21:23
  • 1
    [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Mar 17 '16 at 21:24
  • @JayBlanchard its not gonna use website im using for an assignment in school for now im considering mysql but later on ill study mysqli and PDO for the meantime i need to use mysql coz it was easiest way – user3793272 Mar 17 '16 at 21:26
  • @chris85 forgot to close the while sorry – user3793272 Mar 17 '16 at 21:28
  • 2
    I hate when people say *"I'm not that far along..."* or *"This site will not be public..."* or *"It's only for school, so security doesn't matter..."*. If teachers and professors are not talking about security from day one, they're doing it wrong. Challenge them. They're teaching sloppy and dangerous coding practices which students will have to unlearn later. I also hate it when folks say, *"I'll add security later..."*. If you don't have time to do it right the first time, when will you find the time to add it later? – Jay Blanchard Mar 17 '16 at 21:33

3 Answers3

0

You need to store $totalmin into your while loop and update it with new value if $row['totalmin'] is less then $totalmin.

Use something like this:

$totalmin = null;
while($row = mysql_fetch_array($result)) {
    if (is_null($totalmin) || $row['totalmin'] < $totalmin) {
        $totalmin = $row['totalmin'];
    }
    echo "<tr>";
    echo "<td><center>" . $totalmin . "</center></td>";
    echo "</tr>";
}    
alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
0

Sum up the minutes as you loop through the result:

$sum = 0;
while($row = mysql_fetch_array($result)){
  $sum += floatval($row['totalmin']); //or intval()
  echo "<tr>";
  echo "<td><center>".$row['totalmin']."</center></td>";
  echo "</tr>";
}
echo $sum;

make sure you close your while loop

Dan
  • 10,614
  • 5
  • 24
  • 35
0

Something like this may do the trick, if you want to show sum only But you should NOT use mysql_ functions, and this code is not sequre from SQL Injects

    $id = $_GET['id'];
    $sql = "SELECT * FROM time WHERE id = $id ORDER BY id DESC LIMIT 15";
    $result = mysql_query($sql);
    $sum = 0;
    $num = 0;
    ?>

    <table border="0" style="width:50%">
    <tr>
        <th>Time in</th>
        <th>Time Out</th>
      </tr>
    <?php

    while($row = mysql_fetch_array($result)){
        $num++;
        $sum += $row['totalmin'];
        echo "<tr>";
        echo "<td><center>".$row['totalmin']."</center></td>";
        echo "</tr>";
        if($num == 15){
            echo "<tr>";
            echo "<td><center>".$sum."</center></td>";
            echo "</tr>";
        }
    }
    ?>
</table>

<?php 
mysql_close();
Nutic
  • 1,407
  • 4
  • 16
  • 31