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>";
$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>";
}
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
Related
I want to update the percentage column based on the count data for all is_enabled = 1 rows in a table.
My coding attempt looks like this:
<?php
$total = '';
$result= mysqli_query($conn, "SELECT SUM(count) FROM My_Databse WHERE is_enabled ='1'");
while($row = mysqli_fetch_array($result)){
$total = $row['SUM(count)'];
}
$percentage = '';
$result= mysqli_query($conn, "SELECT * FROM My_Database WHERE is_enabled ='1' ORDER BY count DESC");
while($row = mysqli_fetch_array($result)){
$percentage = ($row[2] / $total) * 100;
echo '<div class="progress">';
echo '<div class="progress-bar" role="progressbar" aria-valuenow="'.$percentage.'" aria-valuemin="0" aria-valuemax="100" style="width:'.$percentage.'%">';
echo $row[1].'('.round($percentage).')';
echo '</div>';
echo '</div>';
$i++;
}
$result = mysqli_query($conn, "UPDATE My_Database SET percentage = ".$percentage." WHERE id = 1");
?>
I have now the problem, that I always get the last percentage. How can I update the percentage for every row?
Update query must be within while loop, after calculating percentage -
mysqli_query($conn, "UPDATE My_Database SET percentage = ".$percentage." WHERE id = ". $row['id'] );
I do not recommend making multiple trips to your database, nor using php for a task that can be simply, efficiently, directly, and completely done with a single query.
Join a derived table containing the count total for all qualifying rows, then build the arithmetic to calculate the percentage and update the rows accordingly.
It is more efficient to join the derived table versus calling the subquery for each qualifying row.
Code: (DB-Fiddle)
UPDATE my_table
JOIN (SELECT SUM(`count`) total FROM my_table WHERE is_enabled = 1) all_enabled
SET percentage = ROUND(`count` / total * 100)
WHERE is_enabled = 1;
New table data:
id
tutorial
count
is_enabled
percentage
1
House
3
1
6
2
Car
34
1
68
3
Tuna Fish
22
0
0
4
Bike
13
1
26
Depending on your circumstances (how often this table is read and written to), you might rather declare a TRIGGER to auto calculate&update the percentage column whenever count or is_enabled values are changed or a new row with is_enabled is INSERTed.
I have a problem, I'm trying to make a sum of a daily data from a specific table and column
<?php
$query = "SELECT (SELECT SUM(totaldeplata) FROM vanzari WHERE
MONTH(datainregistrarii) = MONTH(CURRENT_DATE())) + (SELECT SUM(totaldeplata)
FROM players WHERE MONTH(datainregistrarii) = MONTH(CURRENT_DATE())) as result";
$query_run = mysql_query($query);
$row = mysql_fetch_assoc($query_run);
$sum = $row['result'];
echo "sum of two different column from two tables : "+ $sum;
?>
This php query pick the monthly data and make a sum of it, it works, but I want to be able to make also a sum of daily data.
Any idea how?
I'm using this w3schools way to get data from the database:
$sql = "SELECT num_of_reservations FROM table WHERE date = '$date";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
echo $row["num_of_reservations"];
}
I have database which looks like this:
id - date - num_of_reservations - name
1 - 2017-02-02 - 3 - somebody
2 - 2017-02-02 - 5 - somebody
3 - 2017-02-02 - 7 - somebody
This works fine if I want to echo rows from the database, but now I need to make form which allows you to make reservation only if there is less than 15 reservations already at the same day. Problem in my current design is that every num_of_reservations row is going to different Array (because of while loop I quess) and I need to make them to + (3+5+7) so I can compare, is the num_of_reservations <=15 If it is I display ticket buying screen and if it's not I tell to pick another day.
You can use another query to get the sum of all reservations:
$sql = "SELECT SUM(num_of_reservations) AS sum_of_reservations FROM table WHERE date = '$date'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
if ($row['sum_of_reservations'] <= 15) {
//display ticket buying screen.
} else {
//tell to pick another day.
}
when the user inputs the form, I have know way of knowing what the user can input because there are hundreds of options in the database.
I am using a foreach loop to get the categories and the cost from the posted field that they chose example:
<td><?php foreach($pcategories as $value){echo "$value <br>";}?></td>
$qty $pcategories $cost $Rate
1 Bicycles 100 45
7 Auto Parts 200 60
5 Alarm Sys 300 35
The rate is then being selected from the query below
<?php $connection = mysqli_connect("localhost","root","","customs") or die("Error " . mysqli_error($connection));
$sql = "SELECT `categories`, `rate`, `elevy` FROM `lt_products` WHERE `categories` IN ('".implode("','",$pcategories)."')";
$result = mysqli_query($connection, $sql)or die(mysql_error());
while($row = mysqli_fetch_assoc($result)) {?>
<?php echo $row["rate"]; ?> <br>
<?php } ?>
Here's the problem the query is pulling the correct rates base on the categories the user in putted
but the rates are not coming out to match the order of the categories the user input.
Above rates should actually look like this below but instead that is what im getting above
$qty $pcategories $cost $Rate
1 Bicycles 100 35
7 Auto Parts 200 60
5 Alarm Sys 300 45
The select query seems to be coming out in alphabetical order based on the pcategories field in the table.
I dont know whats the best way to fix this so that the actual rate matches the categories the user chose.
Any help would be great thank you
Use the FIELD function to order the results based on their position in $pcategories
$sql = "SELECT `categories`, `rate`, `elevy`
FROM `lt_products`
WHERE `categories` IN ('".implode("','",$pcategories)."')
ORDER BY FIELD(categories, '".implode("','",$pcategories)."')";
im new on php programming and i've searched the function that i need but didn't found it.
here what exactly i want to do :
i want to select 2 columns from a table
set the order by descending by 1 column that is numeric
and then show in php the first 100 rows that were selected
Here is my code right now php shows all the columns i want it to show the first 100
$result = mysqli_query($con,"SELECT pvpkills,char_name FROM characters ORDER BY pvpkills DESC");
while($row = mysqli_fetch_array($result))
{
echo $row['pvpkills'] . "  " . $row['char_name'];
echo "<br>";
}
SELECT pvpkills,char_name FROM characters ORDER BY pvpkills DESC LIMIT 0,100