The question title is not very clear so let me explain.
I am trying to make a table that gets all line items for a specific order so I am using a loop to get the all the line items for the order however my line item table structure is as follows
|Id (pk)| Order_Id (fk) | Line_Id | Item_Id (fk) | Item_Qty | Item_Price | Line_Total|
|1 | 1 | 1 | 1 | 1 | 25 |25 |
|2 | 1 | 2 | 2 | 3 | 6 |18 |
I now want to make a query within this loop get the item_number from the items table so it can display not just the item_id which is a little vague.
my main problems are I can’t seem to figure out how to get the query to know which item_id to use in the WHERE clause of the SQL statement. and also how to make the loop not run for within the loop for each time that the parent loop/query is running i.e. 3 line items so the the sub-query/loop is going to run 3 times instead of once because there are 3 values in the array through which it is looping.
$id = $_GET['id'];
$query = "SELECT * ";
$query.= "FROM quotes ";
$query.= "WHERE id = {$id}";
$confirmed_query = confirm_query($query);
if (!$confirmed_query) {
echo "Query Failed".mysql_error();
}
$query = mysql_query($confirmed_query);
$query_result = mysql_fetch_assoc($query);
echo "<table border=\"1\"><tr>";
foreach ($query_result as $key => $value) {
echo "<th>".ucwords(str_replace("_", " ", $key))."</th>";
}
echo "</tr><tr>";
foreach ($query_result as $key => $value) {
echo "<td>{$value}</td>";
}
echo "</tr></table><br /><br /><table border=\"1\"<tr>";
$query = "SELECT * ";
$query.= "FROM quote_details ";
$query.= "WHERE quote_id = {$id}";
$confirmed_query = confirm_query($query);
if (!$confirmed_query) {
echo "Query Failed".mysql_error();
}
$query = mysql_query($confirmed_query);
$header_written = false;
echo "</tr><tr>";
while ($query_result = mysql_fetch_assoc($query)) {
while ($header_written == false) {
foreach ($query_result as $key => $value) {
echo "<th>".ucwords(str_replace("_", " ", $key))."</th>";
$header_written = true;
}
}
echo "</tr><tr>";
//problem starts here
foreach ($query_result as $key => $value) {
$item_query = "SELECT item_number ";
$item_query.= "FROM items ";
$key_item = $key;
$item_query.= "WHERE id = {$key_item}";
$confirmed_query = confirm_query($item_query);
if (!$confirmed_query) {
echo "Query Failed".mysql_error();
}
$item_query = mysql_query($confirmed_query);
while ($item_number = mysql_fetch_assoc($item_query)) {
foreach ($item_number as $item) {
echo $item;
}
}
echo "<td>{$value}</td>";
}
}
echo "</tr></table>";
Please don’t mind the display html being mixed with the sql queries.
any help on how to approach this would be great i am just can’t seem to find an angle to begin to work this.
Thanks.
Rather than do it in a loop, use a SQL
JOINYou will need something like this