mySQL和PHP循环

Pau*_*ers 7 php mysql

我被授权访问第三方数据库,并希望使用他们的信息创建工具.为其原始目的而设计的数据库非常庞大且隔离.我需要完成以下任务:

从下面的Schema中,我需要完成以下任务:

在invTypes中查找项目,检查invTypeMaterials和ramTypeRequirements以查看是否需要构建项目的任何材料.如果是,则在invTypes中查找每个材质,然后再次重复该过程以查看那些是否需要组件.这个循环一直持续到对invTypeMaterials和ramTypeRequirements的检查都是False,这可以是5或6个循环,但每个循环要检查5或6个项目,因此可能是1561个循环,假设原始项目有1个循环,则每个循环5个循环材料有5,5次.

在此输入图像描述

现在我尝试完成代码并提出以下内容:

$materialList = array();

function getList($dbc, $item) {

    global $materialList;
    // Obtain initial material list
    $materials = materialList($dbc, $item);

    // For each row in the database
    while ($material == mysqli_fetch_array($materials)) {
        // Check if there are any sub materials required
        if (subList($dbc, $material['ID'])) {
            // If so then recurse over the list the given quantity (it has already done it once)
            for ($i = 0; $i < $material['Qty'] - 1; $i++) {
                if (!subList($dbc, $material['ID'])) {
                    break;
                }
            }
        } else {
            // If there are no further materials then this is the base material so add to the array.
            $materialList .= array(
                "Name" => $mMaterial['Name'],
                "Qty" => $mMaterial['Qty'],
                "ID" => $material['ID']
            );
        }
    }

    return $materialList;
}

function subList($dbc, $item) {

    global $materialList;
    // Query the material incase it require further building
    $mMaterials = materialList($dbc, $item['ID']);

    // If the database returns any rows, then it must have more sub-materials required
    if (mysqli_num_rows($mMaterials) > 0) {
        // Check the sub-materials to see if they intern require futher materials
        if (subList($dbc, $material['ID'])) {
            // If the function returns true then iterate over the list the given quantity (its already done it once before)
            for ($i = 0; $i < $material['Qty'] - 1; $i++) {
                if (!subList($dbc, $material['ID'])) {
                    break;
                }
            }
        } else {
            // if the database returns 0 rows then this object is the base material so add to array.
            $materialList .= array(
                "Name" => $mMaterial['Name'],
                "Qty" => $mMaterial['Qty'],
                "ID" => $material['ID']
            );
            return true;
        }
    } else {
        return false;
    }
}

function materialList($dbc, $item) {

    // Query
    $query = "  SELECT i.typeID AS ID, i.typeName AS Name,  m.Quantity AS Qty
                FROM invTypes AS i
                LEFT JOIN invTypeMaterials AS m
                ON m.materialTypeID = i.typeID
                LEFT JOIN ramTypeRequirements AS r
                ON r.typeID = i.typeID
                WHERE groupID NOT IN(278,269,278,270,268) AND m.typeID = $item";
    $snippets = mysqli_query($dbc, $query) or die('Error: ' . mysqli_error($dbc));

    return $snippets;
}
Run Code Online (Sandbox Code Playgroud)

我确信你们都注意到这个代码在涉及递归数据库调用时会违反每个编程规则.不是真的很实用,特别是在subList()不断调用自己直到它发现它是假的.SQL不是我强大的套件,但我不能为我的生活解决如何克服这个问题.

任何指针都会非常有用,我当然不会要求你们任何人为我重写我的整个代码,但如果你对我应该考虑的事情有任何想法,我将不胜感激.

Ja͢*_*͢ck 1

作为通用解决方案,我会执行以下操作:

  • 对于每一个,从和typeID收集invTypeMaterialsramTypeRequirements
  • 根据收集的数据,您创建一个新SELECT查询并继续循环

初始查询

SELECT t.*, m.materialTypeID, m.quantity AS m_quantity, r.requiredTypeID, r.quantity AS r_quantity
FROM invTypes t
LEFT JOIN invTypeMaterials m USING (typeID)
LEFT JOIN ramTypeRequirements r USING (typeID)
WHERE <conditions to select the types>
Run Code Online (Sandbox Code Playgroud)

我刚刚猜测需要加载额外表中的哪些数据;必要时进行扩展。

materialTypeID对于匹配行, and将requiredTypeID为非空,否则为空。

保留之前已加载的类型表,以便更快地参考。然后,对于第二个查询,您将条件替换为“WHERE t.typeID IN ()”之类的内容

让我知道这是否有意义以及它是否接近对您有用的内容:)