I have this tables:
TABLE addonlist_final
TABLE addons
First, I have to JOIN the same addon_id so I can get all the needed details(which I've already done). Then I want to JOIN the column with the same identity(addon_id) in the table then add the quantity to each other.
So far I have this code:
<?php
include("conn.php");
echo "<table border='1' >";
echo "<tr>";
echo "<th>Description</th>";
echo "<th>Price</th>";
echo "<th>Qty</th>";
echo "<th>Total Cost</th>";
echo "</tr>";
$totaldue = 0;
$currentaddons = mysql_query("SELECT a.*, af.*
FROM addons AS a, addonlist_final AS af
WHERE a.addon_id = af.faddon_id and af.ftransac_id='2685'
ORDER BY af.timef DESC
");
while($rows = mysql_fetch_assoc($currentaddons)){
$desc = $rows['description'];
$price = $rows['price'];
$qty = $rows['quantity'];
$totalcost = $price * $qty;
$totaldue += $price * $qty;
echo "<tr>";
echo "<td>$desc</td>";
echo "<td>$price</td>";
echo "<td>$qty</td>";
echo "<td>$totalcost</td>";
echo "</tr>";
}
echo "</table>";
echo "<h3>Total Due: ".number_format($totaldue)."</h3>";
?>
This shows me this:
What I want to show is:
Is this possible with just one query? Thanks in advance :)
You uave to use group by
SELECT a.description, sum(af.quantity) as quantity,price
FROM addons AS a, addonlist_final AS af
WHERE a.addon_id = af.faddon_id and af.ftransac_id='2685'
group by a.description
ORDER BY af.timef DESC
Related
<?php
$con = mysqli_connect("localhost","root","","final_osa");
$s_stud = $con->query("SELECT * FROM violations_tbl GROUP BY violation_type");
while($data = $s_stud->fetch_assoc() ){
$bilang = $con->query("SELECT COUNT(*) FROM violations_tbl WHERE `violation_type` ='".$data['violation_type']."' ");
$result = $bilang->fetch_assoc();
if($result['COUNT(*)'] > 1 ){
echo "<tr>";
echo "<td>";
$query=$con->query("SELECT `violation_type` FROM `violations_tbl` WHERE `violation_type`='".$data['violation_type']."'");
while($row=$query->fetch_assoc() ){
echo $row['violation_type'].", ";
}
echo "</td>";
echo "</tr>";
}
}
?>
How can i eliminate same fetched data and echo only one? thanks
here is the one that it echoes. it should be that it will echo only one because its the same
What i'm trying to do here is get the mos violated rule in school thanks
If you want all the violations in order of most violated to least violated
$s_stud = $con->query("SELECT violation_type, count(violation_type) as num_violations
FROM violations_tbl
GROUP BY violation_type
ORDER BY num_violations DESC");
while($row= $s_stud->fetch_assoc() ){
//echo the violation and the count
if($row['num_violations'] > 1 ){
echo "<tr>";
echo "<td>$row[violation_type]</td>";
echo "<td>$row[num_violations]</td>";
echo "</tr>";
}
}
If you only want the MOST violated you could add a LIMIT 1 to the query and remove the looping.
$s_stud = $con->query("SELECT violation_type, count(violation_type) as num_violations
FROM violations_tbl
GROUP BY violation_type
ORDER BY num_violations DESC
LIMIT 1");
$row= $s_stud->fetch_assoc();
//echo the violation and the count
echo "<tr>";
echo "<td>$row[violation_type]</td>";
echo "<td>$row[num_violations]</td>";
echo "</td></tr>";
I have 2 tables: users and transactions
In users I have: userid, name, email
And in transactions I have: id, idsender, idreceiver
I want to make a log-table showing all the transactions and I want them to be displayed like: Transaction ID - SENDER'S NAME - RECEIVER'S NAME
I tried like this but it doesn't seem to work, at "Receiver" it doesn't show anything .. :
echo "<table>";
echo "<tr>";
echo "<th>Transaction ID</th>";
echo "<th>Sender</th>";
echo "<th>Receiver</th>";
echo "</tr>";
$resultuser = mysqli_query($conn, "SELECT * FROM users");
$rowuser=mysqli_fetch_array($resultuser);
$resulttrans = mysqli_query($conn, "SELECT * FROM transactions");
$rowtrans=mysqli_fetch_array($resulttrans);
$ress=mysqli_query($conn, "SELECT * FROM transactions");
while($row=mysqli_fetch_array($ress)){
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>"." same as receiver"."</td>";
$receiver=mysqli_query($conn, "SELECT name FROM users WHERE userid='" . $rowtrans['idreceiver'] . "'");
$receivername = mysqli_fetch_array($receiver);
echo "<td>". $receivername ."</td>";
echo "</tr>";
}
Don't use two queries. Join the two tables in one query:
$ress = mysqli_query("SELECT t.id, u1.name AS sendername, u2.name AS receivername
FROM transactions AS t
JOIN users AS u1 ON u1.userid = t.idsender
JOIN users AS u2 ON u2.userid = t.idreceiver");
while ($row = mysqli_fetch_assoc($ress)) {
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>" . $row['sendername'] ."</td>";
echo "<td>" . $row['receivername'] . "</td>";
echo "</tr>";
}
You are echoing from fetched array in $receivername = mysqli_fetch_array($receiver);
you should do echo "<td>". $receivername['name'] ."</td>";
Note:
Fetched array from mysqli_fetch_array() can be echoed using indexes by number $receivername[0] or by string $receivername['name'], that is where you are missing here.
I'm trying to display information from two queries in one single table, but can't figure out how to make it work.
This is what I got working with one query:
SELECT Company,COUNT(*) as count FROM Employee_Table GROUP BY Company ORDER BY count DESC;
Company Employees
ABC 45
DEF 15
GHI 5
Now beneath that I'd like to have another query that simply counts all rows, giving me the total amount of Employees.
SELECT Company,COUNT(*) as count FROM Employee_Table GROUP BY Company ORDER BY count DESC;
SELECT COUNT(*) AS Total FROM Employee_Table;
Company Employees
ABC 40
DEF 15
GHI 5
Total 60
This is what my code looks like right now. I defined two extra variables for my extra query that I want to echo out, but believe this is not the proper way to do it as I get an sqlsrv_fetch_array error.
$query1 = "SELECT Company,COUNT(*) as count FROM Employee_Table GROUP BY Company ORDER BY count DESC;";
$query2 = "SELECT COUNT(*) AS Totaal FROM Employee_Table;";
$result1 = sqlsrv_query($conn, $query1);
$result2 = sqlsrv_query($conn, $query2);
echo "<table id='total'>";
echo "<tr><th>Company</th><th>Amount of employees</th></tr>";
while ($row=sqlsrv_fetch_array($result1, $result2)) {
echo "<tr><td>";
echo $row["Company"];
echo "</td><td>";
echo $row["count"];
echo "</td><td>";
echo $row["Total"];
echo "</td></tr>";
}
echo "</table>";
How can this be achieved? I'd appreciate any help
First correct
sqlsrv_fetch_array()
see here http://php.net/manual/de/function.sqlsrv-fetch-array.php
Use just a single query and use mysqli_num_rows() function to count
$count = mysqli_num_rows($result);
I think you can achieve this by using only first query
$query1 = "SELECT Company,COUNT(*) as count FROM Employee_Table GROUP BY Company ORDER BY count DESC;";
$result1 = mysql_query($conn, $query1);
$total_employee = 0;
echo "<table id='total'>";
echo "<tr><th>Company</th><th>Amount of employees</th></tr>";
while ($row=mysql_fetch_array($result1)) {
echo "<tr><td>";
echo $row["Company"];
echo "</td><td>";
echo $row["count"];
$total_employee += $row["count"];
echo "</td><td>";
echo "</td></tr>";
}
echo "<tr><td>Total</td><td>$total_employee</td></tr>";
echo "</table>";
You can use only one query:
$result = sqlsrv_query($conn, "SELECT Company,COUNT(*) as count FROM Employee_Table GROUP BY Company ORDER BY count DESC");
?>
<table id='total'>
<tr><th>Company</th><th>Amount of employees</th></tr>
<?php
$total = 0;
while ($row=sqlsrv_fetch_array($result)) {
$total += $row["count"];
?>
<tr><td><?= $row["Company"] ?></td><td><?= $row["count"] ?></td></tr>
<?php
}
?>
<tr><td></td><td><?= $total ?></td></tr>
</table>
<?php
Try the following code
$query1 = "SELECT Company,COUNT(*) as count FROM Employee_Table GROUP BY Company ORDER BY count DESC;";
$query2 = "SELECT COUNT(*) AS Totaal FROM Employee_Table;";
$result1 = sqlsrv_query($conn, $query1);
$result2 = sqlsrv_query($conn, $query2);
echo "<table id='total'>";
echo "<tr><th>Company</th><th>Amount of employees</th></tr>";
while ($row=sqlsrv_fetch_array($result1)) {
echo "<tr><td>";
echo $row["Company"];
echo "</td><td>";
echo $row["count"];
echo "</td><td>";
echo "</td></tr>";
}
$rows = sqlsrv_fetch_array($result1)
echo "<tr><td>";
echo $rows["Total"];
echo "</td></tr>";
echo "</table>";
I am making the code of PHP-MySQL. But I gain have the 2 and more as the result of the left join in the MySQL. So I want to union the results in a cell of the result table as the captured picture.
Here is my real captured picture in my computer.
My php code is as like below.
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result))
{
echo "<table border='1' style='background:#dddddd;border-color:green;'>";
echo "<h2><p >진료과 (Subject) : ".$row['subject']."</p></h2>";
echo "<tr>";
echo "<th >"."<form action='search1.php' method='get'>"."<button type='submit'
name='code' value='".$row['code']."'>Code</th>";
echo "<th ><a href='".$row['ds_url']."'>"."한국병명</a></th>";
echo "<th ><a href='".$row['ds_url']."'>"."Disease name(En.)</a></th>";
echo "<th >"."<form action='search1.php' method='get'>"."<button type='submit'
name='code' value='".$row['family']."'>"." Family History</button> </th>";
echo "</tr>";
echo "<tr>";
echo "<td >" . $row['code'] . "</td>";
echo "<td >" .$row['disease_co']."</td>";
echo "<td >" .$row['disease_en']."</td>";
echo "<td >" .$row['family']."</td>";
echo "</tr>";
---
echo "<tr>";
echo "<th>소아과</th>";
echo "<th>내과</th>";
echo "<th>산과</th>";
echo "<th>정형외과</th>";
echo "</tr>";
echo "<tr>";
echo "<td>".$row['ped_content']."</td>";
echo "<td>".$row['im_content']."</td>";
echo "<td>".$row['ob_content']."</td>";
echo "<td>".$row['os_content']."</td>";
echo "</tr>";
----
echo "</table>";
}
The While phrase in the above php code is just 1, but the number of the result table depend on the number of im_content.
So, I want to union the result of im_content in a cell as the above captured picture.
But, I am short of ability.
Please give me a piece of advice.
Thank you for your concern.
My sql query sentence is as like below.
SELECT code_en.code, code_co.disease_co, code_en.disease_en , hx.family,
hx.personal, note.note, inclusion.inclusion, exclusion.exclusion, ds.ds_content,
------
subject.icd_category, subject.group_code, im.im_content
FROM code_en
LEFT JOIN subject ON code_en.code = subject.code
LEFT JOIN note ON code_en.code = note.code
-------
left join im on code_en.code = im.code
WHERE code_en.code = '".$code."'"
Thank you, Barmar!
I'm not totally sure I understand the question, but I think this is what you want:
SELECT code_en.code, code_co.disease_co, code_en.disease_en , hx.family,
hx.personal, note.note, inclusion.inclusion, exclusion.exclusion, ds.ds_content,
------
subject.icd_category, subject.group_code, GROUP_CONCAT(im.im_content) AS im_content
FROM code_en
LEFT JOIN subject ON code_en.code = subject.code
LEFT JOIN note ON code_en.code = note.code
-------
left join im on code_en.code = im.code
WHERE code_en.code = '".$code."'"
Thanks for reading my question
i am trying to make *clients_id* from the table repair_jobs appear as the name from the table contacts
but i am having no luck
i have got 2 sql query's is this wrong?
the 1st
$query = "select * from repair_jobs";
this helps me display the information i need regarding the fields from repair_jobs and works
this is the 2nd
$query = "SELECT repair_jobs.client_id, contacts.name
FROM repair_jobs
INNER JOIN contacts
ON repair_jobs.client_id=contacts.name";
under that i have this to try to display the name of the client
echo "<td>{$client_id}</td>";
but it is only displaying the number and not the data (clients name) that i need
am i missing something?
Additional information
The client_id (repair_jobs) is a number and is the same as id (contacts) but wanting to display the name (contacts)
CLIENTS
Id – name – surname – phone – address
REPAIRS
Id – clients_id (same as id in clients) – unit – date – price
current code
<?php
//include database connection
include 'db_connect.php';
//query all records from the database
$query = "select * from repair_jobs";
//execute the query
$result = $mysqli->query( $query );
//get number of rows returned
$num_results = $result->num_rows;
//this will link us to our add.php to create new record
if( $num_results > 0){ //it means there's already a database record
//start table
//creating our table heading
echo " <table class='table_basic'>";
echo "<thead><tr>";
echo "<th>Job #</th>";
echo "<th>Name Of Unit</th>";
echo "<th>Client</th>";
echo "<th>Estimated Value</th>";
echo "</thead></tr><tbody><tr>";
//loop to show each records
while( $row = $result->fetch_assoc() ){
//extract row
//this will make $row['firstname'] to
//just $firstname only
extract($row);
//creating new table row per record
echo "<tr>";
echo "<td width='40px'><a href='rdetails.php?id={$id}'># {$id}</a></td>";
echo "<td>{$rmake} {$rmodel}</td>";
$query = "SELECT rj.client_id, c.name AS client_name FROM repair_jobs rj INNER JOIN contacts c ON rj.client_id=c.id";
echo "<td>{$client_name}</td>";
echo '<td align="center"><span class="badge badge-success">£';
$lhours = $labour;
$repaircosts = $ourcosts;
$labourpay = $labourcharge;
$sum_total = $repaircosts +($lhours * $labourpay);
print ($sum_total);
echo '</span></td>';
echo "</td>";
echo "";
}
echo "</tr></table>";//end table
}else{
//if database table is empty
echo "No records found.";
}
//disconnect from database
$result->free();
$mysqli->close();
?>
Change your 1st query to you join query, as there is no reason to do a 2nd query in the middle of your code. (also you never executed that query anyway).
//query all records from the database
$query = "SELECT repair_jobs.*, contacts.name as client_name
FROM repair_jobs
INNER JOIN contacts
ON repair_jobs.client_id=contacts.id";
Then in your table keep the $client_name
echo "<td>{$client_name}</td>";
<?php
include 'db_connect.php';
$query = "SELECT rj.Id AS job_number, rj.unit, rj.make, rj.model, c.name AS client_name, rj.price FROM repair_jobs rj INNER JOIN contacts c ON rj.clients_id = c.id ORDER BY c.date";
$result = $mysqli->query( $query );
$num_results = $result->num_rows;
if( $num_results > 0){ //it means there's already a database record
echo " <table class='table_basic'>";
echo "<thead><tr>";
echo "<th>Job #</th>";
echo "<th>Name Of Unit</th>";
echo "<th>Client</th>";
echo "<th>Estimated Value</th>";
echo "</tr></thead><tbody>";
while( $row = $result->fetch_assoc() ){
extract($row);
echo "<tr>";
echo "<td width='40px'><a href='rdetails.php?id={$job_number}'>#{$job_number}</a></td>";
echo "<td>{$make} {$model}</td>";
echo "<td>{$client_name}</td>";
echo "<td align='center'><span class='badge badge-success'>£";
$lhours = $labour;
$repaircosts = $ourcosts;
$labourpay = $labourcharge;
$sum_total = $repaircosts +($lhours * $labourpay);
echo $sum_total;
echo '</span></td>';
echo "</td>";
echo "</tr>";
}
echo "</tbody></table>";
} else {
echo "No records found.";
}
$result->free();
$mysqli->close();
?>