I have two tables in a database. Attached images for the same.
The "Name" row in both tables are the same. Table1 consists of the specification details of each item. Table2 is just the list of product names that should be included on the whole page. I want to fetch all the specifications for each product name. For example, the Product Rajwadi dinner set should get all its specifications from Table1. I need to add about 50 such products. The way I want it displayed is also shown in the image below.
The code I have been trying goes like this :
<?php
include("connection.php");
$db_dinner_set = "dinner_set";
$db_names = "names";
$query2= "SELECT * FROM $db_names";
//echo($query);
$statement2 = $connection2->prepare($query2);
$statement2->execute();
$result2 = $statement2->fetchAll();
foreach($result2 as $row){
echo '
'.$row['Name'].'
<br>
<br>
';
$Name = $row['Name'];
$query3= "SELECT * FROM $db_dinner_set WHERE Name = $Name ";
//echo ($query3);
$statement3 = $connection2->prepare($query3);
$statement3->execute();
$result3 = $statement3->fetchAll();
foreach($result3 as $row)
{
echo '
<div class="poem-genre">
'.$row['Items'].'
</div>
';
}
}
?>
Your table design needs a bit of work, but you can still accomplish the task. Join the tables on the name (not ideal):
SELECT d.Name, d.Items, d.pieces, d.image FROM dinner_set d LEFT JOIN names n ON d.Name = n.Name
Then loop through the results in PHP to build an object for each item.
$items = [];
foreach($result3 as $row){
// Add item by name
if (!array_key_exists($row['name'], $items)) {
$items[$row['name']] = [];
}
$piece = [];
// Add attributes of the particular piece
$piece['name'] = $row[Items];
$piece['qty'] = $row[pieces];
// Assign this piece to this item
$items[$row['name']][] = $piece;
}
Now you have an array of items, each item will contain a list of the pieces for that item.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.