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)."')";
Related
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
So this is the structure of my MySQL table that I wanna work this out with:
ID type category_id amount
12 Expense 3 963.39
13 Expense 5 1200.50
14 Expense 3 444.12
15 Expense 5 1137.56
..............................
Desired output:
1407,41 (for category_id = 3)
2338,06 (for category_id = 5)
....... (and for other category_id)
What I get now:
1407,41 (only for category_id = 3)
My query does not add or display the sum of other category_id.
This is the query I am trying:
$query = "SELECT SUM(amount) AS TotalAmount FROM spendee WHERE type = 'Expense'
group by category_id having count(*) >1 ";
$expense_query = mysqli_query($connection, $query);
$expense_count = mysqli_fetch_array($expense_query);
echo $expense_count[0];
Been stuck with this for the last couple of days. Any help is very much appreciated. Thank you!
You're only calling mysqli_fetch_array() once. You need to call it in a loop to get all the totals. You should also include the category ID in the SELECT list.
$query = "SELECT category_id, SUM(amount) AS TotalAmount FROM spendee WHERE type = 'Expense'
group by category_id having count(*) >1 ";
$expense_query = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($expense_query)) {
echo "{$row['TotalAmount']} (for category_id = {$row['category_id']}<br>\n";
}
The query here works. It's just that you only select the first result from the $expense_count variable. $expense_count[1] will return the second category listed, $expense_count[2 will return the third one, ect...
Try echo implode(" <br>", $expense_count);
Have a nice day.
I have a database with the follow tables:
sales
expenses
taxes
earnings
When I sell some product it adds a item to sales, the same to expenses while I add expenses, taxes are added automaticly when selling and earnings too. They are on the same database but different tables.
I need to sum those fields together. I do it one by one without problems like this:
<?php
$query = mysqli_query($db, "SELECT SUM(total) AS `total` FROM `sales`");
while($result = mysqli_fetch_assoc($query)){
echo number_format($result['total'],0,',','.');}
?>
and
<?php
$query2 = mysqli_query($db, "SELECT SUM(expense) AS `expense` FROM `expenses`");
while($result2 = mysqli_fetch_assoc($query2)){
echo number_format($result2['expense'],0,',','.');}
?>
How do I sum those two and echo a result example:
sales - expense = value ?
<?php
$query = mysqli_query($db, "SELECT SUM(total) AS `total` FROM `sales`");
$query2 = mysqli_query($db, "SELECT SUM(expense) AS `expense` FROM `expenses`");
$total_sales = 0;
$total_expenses = 0;
while($result = mysqli_fetch_assoc($query)){
$total_sales = $total_sales + $result['total'];
echo number_format($result['total'],0,',','.');
}
while($result2 = mysqli_fetch_assoc($query2)){
$total_expenses = $total_expenses + $result['total'];
echo number_format($result2['expense'],0,',','.');
}
?>
The sum would be $total_sales-$total_expenses.
Are you sure you will sum those two together?
There are at least good points you do those things separation.
Firstly, the logic is very clearly.
Secondly, if you have many data like those, when you sum them separately, those will not influence each other. Because when you select data and do some operation, if the data is not huge, it will be ok. But if it is huge data, both your database and your cpu and memory will have big pressure.
Thirdly, when you select in database, it is not suggested doing operations such as sum, sub, division and multiplication, although database can do it. Because these operations are really wasting performance of database and computer.
Anyway, you still want to do that. Then you can try to left joint or right joint your table. You can use some foreign keys to connect these two tables and then you can do things which you wanna to do.
Thanks in advance for any time you spend on my question.
I am trying to display data in a way that will display the manufacturer as a name instead of a number.
Basically when they store the data they choose a manufacturer from a drop down which is generated from a table.. IE Trogues = 1 so products stores the #1 so I know that any beer is associated with trogues is 1. Now I want to display the data but instead of having a 1 I would like to have Trogues be displayed. Where you see manufacturer in the echo code below..
I am not understanding the process logic here..
error_reporting(E_ALL);
ini_set('display_errors', 1);
$sql = "SELECT * FROM products
LEFT JOIN manufacturer
ON product.manufacturer = manufacturer.id
ORDER BY manufacturer.id, product.id";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
echo "
<div class=reportclientproduct>".$row['manufacturer']." - <a href=".$row['website']." target=_blank>".$row['product']."</a></div>";
}
Have you tried the query like this:
$sql = "SELECT man.id AS manufac, products.product AS prod FROM products
LEFT JOIN manufacturer as man
ON product.manufacturer = manufacturer.id
ORDER BY manufacturer.id, product.id";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
echo "
".$row['manufac']." - ".$row['prod']."
";
}
Assuming that the table products had a column named manufacturer which holds the ID of the manufacturer, and that both tables have columns name ID which hold the ID of the table item.
Also the JOIN functions may vary based on the database you use. But the aforementioned method is for mysql.
Can someone help me with this:
I have a mySQL database and I would like to do a simple search amoung the items, here is an example of my database named "orders"
id OrderDate OrderPrice Customer
1 2008/11/12 1000 Hansen
2 2008/10/23 1600 Nilsen
3 2008/09/02 700 Hansen
4 2008/09/03 300 Hansen
5 2008/08/30 2000 Jensen
6 2008/10/04 100 Nilsen
How can I group by duplicated data and display it like this:
<select name="costumer">
<option>Hansen</option>
<option>Nilsen</option>
<option>Jensen</option>
</select>
I know it is the GROUP BY command, but dont know how to display it on a PHP script file
Try this:
$dbconn = new mysqli();
$dbconn->connect("localhost","root","","test");
if($dbconn->connect_errno ){
echo "Connection Failed";
}
$query = "SELECT DISTINCT Customer FROM `Orders`";
$result = $dbconn->query($query);
echo "<select name=\"costumer\">";
while($row = $result->fetch_array())
{
echo "<option>".$row["Customer"]."</option>";
}
echo "</select>";
you can use DISTINCT on this since you only need to have 1 column,
SELECT DISTINCT Customer FROM `Orders`