0

Maybe this question is so basic i cant find it explained.

I have a table with 2 values . instructions | valuations which echos from mysql. I was looking to get the %(percent) value of instructions to valuations. i.e. 10 appointments / 5 sales = 50% conversion rate. the sum would be Sales / Appoints x 100 = %

Any idea how i would echo this into 'percentage' column on my html table?

$query = "SELECT * FROM office_figures2016";
$result = mysqli_query($conn, $query);

while($office_figures2016=mysqli_fetch_assoc($result)){

echo "<tr>";
        //changes date to english format from a time stamp
        echo"<td>".$office_figures2016['id_why']."</td>";

        echo"<td>".date('d-m-Y',strtotime($office_figures2016['date_figures']))."</td>";
        echo"<td>".$office_figures2016['valuations']."</td>";
        echo"<td>".$office_figures2016['instructions']."</td>";
        echo"<td>".$office_figures2016['percentage of above']."</td>";
Glen
  • 39
  • 1
  • 2
  • 14
  • 1
    Possible duplicate of [MySQL Calculate Percentage](https://stackoverflow.com/questions/15746749/mysql-calculate-percentage) – S4NDM4N Aug 09 '17 at 10:08
  • You have everything you need, you know what the calculation is, so what on earth is the problem – RiggsFolly Aug 09 '17 at 10:10
  • I think the answer you're looking for is [here](https://stackoverflow.com/questions/15746749/mysql-calculate-percentage) – S4NDM4N Aug 09 '17 at 10:11
  • correct syntax to echo it. – Glen Aug 09 '17 at 10:11
  • 1
    `$p = ($office_figures2016['valuations']/$office_figures2016['instructions'])*100; echo "$p";` – RiggsFolly Aug 09 '17 at 10:15
  • thanks rigs,,,i think i have code blindness...how could you edit that into my original post?? sorry – Glen Aug 09 '17 at 10:19
  • i have sorted it thanks for support . maybe worth keeping on here in case it gets picked up on a search... i couldnt find the answer on here before you linked it and i often find answers but have no clue how to add into my code – Glen Aug 09 '17 at 10:37

2 Answers2

1
 $query= "SELECT *, 
        concat(round(( instructions/valuations * 100 ),2),'%') AS percentage
    FROM office_figures2016";
    $result = mysqli_query($conn, $query);
        while($office_figures2016=mysqli_fetch_assoc($result)){

        //echo "$applicant_card[id].$applicant_card[first_name]";   //this echos  out a few columns = id and first_name


        echo "<tr>";
        //changes date to english format from a time stamp
        echo"<td>".$office_figures2016['id_why']."</td>";

        echo"<td>".date('d-m-Y',strtotime($office_figures2016['date_figures']))."</td>";
        echo"<td>".$office_figures2016['valuations']."</td>";
        echo"<td>".$office_figures2016['instructions']."</td>";
        echo"<td>".$office_figures2016['percentage']."</td>";
        echo"<td>".$office_figures2016['firsts']."</td>";


        echo "<td><a href='edit_figures.php?edit=$office_figures2016[id_why]'>Edit</a></td>";
        //echo "<td><a href='delete_applicant.php?id=$office_figures2016[id]'>x</a></td></tr>";

      }
Glen
  • 39
  • 1
  • 2
  • 14
0

You shouldn't create such a column - it is bad habit to have duplicate values in database. It is simple value, so you can calculate it each time, for example:

SELECT a.a, a.b, (a/b) FROM a ORDER BY (a/b);

If you really want to add it, you can simply create column in phpMyAdmin and then update it width sql

UPDATE A SET a_b=a/b
b19
  • 11
  • 1