I have this problem, first am getting the total students and then the total amount paid by students in column amount_paid and I am querying all the students id(system_id) and then the payments each made and then the invoices and the cost fee of the invoices target to the id’s matching the student.
And then at last am trying to subtract the total amount paid to all the invoices sent to a selected student in a loop and then subtract it from the fees sent to targeted students in that same loop. Now my many issue is for the first students it’s calculationg the balance fine but with the rest the balances are rapidly increasing. Please where is the issue with my code:
<?php
$students = mysqli_query($conn, "SELECT * FROM students GROUP BY system_id");
while($row = mysqli_fetch_assoc($students)){
$user_uid = $row['system_id'];
$exam = $row['exam_number'];
//getting payments balances
//gettingshopping cart details
$Balance_query = mysqli_query($conn, "SELECT SUM(amount_paid) AS 'sumitem_cost' FROM payments WHERE payment_by='$user_uid' ");
$balance_data = mysqli_fetch_array($Balance_query);
$balance_price = $balance_data['sumitem_cost'];
$py = mysqli_query($conn, "SELECT * FROM payments WHERE payment_by='$user_uid' AND status!='rejected' GROUP BY invoice_id");
while($rowpy = mysqli_fetch_assoc($py)){
$paidAmout = $rowpy['amount'];
$invoiceId = mysqli_real_escape_string($conn, $rowpy['invoice_id']);
$PaymentStatus = mysqli_real_escape_string($conn, $rowpy['status']);
//Getting invoice
$Invoice = mysqli_query($conn, "SELECT * FROM invoices WHERE id='$invoiceId'");
while($rowInv = mysqli_fetch_assoc($Invoice)){
$NewFeeId = $rowInv['id'];
$sql = mysqli_query($conn,"SELECT SUM(fee) as total FROM invoices WHERE id='$invoiceId'");
$row = mysqli_fetch_array($sql);
$sum = $row['total'];
$total_price += $row[‘fee’];
}}
echo'<br>'.$exam.':' . $BalanceToPay = $total_price - $balance_price;
}
?>
You have some weird queries. Single quotes should never be used for column aliases and why do you group by system_id?
Your calculations would be greatly simplified by using JOINs and if I were your teacher I'd expect to see joins.
This will solve the assignment at once:
SELECT s.system_id
, sum(amount_paid) as sumitem_cost
, sum(fee) as total
FROM students s
JOIN payments p on s.system_id = p.payment_by
JOIN invoices i on p.invoice_id = i.id
You need to zero (init) $total_price every time you deal with new student. You do not do that explicitely, so on first use of $total_price it's created zeroed, but then you iterate further and keep adding to it, ending up with cumulative value. So simply add
$total_price = 0;
for each while loop iteration:
while($row = mysqli_fetch_assoc($students)){
$total_price = 0;
...
you can try this code
<?php
$students = mysqli_query($conn, "SELECT * FROM students GROUP BY system_id");
$grandBalanceForAllStudent = 0 ;
while($row = mysqli_fetch_assoc($students)){
$user_uid = $row['system_id'];
$exam = $row['exam_number'];
$total_price = 0 ;
//getting payments balances
//gettingshopping cart details
$Balance_query = mysqli_query($conn, "SELECT SUM(amount_paid) AS 'sumitem_cost' FROM payments WHERE payment_by='$user_uid' ");
$balance_data = mysqli_fetch_array($Balance_query);
$balance_price = $balance_data['sumitem_cost'];
$py = mysqli_query($conn, "SELECT * FROM payments WHERE payment_by='$user_uid' AND status!='rejected' GROUP BY invoice_id");
while($rowpy = mysqli_fetch_assoc($py)){
$paidAmout = $rowpy['amount'];
$invoiceId = mysqli_real_escape_string($conn, $rowpy['invoice_id']);
$PaymentStatus = mysqli_real_escape_string($conn, $rowpy['status']);
//Getting invoice
$Invoice = mysqli_query($conn, "SELECT * FROM invoices WHERE id='$invoiceId'");
while($rowInv = mysqli_fetch_assoc($Invoice)){
$NewFeeId = $rowInv['id'];
$sql = mysqli_query($conn,"SELECT SUM(fee) as total FROM invoices WHERE id='$invoiceId'");
$row = mysqli_fetch_array($sql);
$sum = $row['total'];
$total_price += $row[‘fee’];
}}
$BalanceToPay = $total_price - $balance_price;
// you can keep for your data
$grandBalanceForAllStudent += $BalanceToPay ;
echo'<br>'.$exam.':' . $BalanceToPay ;
}
?>
Related
good morning. I need to create a query that joins me 4 tables: recipes, food_recipes, prices_food AND users
tables
Currently what I do is:
search for recipes together with their user
inside I look for information on their food
and inside, in each iteration, I look for the information on the price of the food that the user entered
If the user did not enter any price, I look for the average of all the prices of people in the same country on certain dates
If the number of people who entered that price does not exceed the minimum, then the total price of the prescription will be 0
and it works very well. But I need everything to be in the same query to be able to filter for the price.
Try to create the query, with inner join and left join, but either way I don't get the result I need. Could you give me some guidance on what the correct syntax would be like? please
$wsqli="SELECT * from recipes
INNER JOIN users
on recipes.id_user = users.id_user
where $where
order by recipes.date desc
limit $limit
OFFSET $offset ";
$result = $linki->query($wsqli);
if($linki->errno) die($linki->error);
while($row = $result->fetch_array()){
$precio_receta = recipe_price($id_recipe);
}
function recipe_price($id_recipe)
{
$current_date = date('Y/m/d', strtotime('+1 day'));
$previous_date = date('Y/m/d', strtotime('-8 month'));
$price_recipe = 0;
$wsqli="SELECT * from recipes_foods
where id_recipe='$id_recipe' ";
$result = $linki->query($wsqli);
if($linki->errno) die($linki->error);
while($row = $result->fetch_array()){
$weight = $row['weight'];
$fdcId = $row['fdcId'];
$wsqli="SELECT * from foods_prices
where fdcId='$fdcId' and id_user='$id_user_session' and (foods_prices.date between '$previous_date' and '$current_date')";
$result2 = $linki->query($wsqli);
if($linki->errno) die($linki->error);
if($row = $result2->fetch_array()){
$price_recipe = $price_recipe + (($row['price'] / 1000) * $weight);
}else{
$wsqli="SELECT
COUNT(*) as quantity_prices,
AVG(price) as average_price
from foods_prices
inner join users
on users.id_user = foods_prices.id_user
where fdcId='$fdcId'
and country='$user_country'
and (foods_prices.date between '$previous_date' and '$current_date')";
$result2 = $linki->query($wsqli);
if($linki->errno) die($linki->error);
if($row = $result2->fetch_array()){
if ($row['quantity_prices'] > 50) {
$price_recipe = $price_recipe + (($row['average_price'] / 1000) * $weight);
}else{
$price_recipe = 0;
return $price_recipe;
break;
}
}else{
$price_recipe = 0;
return $price_recipe;
break;
}
}
}
return $price_recipe;
}
I have already managed to gather all the data in a single query.
$wsqli="SELECT *
from recipes
left JOIN recipes_foods
on recipes_foods.id_recipe = recipes.id_recipe
left JOIN foods_prices
on foods_prices.fdcId = recipes_foods.fdcId
left JOIN users
on users.id_user = foods_prices.id_user
where country='AR'
order by recipes.date desc";
$result = $linki->query($wsqli);
if($linki->errno) die($linki->error);
while($row = $result->fetch_array()){
var_dump($row);
}
but I still can't:
know if the quantity of prices entered for that food is greater than or equal to that required (min. 50). Try "where COUNT (price)> 50" but it says "Invalid use of group function"
If the quantity of prices IS LESS than required, stop last in the results (or do not show)
If the quantity of prices is greater than required .... Order by price. Adding "order by COUNT (price) desc" reduces the results obtaining only the first one.
I want in this code please to increase the quantity of the same item when inserting
without adding a new record to the table.
the insert working is done, but please I need when inserting to the table that
contains a unique number of the invoice to increase the number of items without
duplicate records.
<?php
session_start();
include('../connect.php');
$a = $_POST['invoice'];
$b = $_POST['product_id'];
$c = $_POST['qty'];
$saleproduct = $_POST['saleproduct'];
$w = $_POST['pt'];
$amount = $_POST['amount'];
$dateok = $_POST['dateok'];
$result = $db->prepare("SELECT * FROM products WHERE product_id= :userid");
$result->bindParam(':userid', $b);
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
$sellprice=$row['price'];
$prodcode=$row['prod_code'];
$code=$row['product_code'];
$color=$row['color_name'];
$size=$row['size_name'];
$sizenum=$row['note'];
$name=$row['product_name'];
}
$sql = "UPDATE products
SET qty=qty-?
WHERE product_id=?";
$q = $db->prepare($sql);
$q->execute(array($c,$b));
$grandtotal = $sellprice- $saleproduct ;
$d=$grandtotal*$c;
$sql = "INSERT INTO sales_order
(invoice_number,prod_code,product_id,qty,amount,saleproduct,name,color_name,size_name,note,price,product_code,date,dateok) VALUES
(:a,:prodcode,:b,:c,:d,:saleproduct,:e,:ee,:eee,:sizenum,:sellp,:i,:k,:kok)";
$q = $db->prepare($sql);
$q->execute(array(':a'=>$a,':prodcode'=>$prodcode,':b'=>$b,':c'=>$c,':d'=>$d,':saleproduct'=>$saleproduct,':e'=>$name,':ee'=>$color,':eee'=>$size,':sizenum'=>$sizenum,':sellp'=>$sellprice,':i'=>$code,':k'=>$date,':kok'=>$dateok));
?>
You'll need to run UPDATE query to update quantity of the existing record i.e.:
UPDATE `sales_order` SET `qty` = `qty` + 1 WHERE `id` = ?
You will obviously have to bind id of the desired record within sales_order table.
<?php
require_once "config.php";
$sql = "SELECT * FROM charges";
$results = mysqli_query($link, $sql);
$row = mysqli_fetch_array($results);
$count = mysqli_num_rows($results);
for ($i=0; $i <=$count ; $i++) {
$id = $row['c_id'];
$newBalance = $row['charge']+$row['balance'];
$query = "UPDATE charges SET balance = $newBalance WHERE c_id = $id";
mysqli_query($link, $query)
}
?>
that's my php code
and the below pic is my database tables
database table
Your query is:
UPDATE charges SET balance = balance + charge
With php it is:
$sql = "UPDATE charges SET balance = balance + charge";
$results = mysqli_query($link, $sql);
enter image description here*now how can i calculate the sub total of the (tolal) field values these values are not from database, these are from (price of item * quantity)*
This is my code:
<?php
$ipAdd = getRealIpAddr();
$checkPrice= "select * from cart where ipAdd= '$ipAdd' ";
$run= mysqli_query($conn,$checkPrice);
while($record= mysqli_fetch_array($run)){
$proId= $record['pId'];
$cId= $record['cId'];
$cQuant= $record['qnty'];
$proPric= "select * from products where prodId= '$proId' ";
$runPrice=mysqli_query($conn, $proPric);
while($pPrice=mysqli_fetch_array($runPrice)){
$proPri= $pPrice['prodPrice'];
$t = $proPri* $cQuant ;
}
}
?>
Alrighty, so assuming you want total of all items times all the quantities, here's what you do:
$ipAdd = getRealIpAddr();
$total = 0;
$checkPrice = "SELECT * FROM cart WHERE ipAdd = '$ipAdd' ";
$run = mysqli_query($conn,$checkPrice);
while($record = mysqli_fetch_array($run)){
$proId = $record['pId'];
$cId = $record['cId'];
$cQuant = $record['qnty'];
$proPric = "SELECT * FROM products WHERE prodId = '$proId' ";
$runPrice = mysqli_query($conn, $proPric);
$pPrice = mysqli_fetch_array($runPrice);
$proPri = $pPrice['prodPrice'];
$t = $proPri * $cQuant ;
$total += $t;
}
echo $total; // will be a sum of all prices times all quantities
You don't need while if you're fetching just one value, literally just removing it works fine
You start this process by declaring $total variable, in this case resetting it
You do everything you already did, after which you increment $total by whatever is the value of $t
Keep in mind that $total will change during this while loop, so considering this looks like a per-user-query, make sure to use it outside of the loop.
Good day! Please tell me how to pull data to obtain information with 3-different tables?
I now have:
$result = $this->db->query("SELECT * FROM oc_order_product WHERE order_id = (SELECT order_id FROM oc_order_product ORDER BY order_product_id DESC LIMIT 1)");
and
EOD;
for($i = 0; $i<count($result->rows); $i++) {
$points = $result->rows[$i]['points'];
$price = $result->rows[$i]['price'];
$counts = $result->rows[$i]['quantity'];
$str .= <<<EOD
And these have come from another table, not with oc_order_product, and oc_product_option_value:
$price = $result->rows[$i]['price'];
$counts = $result->rows[$i]['quantity'];
How do I add correctly**$result** oc_product_option_value and price, quantity? As has just never tried something goes wrong)