查询2个表并将数据组合在一个数组中

时间:2014-07-05 08:22:04

标签: php mysql arrays

我正在尝试将来自2个查询的结果合并到一个数组中,但是在获取正确的代码时遇到了问题。

我有一个名为PrimaryEvents的表,我将其查询以添加到数组中。此查询可以正常工作:

$rows = query("SELECT * FROM PrimaryEvents WHERE unit = ? ORDER BY event", $unit);

我使用以下内容将数据添加到数组中:

foreach ($rows as $row)
    {
    //    $event = lookup($row["event"]); (removed as spotted by sergio not required)
        $event["event"] = $row["event"];
        $event["validity"] = $row["validity"];
        $event["eventsrequired"] = $row["eventsrequired"];
        $event["unit"] = $row["unit"];
        $event["role"] = $row["role"];

        //now we have the data lets try and do something to it
        if ($event["role"] == "0")
            {
                //then the user is active so change value to a YES
                $event["role"] = "ALL";
            }

        //add our stock to the portfolio array
        $portfolio[] = $event;  
    }

到目前为止,一切运作良好。我想查询一个单独的表并将某些结果添加到数组组合[];

$rowsCompleted = query("SELECT * FROM portfolio WHERE id = ? ORDER BY name", $_SESSION["id"]);

我最初的计划是使用以下附加代码将感兴趣的数据添加到投资组合[]但它不起作用:

