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.
Related
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'].
I have two different sql statements. $sql grabs all the items whose title matches a certain search text. $cat_sql grabs all the category_items that are in a certain category. An item has an ID. A category_item has a field called item_id which is a foreign key to IDs in the items table
...
mysqli setup code
...
$title = $_POST["title"];
$cat_id = $_POST["cat_id"];
$cat_sql = "SELECT * FROM category_items WHERE category_id = '".$cat_id."'";
$sql = "SELECT * FROM items where title LIKE '%". $title ."%' Limit 70";
if (!$result_cat = $mysqli->query($cat_sql)) {
// The query failed.
echo "<h2 >ERROR</h2>";
exit;
}
if (!$result = $mysqli->query($sql)) {
// The query failed.
echo "<h2 >ERROR</h2>";
exit;
}
Then I display all items:
while ($item = $result->fetch_assoc()) {
include 'item_card.php';
}
Currently this just displays all items fetched in the $sql query. Is there some way to remove all items from $result that do not have their ID represented as an item_id in $result_cat?
NOTE:
I would strongly prefer not to do just combine both SELECT statements into a table join because the actual $sql and $cat_sql are not nearly as simple as I have represented here. Also, they vary depending on which if statement they are in.
My question is: given $result and $result_cat, can I remove items from $result?
EDIT 1
As suggested by comments I am making an array if item_ids then doing an in_array query. Progress thus far:
$result_cat_ids = [];
while ($cat_item = $result_cat->fetch_assoc()) {
$result_cat_ids[] = $cat_item['item_id'];
}
EDIT 2 Here is the working code following the suggestions in the comments
if (in_array($item['id'], $result_cat_ids)) {
include 'item_card.php';
}
You may also use 'INTERSECT' sql clause.
$sql = "SELECT * FROM items WHERE id IN (SELECT item_id FROM category_items WHERE category_id = '".$cat_id."' INTERSECT SELECT id FROM items where title LIKE '%". $title ."%')";
This way, you can query for items that accomplish both conditions.
Note: I'm not using "limit 70" but you may add it as well.
I'm trying to make a list where the format goes as follows:
CLUB
name
name
name
CLUB 2
name
name
So far, I've managed to autogenerate tables but for each name it displays the name of the club above, even if it's the same club as another one above it.
I've tried to do it through mysqli, I don't know if it would be better through PHP. Here's my query so far:
$sql = "SELECT DISTINCT soc.nombre, soc.apellido, club.nombreClub FROM socios as soc
INNER JOIN club4h as club
ON soc.nombreClub = club.nombreClub";
$result =NULL;
foreach($conn->query($sql) as $row){
$club[] = $row["nombreClub"];
$nombreSoc[] = $row["nombre"];
$apellidoSoc[] = $row["apellido"];
}
And here's how it's looking with that query:
You can just use order by statement in your SQL query
$sql = "SELECT DISTINCT soc.nombre, soc.apellido, club.nombreClub FROM
socios as soc
INNER JOIN club4h as club
ON soc.nombreClub = club.nombreClub
ORDER NY club.nombreClub DESC, soc.nombre DESC";
So this way you will get your data sorted out of the database and all you need to do is display it.
So I am trying to populate a drop down by grabbing my playersID from the players table whilst sharing the same playersID with my Payments table.
When I put my query as below, it works fine.
$sql = "SELECT playersID FROM players";
But when I want to join the two tables together, I get an empty drop down.
<?php
include "dbconnect.php";
$sql = "SELECT players.playersID FROM players INNER JOIN Payments ON Payments.playersID = players.playersID";
if (!$result = mysql_query($sql, $conn))
{
die('Error in querying the database' . mysql_error());
}
echo "<br><select name = 'listbox' id = 'listbox' onclick = 'populate()'>";
while ($row = mysql_fetch_array($result))
{
$playersID = $row['playersID'];
$allText = "$playersID,";
echo "<option value = '$allText'> $playersID</option>";
}
echo "</select>";
mysql_close($conn); ?>
Note I have never done an INNER JOIN before, so it may be a simple issue. The aim of the code is to enter payment information into a form and save the data to the data table. I want to show the playerID in the payments table to show that particular player paid for this product.
Cheers!
It looks like you want to LEFT OUTER JOIN with the payments table instead of inner join, because the payment table does not have a payment for all players yet or for any at all.
Namastey!
I'm currently working on one of my projects where I get to one doubt.
What I'm doing is:
I have got 4 tables called:
1: project_degree
2: project_department
3: project_category
4: Projects
project_degree have got four top level degrees which are: B.tech, M.tech, M.C.A. & M.B.A.
project_department have got their respective departments such as: C.S.E, I.T., E.E.E. and so on.
Now, project_category have some categories like PHP, .NET, SAP etc.
What I'm trying to do is, I'm adding a project to database called "projects" where I'm assigning it to its degree, department and category.
Structure of projects table is as follows:
id | project name | degree_id | Dept_id | Cat_id
1 | Sample project | 1 | 3 | 4,3,6,5,9
Now as you can see above, field "cat_id" have got multiple categories where each categories ID have been separated by a comma.
Now when I call a query get all projects from the table called "projects", I want to list of categories related to that particular project and my code goes like this:
$query = mysql_query("SELECT * from projects ORDER BY id DESC");
while($row = mysql_fetch_array($query)) {
$categories = $row['cat_id'];
$cat_arr = explode(',',$categories);
$cat_size = sizeof($cat_arr);
for($i=0;$i<$cat_size;$i++) {
$get_cat = mysql_query("SELECT * from project_category WHERE id='".$cat_arr['$i']."'");
$cat_row = mysql_fetch_array($get_cat);
echo $cat_row['name']; // Here finally I'm getting categories name
}
}
So, finally I'm running nested loop and two queries to get a category name. I doubt it affect the page load time when it comes to huge data.
Is there any better alternative to this situation?
Here should be work
$query = mysql_query("SELECT * from projects ORDER BY id DESC");
while($row = mysql_fetch_array($query)) {
$get_cat = mysql_query("SELECT * from project_category WHERE id IN (" . $row['cat_id'] . ")");
$cat_row = mysql_fetch_array($get_cat);
echo $cat_row['name']; // Here finally I'm getting categories name
}
But normalization is better.
It's better to add a new table, project_has_catID where you can store project.id and cat.id as two columns (project_id and cat_id). Remove the Cat_id column in the project table.
Once done, simply select the name by the next query;
$query = mysql_query("SELECT project_category.name from project_category INNER JOIN project_has_catID phc ON phc.cat_id = project_category.id ORDER BY phc.project_id DESC");
By this, you have a list of category names. You don't have to loop with multiple database communications.
You could load all category names into an associative array first, then look up category names in this array when looping through the projects.
$categoryNames = array();
$query = mysql_query("SELECT id, name FROM project_category");
while ($row = mysql_fetch_array($query))
{
$categoryNames[$row['id']] = $row['name'];
}
$query = mysql_query("SELECT * FROM projects ORDER BY id DESC");
while ($row = mysql_fetch_array($query))
{
$categoryIds = explode(',', $row['cat_id']);
foreach ($categoryIds as $cat_id)
{
$cat_name = $categoryNames[$cat_id];
//do what you want with the name here
}
}
This way, you don't have to change your database structure, or the structure of your code. And you only have to execute two queries in total, so this is by far the simplest solution.
Needless to say, normalization is always better as others have indicated (never use a column that contains multiple comma-separated values), but you probably knew that and had your reasons for setting up your database tables this way.