Join issue for two tables - php

I can not get Select to work properly. I'm fairly familiar putting img src and have used loops before but the select is stumping me.
I have two tables. TableA (Aid, Aname, Adescription) TableB (Bid, Bimagefile, Aid). TableB holds multiple images which belong to TableA Aid. There may be 5 images for one Aid and 3 images for another Aid.
How can I write the Select and Join so I only see images related to 1 TableA Aid? I have the following
$query = "SELECT TableB.Bid, TableB.Bimagefile, TableA.Aname
FROM TableB
INNER JOIN TableA ON TableB.Aid = TableA.Aid";
but the results are a complete list of all Aname and their matching Bimagefile. I need to have the result show for one Aid. Help please.
$stmt = $con->prepare($query);
$stmt->execute();
$num = $stmt->rowCount();
if($num>0){
echo "<table class='table table-hover table-responsive table-
bordered'>";
echo "<tr>";
echo "<th>Name</th>";
echo "<th>Image</th>";
echo "</tr>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
echo "<tr>";
echo "<td>{$Aname}</td>";
echo "<td>{$Bimagefile}</td>";
echo "</tr>";
}
echo "</table>";
}

Related

PHP show table from record with same id

I want to return to the user a table that shows his orders but when a user orders one product the table is ok but in the moment that orders 2 product they get 2 lines with the same ID but different ProductID same total price.
$email = strval($_SESSION["email"]);
$TabellaOrdini = "SELECT DISTINCT ordini.IDOrdini, clienti.Indirizzo,
clienti.Nome, clienti.Cognome, ordini.StatoConsegna,
prodotti.Descrizione, ordini.TotaleOrdine,
pacco.IDProdotti, pacco.Quantita
FROM (((clienti NATURAL JOIN ordini)
JOIN pacco ON ordini.IDPacco = pacco.IDPacco)
JOIN prodotti ON pacco.IDProdotti = prodotti.IDProdotti)
WHERE clienti.IDClienti = ordini.IDClienti
AND clienti.Email = '$email'
ORDER BY ordini.IDOrdini";
$CheckOrdini = "SELECT insertphpordini.IDOrdini,
concat(insertphpordini.Descrizione,' x ',insertphpordini.Quantita) AS Risultato
FROM insertphpordini
WHERE insertphpordini.Email = '$email'
AND insertphpordini.IDOrdini IN
( SELECT insertphpordini.IDOrdini
FROM insertphpordini
GROUP BY insertphpordini.IDOrdini
HAVING COUNT(IDOrdini)>1
)";
$result = $conn -> query($TabellaOrdini);
$result2 = $conn -> query($CheckOrdini);
echo "<table id='customers'>";
echo "<tr><th colspan=7>Ordini</th></tr>";
echo "<tr>";
echo "<td><b>IDOrdine</b></td>";
echo "<td><b>Nome</b></td>";
echo "<td><b>Cognome</b></td>";
echo "<td><b>Indirizzo</b></td>";
echo "<td><b>Stato Consegna</b></td>";
echo "<td><b>Prodotto</b></td>";
echo "<td><b>Totale Ordine</b></td>";
while($row = $result -> fetch_assoc()) {
echo "<tr>";
echo "<td> $row[IDOrdini]</td>";
echo "<td> $row[Nome]</td>";
echo "<td> $row[Cognome] </td>";
echo "<td> $row[Indirizzo] </td>";
echo "<td> $row[StatoConsegna] </td>";
echo "<td> $row[Descrizione] / $row[Quantita]</td> ";
echo "<td> $row[TotaleOrdine]€ </td>";
echo "</tr>";
}
echo "</table>";
the query $TabellaOrdini get all values from a user whit that email and the query $CheckOrdini get only the values with the same ID and concat the Description and quantity as Risultato.
My goal is that is possible to have 1 order with 1 ID and in Product(row 6 from left of the table) have "ProductName1/Quantity1, ProductName2/Quantity2"
Use GROUP_CONCAT() to combine values from multiple rows into a single column.
$TabellaOrdini = "SELECT ordini.IDOrdini, clienti.Indirizzo, clienti.Nome, clienti.Cognome, ordini.StatoConsegna,
prodotti.Descrizione, ordini.TotaleOrdine, GROUP_CONCAT(pacco.IDProdotti, '/', pacco.Quantita) AS Prodotto
FROM clienti
NATURAL JOIN ordini
JOIN pacco ON ordini.IDPacco = pacco.IDPacco
JOIN prodotti ON pacco.IDProdotti = prodotti.IDProdotti
WHERE clienti.IDClienti = ordini.IDClienti AND clienti.Email = '$email' ORDER BY ordini.IDOrdini
GROUP BY ordini.IDOdini";
It's also not necessary to group your joins with ().
And you should stop substituting variables directly into SQL, and instead use prepared statements with bind_param().
You can use better display of Orders with Order ID, Customer details, Date in one row along with a drop down with all detailed products and quantities. This way you are not limited with tabular display option.
Else, you can use GROUP_CONCAT with ordini.IDOrdini (your OrderID) and display products as "product01, product02, product03" for same Order01 for instance.
Here some link for data-tables with child-rows :
https://datatables.net/examples/api/row_details.html
Or you can ajax it out for large child product sets :
https://datatables.net/blog/2017-03-31
NOTE: Please ensure PHP security and try to follow some design pattern for a clean code. Look for Database Object and Binding input parameters with $conn->prepare [https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php]

K/D Ratio with SQL and PHP