//now add individual stats to the array
foreach($rowsCompleted as $row)
{
    if ($portfolio["event"] == $row["name"]
    {
    //then add our number and date to the array
    $portfolio["datecompleted"] = $row["datecompleted"];
    $portfolio["number"] = $row["number"];
    }

//else do nothing
}

我的最终目标是从投资组合表中提取datacompleted值和数值,然后仅在事件名称与投资组合数组中的名称匹配时才将其添加到数组中。

希望我已经很好地描述了我的问题和所需的行为。

感谢可以提供的帮助,我是php / sql的新手,所以我一边学习。

安迪

编辑: 还尝试了以下循环来尝试循环遍历数组:

//now add individual stats to the array
foreach($rowsCompleted as $row)
{
    foreach ($portfolio["event"] as $item)
    {
    if ($portfolio["event"] == $row["name"]
    {
        //then add our number and date to the array
        $portfolio["datecompleted"] = $row["datecompleted"];
        $portfolio["number"] = $row["number"];
    }
    }
//else do nothing
}

EDIT2:尝试使我的问题更加清晰:PrimaryEvents表列出了特定用户必须完成的所有事件。投资组合表跟踪用户已完成的事件(通过datecompleted和number)。

我想运行一个列出PrimaryEvents中所有事件的查询,然后在这些结果中添加来自用户组合的datecompleted和number。如果投资组合的答案是空白的,那么用户尚未完成该事件,但我仍希望从PrimaryEvents表数据中列出该事件。

我尝试了两个查询,因为它似乎遵循了我想要实现的目标。

EDIT3:

Barmar帮助下的新代码

foreach ($rows as $row) {
    $event = lookup($row["event"]);
    $event["event"] = $row["event"];
    $event["validity"] = $row["validity"];
//  $event["datecompleted"] = $row["datecompleted"];
//  $event["number"] = $row["number"];
    $event["eventsrequired"] = $row["eventsrequired"];
    $event["unit"] = $row["unit"];
    $event["role"] = $row["role"];

//now we have the data lets try and do something to it
    if ($event["role"] == "0") {
//then the user is active so change value to a YES
        $event["role"] = "ALL";
    }

//add our stock to the portfolio array
    $portfolio[] = $event;

//now add individual stats to the array
    foreach ($rowsCompleted as $row) {
        foreach ($portfolio as &$item) {
            if ($item["event"] == $row["name"]) {
//then add our number and date to the array
                $item["datecompleted"] = $row["datecompleted"];
                $item["number"] = $row["number"];
            }
        }
    }

}

页面仍然没有被chrome显示,所以猜测我错过了什么。

EDIT4:页面显示,当我在html页面上呈现表格时,我现在收到未定义的索引错误。

具体来说,未定义的索引错误是针对datecompleted和number的。我认为这意味着这些值没有被添加到投资组合数组中?尝试并帮助我添加了一个Else语句(如下所示),以确保即使事件不可用,索引也是:

//now add individual stats to the array
foreach($rowsCompleted as $row)
{
    foreach ($portfolio as &$item)
    {
         if ($item["event"] == $row["name"])
         {
    //then add our number and date to the array
        $item["datecompleted"] = $row["datecompleted"];
        $item["number"] = $row["number"];
         }
        else 
        {
        $item["datecompleted"] = "Never";
        $item["number"] = "0";
        }
    }
}

编辑5:接近成功。很抱歉重新打开这个但我刚刚注意到我没想到的行为:嵌套循环只设置datecompleted和第一个值的数字来匹配组合表。未设置后续值。看起来嵌套循环似乎没有单步执行所有投资组合值,只是退出第一个“event”==“name”。

编辑6:关于所需功能的样本数据和说明:

投资组合样本数据

|id|name    |number|datacompleted
|21|event1  |3     |2014-07-07
|15|event1  |5     |2014-07-05
|21|event2  |5     |2014-05-08
|15|event1  |1     |2013-05-05

id是完成事件的用户的id number是完成的事件数量

PrimaryEvents示例数据

|id|event   |validity|eventsrequired
|1 |event1  |7       |10
|1 |event2  |25      |1
|1 |event3  |12      |50

id是创建条目的用户的ID(仅用于历史目的)

所需的功能是:

查询应创建一个数组,以允许在PrimaryEvents表中创建所有内容的html表。此表列出了用户必须完成的事件。

第二个查询或当前嵌套循环应该从组合表中收集当前用户标识的数据,然后将事件名称与Primaryevents数组中的名称匹配,并更新(如果存在)number和datecompleted值。 (I.E填充用户已完成事件的数据)。

当前代码仅为第一个匹配合并来自投资组合的数据,但随后嵌套循环似乎退出。

希望这是对我想要实现的目标的更清晰的描述。

编辑:我已经更改了使用下面左连接语句的功能,但仍然遇到问题:

该表仅包含来自primaryEvents表的一些事件,而不是所有事件。被拉过的事件仅是用户已完成的事件,用户尚未完成的事件未被显示。

编辑:此查询似乎有效:

$allEvents = query("SELECT * FROM PrimaryEvents LEFT JOIN portfolio ON (PrimaryEvents.event = portfolio.name) WHERE PrimaryEvents.event = ? AND (portfolio.id = ? Or portfolio.id is null) ORDER BY PrimaryEvents.event", $currentEvent, $_SESSION["id"]);

2 个答案:

答案 0 :(得分:1)

您需要注意,投资组合不是关联数组而是多维数组,因此您无法使用$portfolio["event"]访问它。您应该使用$portfolio[0]["event"]$portfolio[1]["event"]等等。

很难向您展示确切的解决方案,因为我不知道应该如何合并这些数组/数据库查询。

修改

您的查询似乎应如下所示:

query("SELECT * FROM PrimaryEvents e LEFT JOIN portfolio p ON e.event = p.name WHERE e.unit = ? AND p.id = ? ORDER BY e.event", $unit,$_SESSION["id"]);

<强> EDIT2

由于性能损失,我没有提出嵌套循环(因为它现在处于修改过的问题中)。

答案 1 :(得分:1)

您的第二个查询越来越近了,但仍然对每个数组中的内容感到困惑。

foreach($rowsCompleted as $row)
{
    foreach ($portfolio as &$item) // Need to use reference so we can update it
    {
        if ($item["event"] == $row["name"])
        {
            //then add our number and date to the array
            $item["datecompleted"] = $row["datecompleted"];
            $item["number"] = $row["number"];
            break;
        }
    }
}

为了避免嵌套循环,$portfolio最好是一个关联数组。更改要使用的初始查询的代码:

    //add our stock to the portfolio array
    $portfolio[$event["name"]] = $event;  

然后第二个循环变为:

foreach($rowsCompleted as $row)
{
    $name = $row["name"];
    if (isset($portfolio[$name]) {
        $portfolio[$name]["datecompleted"] = $row["datecompleted"];
        $portfolio[$name]["number"] = $row["number"];
    }
}