gpt4 book ai didi

php - 如何摆脱多个 while 循环并更好地显示结果

转载 作者:行者123 更新时间:2023-11-29 14:32:24 25 4
gpt4 key购买 nike

我的应用程序需要创建一个库存管理表,其中包含许多分类为子组的项目。对于这个想法,库存管理表如下所示:

                           Location1             Location2                Total
Desktops // Main Category
Microsoft //Sub-Category

Windows 7 23000 150000 173000
Office 2011 203300 3002 206302
....

Apple //Sub-Category
OS Snow Leopard 4000 3000 7003
OS Lion 39494 40034 79528
...

Tablets //Main Category
Lenovo //Sub-Cateogry

LX-243 3434 4399 7833
...

这是表格应该是什么样子的可视化,现在我有一个巨大的 mysql 查询,它为我做这个,这很可怕。总而言之,我正在做的事情如下:

  1. 选择第一个类别并启动 while 循环//echo 作为主要类别
  2. 选择主类别对应的子类别,并再次启动while循环。//echo 作为子类别
  3. 选择子类别中的所有项目并启动 while 循环。
  4. 选择第一个位置。
  5. 从 3 中选择当前项目,从 4 中选择当前位置并回显项目。//回显位置中的元素名称和数量。

现在我的问题是有没有比使用多个 while 循环更好的方法来显示它,我尝试使用函数,但它们也变得一团糟。另外,我不知道应该在哪里执行计算才能获得最后一列中的总数。

数据库结构:

Category: CategoryID, Description
Sub-Cateogry: Sub-Category_ID, Category_ID, Description
Item: Item_ID, Sub-category_id, Description
Location: Location_ID, Description
Stock_Management: Item_ID, Location_ID, Quantity

请求的代码:

$sql = mysql_query("select Board_ID, Title from Board where Company_ID = '$company_id' order by Title");

while($row = mysql_fetch_array($sql)) {
$curr_ID = $row[0];
$curr_category = $row[1];
echo "<tr class='sub-heading' style='background: rgba(76, 178, 255, 0.1)'><td colspan='".$count."'>".$curr_category."</td></tr>";
$sql1 = mysql_query("select Sub_Category_ID, Title from Sub_Category where Category_ID = '$curr_ID' order by Title");
while($row1 = mysql_fetch_array($sql1)) {
$curr_sub_cat_id = $row1[0];
$curr_sub_cate = $row1[1];
echo "<tr style='background: rgba(149, 255, 145, 0.10'><td colspan='".$count."'><b>".$curr_sub_cate."</b></td></tr>";
$sql2 = mysql_query("select Book_ID, title from Book where sub_category_id = '$curr_sub_cat_id' Order by title");
while($row2 = mysql_fetch_array($sql2)) {
$curr_book = $row2[0];
echo "<tr><td>".$row2[1]."</td>";
$sql4 = mysql_query("select OfficeID, OfficeTitle from Office where OfficeTitle IN ('$locations')");
while($row3 = mysql_fetch_array($sql4)) {
$curr_location = $row3[0];
$sql3 = mysql_query("select Quantity from Stock_Management where Book_ID = '$curr_book' and Location_ID = '$curr_location'");
while($row3 = mysql_fetch_array($sql3)) {
echo "<td>".$row3[0]."</td>";
}
}
echo "</tr>";
}
}
}

最佳答案

像这样怎么样 -

<?php

$locations = array('Location 1', 'Location 2', 'Location 3');
$locationCols = array();
$locationList = array();

foreach ($locations as $location) {
$locationColName = preg_replace('[^a-z0-9]', '_', strtolower($location));
$location = mysql_real_escape_string($location);
$locationCols[] = "SUM(IF(Office.OfficeTitle = '$location', Stock_Management.Quantity, 0)) AS `$locationColName`";
$locationList[] = "'$location'";
}
$locationCols = implode(', ', $locationCols);
$locationList = implode(',', $locationList);

$sql = "SELECT Board.Title AS BoardTitle, Sub_Category.Title AS SubCatTitle, Book.title AS BookTitle, $locationCols, SUM(Stock_Management.Quantity) AS Total
FROM Board
INNER JOIN Sub_Category
ON Board.Board_ID = Sub_Category.Category_ID
INNER JOIN Book
ON Sub_Category.Sub_Category_ID = Book.sub_category_id
INNER JOIN Office
LEFT JOIN Stock_Management
ON Book.Book_ID = Stock_Management.Book_ID
AND Office.OfficeID = Stock_Management.Location_ID
WHERE Board.Company_ID = 2
AND Office.OfficeTitle IN ($locationList)
GROUP BY Board.Title, Sub_Category.Title, Book.title";

$result = mysql_query($sql);

$prevBoard = '';
$prevSubCat = '';

echo '<table>';

while ($row = mysql_fetch_object($result)) {

// if new board print board
if ($prevBoard != $row->BoardTitle) {
echo '<tr class="sub-heading" style="background: rgba(76, 178, 255, 0.1)"><td colspan="">' . $row->BoardTitle . '</td></tr>';
}
$prevBoard = $row->BoardTitle;

if ($prevSubCat != $row->SubCatTitle) {
echo '<tr style="background: rgba(149, 255, 145, 0.10)"><td colspan=""><b>' . $row->SubCatTitle . '</b></td></tr>';
}
$prevSubCat = $row->SubCatTitle;

// print product row
echo '<tr>';
echo "<td>{$row->BookTitle}</td>";

foreach ($locations as $location) {
$locationColName = preg_replace('[^a-z0-9]', '_', strtolower($location));
echo "<td>{$row->$locationColName}</td>";
}

echo "<td>{$row->Total}</td>";
echo '</tr>';
}

echo '<table>';

关于php - 如何摆脱多个 while 循环并更好地显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9752830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com