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)
Related
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 ;
}
?>
I am android developer and have not much knowledge of php. currently I am working for make backend of my android application. I have quote table with about 1000 row in it. I am trying to update some row with below query.
$randomnumbershare = (rand(10,50));
$randomnumberlike = (rand(10,50));
$results = mysqli_query($mysqli,"UPDATE tbl_quotes SET qu_status=0, qu_like=$randomnumberlike,qu_share=$randomnumbershare,qu_favorite=0, qu_time=Now() ORDER BY _quid ASC ");
I want set qu_like and qu_share with random number from 10 to 50. its working fine but have only one issue that it's setting same number in all row, instead I want different number in all row. Anyone can please suggest me how can I do it ?
Thanks
The problem is you don't have a where clause, that means that the update is going to update all the rows in the table, if you want to have different values you need to do the update inside a for or while statement to repeat the update and add a where clause that matches every id of the table on every cicle.
Now you have this:
$randomnumbershare = (rand(10,50));
$randomnumberlike = (rand(10,50));
$results = mysqli_query($mysqli,"UPDATE tbl_quotes SET qu_status=0, qu_like=$randomnumberlike,qu_share=$randomnumbershare,qu_favorite=0, qu_time=Now() ORDER BY _quid ASC ");
But you need something like:
If you have a numeric id that goes from 1 to 1000 you can do something like
for ($x = 1; $x <= 1000; $x++)
{
$randomnumbershare = (rand(10,50));
$randomnumberlike = (rand(10,50));
$results = mysqli_query($mysqli,"UPDATE tbl_quotes SET qu_status=0, qu_like=$randomnumberlike,qu_share=$randomnumbershare,qu_favorite=0, qu_time=Now() where id=$x ORDER BY _quid ASC ");
}
That code do your update 1000 times, one update for each row you have in you table, now if you have a varchar id that is somethin more complex than a numeric sequence you need to add a query before that stores that ids into an array, something like:
$sql = mysql_query("select id from table_name");
$tableinfo = array();
foreach ($tableinfo as $tableid)
{
$randomnumbershare = (rand(10,50));
$randomnumberlike = (rand(10,50));
$results = mysqli_query($mysqli,"UPDATE tbl_quotes SET qu_status=0, qu_like=$randomnumberlike,qu_share=$randomnumbershare,qu_favorite=0, qu_time=Now() where id=$tableid[id] ORDER BY _quid ASC ");
}
For n number of row thinking you have a sequence from 1 to x number you can do:
$sql = mysql_query("select id from table_name");
$num_rows = mysql_num_rows($sql);
for ($x = 1; $x <= $num_rows; $x++)
{
$randomnumbershare = (rand(10,50));
$randomnumberlike = (rand(10,50));
$results = mysqli_query($mysqli,"UPDATE tbl_quotes SET qu_status=0, qu_like=$randomnumberlike,qu_share=$randomnumbershare,qu_favorite=0, qu_time=Now() where id=$x ORDER BY _quid ASC ");
}
I am displaying a list of items based on a database. The order of the items is by ProductID.
When I add an item to the database, it is automatically assigned a ProductID.
However, I would like to be able to either
a) sort the list alphabetically by name, without changing the ProductID in the database or
b) add another column to the database called, for example: Rank, and have this list display the list by Rank, rather than by ProductID.
Here is my code. Any help in re-writing this section of the code to accomplish either a) or b) would be very helpful!
PS: I'm a little familiar with PHP and databases, but I am by no means an experienced coder.
$category = array_search(strtolower('upright'), $CFG["Category"]);
$product2 = new ProductData();
$where2 = sprintf(" WHERE CategoryID=%d ORDER BY ProductID DESC", $category);
$rows2 = $product2->GetRows($where2);
$count2 = count($rows2);
$line_count2 = 4;
$total_lines2 = ceil($count2/$line_count2);
// Photo
$photo5 = new PhotoData();
$thumb_array5 = array();
$count3 = count($rows2);
for ( $i = 0; $i < $count3; $i++ )
{
$ret = $photo5->Query($rows2[$i]["ProductID"]);
if ( $ret != 0 )
{
continue;
}
$thumb_array5[$photo5->Get("ProductID")] = $photo5->Get("Photo1");
}
Your SELECT query is not shown which I assume is coming from your ProductData() class. If you could change the source of your ProductData() that includes ranking, you could use a variable for ranking, like:
SELECT
#Ranking := #Ranking + 1 AS Rank,
productID,
product_name
FROM yourTable t, (SELECT #Ranking := 0) r
ORDER BY product_name;
Otherwise you could do it in your Php instead:
First change the ORRDER BY to your product name instead of product id:
$category = array_search(strtolower('upright'), $CFG["Category"]);
$product2 = new ProductData();
$where2 = sprintf(" WHERE CategoryID=%d ORDER BY Product_Name", $category);
Then in your php use a variable to do the ranking:
$count3 = count($rows2);
echo "<table><tr><th>Ranking</th><th>Product Name</th><th>Product Name</th></tr>";
for ( $rank = 0; $rank < $count3; $rank++ )
{
echo "<tr>";
echo "<td>$rank+1</td><td>".$rows2[$rank]["Product_name"]."</td><td><img src='".$photo5->Get("Photo1")."'></td>"
echo "</tr>";
}
echo "</table>";
Here is my query, I am trying to get numbers from another table using a number from another table here is my query ...
$order_id = $template_vars['{order_name}'];
// Query to find the product id for the current order and then set it to a variable
$query="SELECT product_id FROM ps_order_detail WHERE id_order = $order_id";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
$Product_id = $row['0'];
// get all the custom part numbers and set them to the variables
$customnumbers ="SELECT API, SWAIM, JOHN_CRANE, SNOW_WELL, MIDAS, QUINN, WILSON, WEATHERFORD, HF, BLACK_GOLD, EDI, SO_CAL_PUMPS, WEST_RIVER
FROM ps_product_part_number WHERE Product_ID = $Product_id";
$secondresult = mysql_query($customnumbers);
$secondrow = mysql_fetch_row($secondresult);
$API = $secondrow['0'];
$SWAIM = $secondrow['1'];
$JOHN_CRANE = $secondrow['2'];
$SNOW_WELL = $secondrow['3'];
$MIDAS = $secondrow['4'];
$QUINN = $secondrow['5'];
$WILSON = $secondrow['6'];
$WEATHERFORD = $secondrow['7'];
$HF = $secondrow['8'];
$BLACK_GOLD = $secondrow['9'];
$EDI = $secondrow['10'];
$SO_CAL_PUMPS = $secondrow['11'];
$WEST_RIVER = $secondrow['12'];
How about doing it in one step with a join?
SELECT ppn.*
FROM ps_product_part_number ppn
join ps_order_detail od on od.product_id = ppn.Product_ID
WHERE od.id_order = $order_id
Am trying to calculate the number of rows in a table depending on a certain condition.
So, I did manage to write a piece of code that would function as required.
But, it's bit too long. So am wondering if there is any easier way to achieve it.
Code:
// Comments
$sql_total_comments = mysql_query("SELECT * FROM comments");
$sql_pending_comments = mysql_query("SELECT * FROM comments WHERE comment_status = '0'");
$sql_approved_comments = mysql_query("SELECT * FROM comments WHERE comment_status = '1'");
$sql_declined_comments = mysql_query("SELECT * FROM comments WHERE comment_status = '2'");
$total_comments_num = mysql_num_rows($sql_total_comments);
$pending_comments_num = mysql_num_rows($sql_pending_comments);
$approved_comments_num = mysql_num_rows($sql_approved_comments);
$declined_comments_num = mysql_num_rows($sql_declined_comments);
SELECT comment_status, COUNT(comment_status)
FROM comments GROUP BY comment_status;
In your code, either total the counts up for the total number or run a separate query on COUNT(*) as in the other answers.
So your code would look something like this, assuming you're using PDO:
$sql = 'SELECT comment_status, COUNT(comment_status) AS "count" '.
'FROM comments GROUP BY comment_status;';
$query = $db->query($sql);
$count_total = 0;
$count = array( );
while (($row = $query->fetch(PDO::FETCH_ASSOC)) !== FALSE) {
$count_total += $row['count'];
$count[$row['comment_status']] += $row['count'];
}
$query = null; // Because I'm neurotic about cleaning up resources.
Use COUNT() DB FUNCTION to do such job.
$ret = mysql_query("SELECT COUNT(*) FROM comments");
$row = mysql_fetch_row($ret);
$total_comments_num = $row[0];
Use select count(*) from comments where .....