So I'm trying to make a K/D ratio with some data from a database using SQL.
The main problem is that the tables are so strange and I'm trying to display it as an HTML table.
There's a table called stats_player_kill (when player kills another)
Here's the content of the table:
So if I want to get the kills, I count the killer column with the same id.
Here's how I get number of kills of each player and display it as an HTML table:
<table class="tablitas" id="kills">
<thead>
<tr>
<th data-column-id="name">Player</th>
<th data-column-id="killer" data-type="numeric">Kills</th>
</tr>
</thead>
<?php
$sql = "select p.name as name, count(*) as amountOfObjects
from stats_player_kill b
join stats_player p on p.id = b.victim
group by b.victim
order by count(*) desc
limit 15";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["name"]."</td><td>".$row["amountOfObjects"]."</td></tr>";
}
echo "</tbody>";
echo "</table>";
echo "</td>";
} else {
echo "0 results";
}
?>
So I need help making the math to divide number of kills by the total deaths.
Sorry for my English and thanks to everyone! :D

MySQL Selecting A Key From A Seperate Table

I am currently fetching data from a group of location MySQL tables here is what I currently have
$query = "SELECT Name, CountryCode, Population FROM City WHERE (Population > '6000000') ORDER BY Population DESC";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td>$row[2]</td>";
echo "</tr>\n";
}
mysql_close($link);
The thing I am trying to figure out is how to convert the CountryCode to the actual country. Here is how I would call the country table to get the name
SELECT Name FROM Country WHERE Code = 'CountryCode';
How would I combine these two statements into one?
EDIT: Here is what I got to work. Is this considered a JOIN?
$query = "SELECT a.Name, a.CountryCode, a.Population, b.Code, b.Name FROM City a, Country b WHERE (a.CountryCode = b.Code) AND (a.Population > '6000000') ORDER BY Population DESC";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>$row[0]</td>";
echo "<td>$row[2]</td>";
echo "<td>$row[4]</td>";
echo "</tr>\n";
}
mysql_close($link);
U need to join City table and Country table having one primary key and one foreign key.
Read about Natural Join. It's easy.
You can use this query:
$query = "SELECT city.Name,country.Name,city.CountryCode,city.Population from city INNER JOIN country where country.CountryCode=city.CountryCode and city.population>5000 ORDER BY city.Population DESC";
This is working fine.

Display count of unique values in MySQL db table using PHP

So I have a table named pins that has 3 columns that need to be taken into consideration. These columns are plan and order_id.
I need to get a count for all of the pins that have an order_id=0 and plan=9.
This is what I have so far:
$qT="SELECT plan, COUNT(*) as cnt FROM pins WHERE order_id=0 and plan=9";
$res=mysql_query($qT);<br/>
mysql_free_result($res);
while($row = mysql_fetch_array($res)) {<br/>
echo $row['plan'];<br/>
}
Any help in displaying the results would be a great help.
Try to group your pin using GROUP BY, So
$result = mysql_query("SELECT plan, COUNT(pin) as cnt FROM pins WHERE (order_id=0 and plan=9) GROUP BY plan");
echo "<table border='1' style='border-collapse: collapse'>";
echo "<th>Plan</th><th>Count</th>";
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>".$row['plan']."</td><td>".$row['cnt']."</td></tr>";
}
echo "</table>";
mysql_free_result($result);
?>

how to get specific ID for my WHERE clause from a different table? using joins

i use this table, and i was computing for the total of the SUM(quantity * price) but now i dont know how to use it for the specific serial from the customers table.
here is my query
<?php
$qry = "SELECT SUM(price * quantity), chever from order_detail";
$result = #mysql_query($qry);
while ($row=mysql_fetch_array($result)){
echo $row['chever'];
}
?>
i was using this but i didnt really understand it, i created the new query because SUM() messes up the old one, i dont kno why, but it shows 1 row only and no more.
$qry = "SELECT
order_detail.quantity,
order_detail.price,
customers.serial,
customers.name,
customers.payment,
customers.carrier,
orders.date,
order_detail.productid,
order_detail.quantity,
order_detail.price,
order_detail.orderid,
inventory.prod_name
FROM
customers
RIGHT JOIN
orders on customers.serial=orders.serial
RIGHT JOIN
order_detail on orders.serial=order_detail.orderid
LEFT JOIN
inventory on order_detail.productid=inventory.prod_id
WHERE
customers.serial='{$_GET['serial']}'";
mysql_set_charset("UTF8");
$result = #mysql_query($qry);
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}
echo "<div align='left'>";
echo "<table class='CSSTableGenerator' id=''>
<tr>
<td>Products</td>
<td>Quantity</td>
<td>Total Price</td>
<tr>";
while ($row=mysql_fetch_array($result)){
echo "<tr>";
//echo "<td>".$row['date']."</td>";
//echo "<td>".$row['name']."</td>";
//echo "<td>".$row['orderid']."</a></td>";
echo "<td>".$row['prod_name']."</td>";
echo "<td>".$row['quantity']."</td>";
echo "<td>".($row['price']*$row['quantity'])."</td>";
//echo "<td>".$row['payment']."</td>"
echo "</tr>";
}
echo "</table>";
how do i join my tables and use the where clause.
This might do the trick
$qry = "SELECT SUM(order_detail.price * order_detail.quantity) AS totalOD, customers.serial from order_detail
LEFT JOIN orders ON orders.serial = order_detail.orderid
RIGHT JOIN customers ON customers.serial = orders.serial
WHERE
customers.serial='{$_GET['serial']}'";
$result = #mysql_query($qry);
while ($row=mysql_fetch_array($result)){
echo $row['totalOD'];
}
?>

Categories