I have a table named "prices" in which I have two fields where I reference two other tables named "prodcuts" and "supliers". In the "prices" table I do reference to the "id" of the "prodcuts" and "supliers" and I need to get the name of both in a php code... What I have so far is this, but I don't know how to make it work:
$result = mysqli_query($conn, "SELECT * FROM prices") or die("Could not find");
if (mysqli_num_rows($result) > 0) {
while ($rr = mysqli_fetch_assoc($result)) {
$a = $rr['id'];
$b = $rr['date'];
$c = $rr['product'];
$d = $rr['suplier'];
$e = $rr['quantity'];
$f = $rr['packaging'];
$g = $rr['event'];
$h = $rr['price'];
$prov .="
<tr>
<td>".$b."</td>
<td><a href='productos-result.php?search=".$c."'>".$c."</a></td>
<td><a href='proveedores-result.php?search=".$d."'>".$d."</a></td>
<td>".$e."</td>
<td>".$g."</td>
<td>$".$h."</td>
</tr>";
}
}
I need to display the name of the product $c and suplier $d instead of the id, my tables are like this:
prices
id, date, productid, suplierid, quantity, event, price
product
id, name, description
suplier
id, name, description
Change your query to join the products and suppliers and have their names in the list of columns aliased to what you want.
SELECT prices.id,
prices.date,
product.name product,
suplier.name suplier,
prices.quantity,
prices.packaging,
prices.event,
prices.price
FROM prices
INNER JOIN product
ON product.id = prices.productid
INNER JOIN suplier
ON suplier.id = prices.suplierid;
(Change the INNER JOIN to LEFT JOIN if you have prices without supplier or product but want to show them anyway (with empty names).)
Related
For this problem I need to make a PHP page where the user can search an invoice table by inputting a "quantity" value. The form then takes that quantity and spits out a table with the name, invoice number, quantity, and item description for all invoices where the quantity exceeds the quantity the user submitted.
For the most part I have my page set up and working fine. Where I'm getting stuck is on the query side -- specifically, the code below is providing me a list of invoices where the quantity is identical to the input quantity.
I've tried changing "WHERE ii.quantity LIKE ?" to "WHERE ii.quantity > ?" but all that does is provide me a list of all invoices without filtering by the user submitted quantity.
$query =
"SELECT c.first_name, c.last_name, ii.invoice_id, ii.quantity, it.description
FROM `c2092`.`customer` c
JOIN `c2092`.`invoice` i ON c.customer_id = i.customer_id
JOIN `c2092`.`invoice_item` ii ON ii.invoice_id = i.invoice_id
JOIN `c2092`.`item` it ON it.item_id = ii.item_id
WHERE ii.quantity LIKE ?
ORDER BY ii.invoice_id";
This is too complex query. Try to make explain on it. I am sure it will use filesort, and even temp table.
The better approach is to make several queries:
$invoiceItems = $db->query(
"SELECT ii.invoice_id, ii.id, ii.quantity, it.description
FROM `c2092`.`invoice_item` ii
JOIN `c2092`.`item` it ON it.item_id = ii.item_id
WHERE ii.quantity > ?
ORDER BY ii.invoice_id");
$invoiceItemMap = [];
$invoiceIds = [];
foreach ($invoiceItems as $invoiceItem) {
$invoiceItemMap[$invoiceItem['invoice_id']][] = $invoiceItem;
$invoiceIds[$invoiceItem['invoice_id']] = $invoiceItem['invoice_id'];
}
$invoiceIds = array_values($invoiceIds);
$userInvoices = $db->query(
"SELECT c.first_name, c.last_name, i.invoice_id
FROM `c2092`.`invoice` i
JOIN `c2092`.`customer` c ON c.customer_id = i.customer_id
WHERE i.id IN (".implode(',', $invoiceIds).")");
$result = [];
foreach ($userInvoices as $row) {
$result[] = array_merge($row, $invoiceIds[$row['invoice_id']]);
}
Hello,
What is the value of your variable?
Prefer the use of named parameter instead of ? syntax.
So basically, I have 2 tables
first table called "brand" and it contains columns "brand_id", "brand_name"
Then I have a second table called "products" that contains many columns but the most important is "product_brand"
Brand table:
Product table:
When i use this code
$brand = mysqli_query($conn, "SELECT products.*,brand.brand_name FROM products LEFT JOIN brand ON products.product_brand = brand.brand_id") or die(mysqli_errno($conn). '-'. mysqli_error($conn));
then i try to echo it like this <?php echo $znacka['brand_name']; ?>
When i do that it only display the first brand on every product.
Can you please give me some advice?
If you want all data from both table then please remove left.
You have to apply inner join for this.
$brand = mysqli_query($conn, "SELECT products.*,brand.brand_name FROM products JOIN brand ON products.product_brand = brand.brand_id") or die(mysqli_errno($conn). '-'. mysqli_error($conn));
Now you can use $znacka['brand_name'].
Hey guys i want to show a table so that table have 2 foreign keys.My food and restaurant table have name column.i tried following codes but i dont know how to show restaurant name and food name different.
$sql_select_food = "SELECT food.id,restaurant.name,food.name,food.restaurant_id,foodcategory.title from food INNER join foodcategory ON food.foodcategory_id=foodcategory.id INNER join restaurant ON food.restaurant_id=restaurant.id WHERE food.isActive = 1 ORDER BY food.updateDate DESC";
$result_select_food = mysqli_query($connection, $sql_select_food);
if (mysqli_num_rows($result_select_food) > 0) {
while ($row_select_food = mysqli_fetch_assoc($result_select_food)) {
echo "
<tr>
<td>$row_select_food[name]</td>
<td>$row_select_food[name]</td>
<td>$row_select_food[title]</td>
</tr>
";
}
}
Use column aliases:
SELECT food.id AS food_id, restaurant.name AS restaurant_name, food.name AS food_name ...
Then
$row_select_food['restaurant_name'] is the name of the restaurant,
$row_select_food['food_name'] is the name of the food,
etc.
Note: AS keyword is optional.
Put an alias to the column name and call it later in the code as a column name like :
$sql_select_food = "SELECT food.id,restaurant.name as rname, food.name as fname, food.restaurant_id,foodcategory.title from food INNER join foodcategory ON food.foodcategory_id=foodcategory.id INNER join restaurant ON food.restaurant_id=restaurant.id WHERE food.isActive = 1 ORDER BY food.updateDate DESC";
$result_select_food = mysqli_query($connection, $sql_select_food);
if (mysqli_num_rows($result_select_food) > 0) {
while ($row_select_food = mysqli_fetch_assoc($result_select_food)) {
echo "
<tr>
<td>$row_select_food[rname]</td>
<td>$row_select_food[fname]</td>
<td>$row_select_food[title]</td>
</tr>
";
}
}
I'm having problem to sort table values by different categories of books and I'm having 10 categories of books.
Categories are mentioned in a table entitled "categories" as (1, 2, 3,...10) and each category contains more than 100 books.
I just want to sort books only by one category such as 1 from my database and ignore all the other categories.
I used following php codes but it sorted the results as whole of column "categories" that means all the categories are sorted one by one but
I just want to sort data by single category.
$orderby = "ORDER BY books.category DESC";
$addparam = "";
if ($_GET["incldead"] == 2)
$wherea[] = "visible = 'yes'";
$res = mysql_query("SELECT COUNT(*) FROM books $where") or die(mysql_error());
$row = mysql_fetch_array($res,MYSQL_NUM);
$count = $row[0];
if ($count)
{
$pager = pager($booksperpage, $count, "browse.php?" . $addparam);
$query = "SELECT books.id, books.category, books.name, books.times_completed, books.size, books.added, books.type, books.comments,books.numfiles,books.filename,books.owner,IF(books.nfo <> '', 1, 0) as nfoav," .
"categories.name AS cat_name, categories.image AS cat_pic, users.username FROM books LEFT JOIN categories ON category = categories.id LEFT JOIN users ON books.owner = users.id $where $orderby {$pager['limit']}";
$res = mysql_query($query) or die(mysql_error());
Output of above code similar to below:
My desired output:
Please guide me how it'll be possible by amending in above php codes...
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.