使用php

时间:2016-04-07 22:23:50

标签: php mysql arrays

我正在一个网站上工作,我从多个表中输出收集的数据,其中前两个保存基本信息,后两个保存属性和图像。目前,我已经完成了几行代码,这些代码返回表格的值,并输出大多数我最终将其附加到html时显示的数据。我遇到的几个问题是复制问题,我理解它与内部联接的领域有关。我的php文件已经过滤掉了一定程度的重复,但是我试图制作的两个数组失败了,那就是图像数组或特征数组。所以我有

if (isset($_POST['getAll'])) {

$sortBy = $_POST['getAll'];

$sqlQuery = "
SELECT 
    u.user_id, 
    u.user_username, 
    pr.project_id, 
    pr.created_date, 
    pr.closing_date, 
    pr.prize, 
    pr.project_desc, 
    pr.project_title, 
    pr.state, 
    prr.room_id, 
    prr.room_name, 
    prr.room_type, 
    prp.prop_id, 
    prp.comment_extra_details, 
    prp.feature_name, 
    prf.caption, 
    prf.filename, 
    prf.filetype, 
    prf.file_id,
     prf.public_name 
FROM users AS u 
    INNER JOIN projects as pr ON u.user_id = pr.creator_id 
    INNER JOIN project_rooms as prr ON prr.project_id = pr.project_id 
    INNER JOIN project_properties as prp ON prp.room_id = prr.room_id 
    INNER JOIN project_files as prf ON prf.room_id = prp.room_id
WHERE 
    state = 1 
ORDER BY 
    `created_date` DESC";
}

$result = $conn->query($sqlQuery);

$proj_id = 0;
$room_id = 0;
$prop_id = 0;
$file_id = 0;

$projects = array();
$rooms = array();
$features = array();
$images = array();

while ($row = $result->fetch(PDO::FETCH_ASSOC)) {

if ($proj_id != $row["project_id"]) {

    $details = array(
        "user_id" => $row["user_id"],
        "username" => $row["user_username"],
        "project_id" => $row["project_id"],
        "created_date" => $row["created_date"],
        "closing_date" => $row["closing_date"],
        "prize" => $row["prize"],
        "project_desc" => $row["project_desc"],
        "project_title" => $row["project_title"],
        "rooms" => array()
    );

    $proj_id = $row["project_id"];
    $projects[] = $details;
    $info = array();

}

if ($room_id != $row["room_id"]) {

        $info = array(
                "room_id" => $row["room_id"],
                "room_name" => $row["room_name"],
                "room_type" => $row["room_type"],
                "comment_extra_details" => $row["comment_extra_details"],
                "caption" => $row["caption"],
                "feats" => array(),
                "images" => array()
        );

        $room_id = $row["room_id"];
        $rooms[] = $info;
        $feat = array();
    $counter = sizeof($projects) - 1;
    $projects[$counter]["rooms"] = $info;


}

//This is giving me the trouble
//if ($prop_id != $row["prop_id"]) {

//  $feat = array(
//    "feature_name" => $row["feature_name"]
//);

//$prop_id = $row["prop_id"];
//$features[] = $feat;

//$projects[$counter]["rooms"]["feats"] = $features;

//}


else if ($file_id != $row["file_id"]) {

    $img[] = array(
        "filename" => $row["filename"],
        "filetype" => $row["filetype"],
        "file_id" => $row["file_id"],
        "public_name" => $row["public_name"]
    );

    $file_id = $row["file_id"];
    $images = $img;

    $projects[$counter]["rooms"]["images"] = $images;
}

}

输出:

My Semi Desired Output

当我在那里抛出这个代码块时,它循环遍历我的特征行

if ($prop_id != $row["prop_id"]) {

  $feat = array(
    "feature_name" => $row["feature_name"]
);

$prop_id = $row["prop_id"];
$features[] = $feat;

$projects[$counter]["rooms"]["feats"] = $features;

}

我得到了这个不需要的输出,我只能假设是由于逻辑流和循环结束:

The bad...

现在我知道肯定没有15个功能,有5个和3个图像,但我似乎无法隔离其中一个或另一个没有功能或图像以某种方式破坏。

编辑 - 我已经包含了查询返回的表数据的可视化,我希望这会有所帮助。

+---------+---------------+------------+---------------------+---------------------+-------+------------------+-------------------+------------+---------+-----------+-------------+---------+-----------------------+--------------+---------+-----------------------------------------------+------------+---------+-------------+
| user_id | user_username | project_id |    created_date     |    closing_date     | prize |   project_desc   |   project_title   |   state    | room_id | room_name |  room_type  | prop_id | comment_extra_details | feature_name | caption |                   filename                    |  filetype  | file_id | public_name |
+---------+---------------+------------+---------------------+---------------------+-------+------------------+-------------------+------------+---------+-----------+-------------+---------+-----------------------+--------------+---------+-----------------------------------------------+------------+---------+-------------+
|      10 | CalebB        |         27 | 2016-04-06 21:38:00 | 2016-04-27 21:38:00 |  5500 | heres some janks | Just another test | qualifying |      26 | NULL      | Family Room |      42 | NULL                  | Decor        |         | b59b3bb7cac73e8cbba0f68795aba254_1459993038.p | image/png  |      38 | NULL        |
|      10 | CalebB        |         27 | 2016-04-06 21:38:00 | 2016-04-27 21:38:00 |  5500 | heres some janks | Just another test | qualifying |      26 | NULL      | Family Room |      43 | NULL                  | Furniture    |         | b59b3bb7cac73e8cbba0f68795aba254_1459993038.p | image/png  |      38 | NULL        |
|      10 | CalebB        |         27 | 2016-04-06 21:38:00 | 2016-04-27 21:38:00 |  5500 | heres some janks | Just another test | qualifying |      26 | NULL      | Family Room |      44 | NULL                  | Paint        |         | b59b3bb7cac73e8cbba0f68795aba254_1459993038.p | image/png  |      38 | NULL        |
|      10 | CalebB        |         27 | 2016-04-06 21:38:00 | 2016-04-27 21:38:00 |  5500 | heres some janks | Just another test | qualifying |      26 | NULL      | Family Room |      45 | NULL                  | Flooring     |         | b59b3bb7cac73e8cbba0f68795aba254_1459993038.p | image/png  |      38 | NULL        |
|      10 | CalebB        |         27 | 2016-04-06 21:38:00 | 2016-04-27 21:38:00 |  5500 | heres some janks | Just another test | qualifying |      26 | NULL      | Family Room |      46 | NULL                  | Apolstery    |         | b59b3bb7cac73e8cbba0f68795aba254_1459993038.p | image/png  |      38 | NULL        |
|      10 | CalebB        |         27 | 2016-04-06 21:38:00 | 2016-04-27 21:38:00 |  5500 | heres some janks | Just another test | qualifying |      26 | NULL      | Family Room |      42 | NULL                  | Decor        |         | 21c3b842752b3866a37912048e6d0bbd_1459993053.j | image/jpeg |      39 | NULL        |
|      10 | CalebB        |         27 | 2016-04-06 21:38:00 | 2016-04-27 21:38:00 |  5500 | heres some janks | Just another test | qualifying |      26 | NULL      | Family Room |      43 | NULL                  | Furniture    |         | 21c3b842752b3866a37912048e6d0bbd_1459993053.j | image/jpeg |      39 | NULL        |
|      10 | CalebB        |         27 | 2016-04-06 21:38:00 | 2016-04-27 21:38:00 |  5500 | heres some janks | Just another test | qualifying |      26 | NULL      | Family Room |      44 | NULL                  | Paint        |         | 21c3b842752b3866a37912048e6d0bbd_1459993053.j | image/jpeg |      39 | NULL        |
|      10 | CalebB        |         27 | 2016-04-06 21:38:00 | 2016-04-27 21:38:00 |  5500 | heres some janks | Just another test | qualifying |      26 | NULL      | Family Room |      45 | NULL                  | Flooring     |         | 21c3b842752b3866a37912048e6d0bbd_1459993053.j | image/jpeg |      39 | NULL        |
|      10 | CalebB        |         27 | 2016-04-06 21:38:00 | 2016-04-27 21:38:00 |  5500 | heres some janks | Just another test | qualifying |      26 | NULL      | Family Room |      46 | NULL                  | Apolstery    |         | 21c3b842752b3866a37912048e6d0bbd_1459993053.j | image/jpeg |      39 | NULL        |
|      10 | CalebB        |         27 | 2016-04-06 21:38:00 | 2016-04-27 21:38:00 |  5500 | heres some janks | Just another test | qualifying |      26 | NULL      | Family Room |      42 | NULL                  | Decor        |         | 5b4645c9ee88704eae9a12e422d86ba2_1459993063.j | image/jpeg |      40 | NULL        |
|      10 | CalebB        |         27 | 2016-04-06 21:38:00 | 2016-04-27 21:38:00 |  5500 | heres some janks | Just another test | qualifying |      26 | NULL      | Family Room |      43 | NULL                  | Furniture    |         | 5b4645c9ee88704eae9a12e422d86ba2_1459993063.j | image/jpeg |      40 | NULL        |
|      10 | CalebB        |         27 | 2016-04-06 21:38:00 | 2016-04-27 21:38:00 |  5500 | heres some janks | Just another test | qualifying |      26 | NULL      | Family Room |      44 | NULL                  | Paint        |         | 5b4645c9ee88704eae9a12e422d86ba2_1459993063.j | image/jpeg |      40 | NULL        |
|      10 | CalebB        |         27 | 2016-04-06 21:38:00 | 2016-04-27 21:38:00 |  5500 | heres some janks | Just another test | qualifying |      26 | NULL      | Family Room |      45 | NULL                  | Flooring     |         | 5b4645c9ee88704eae9a12e422d86ba2_1459993063.j | image/jpeg |      40 | NULL        |
|      10 | CalebB        |         27 | 2016-04-06 21:38:00 | 2016-04-27 21:38:00 |  5500 | heres some janks | Just another test | qualifying |      26 | NULL      | Family Room |      46 | NULL                  | Apolstery    |         | 5b4645c9ee88704eae9a12e422d86ba2_1459993063.j | image/jpeg |      40 | NULL        |
+---------+---------------+------------+---------------------+---------------------+-------+------------------+-------------------+------------+---------+-----------+-------------+---------+-----------------------+--------------+---------+-----------------------------------------------+------------+---------+-------------+

非常感谢这里的任何帮助或正确方向上的一点

3 个答案:

答案 0 :(得分:0)

由于我无法发表评论,我会说我怀疑你是否有笛卡尔联盟。

构成关系的那些表中可能还有其他标识符。

例如,您似乎获得了该会议室中所有项目的所有项目属性,而我假设您只想要与该用户ID相关联的项目。

添加表格结构以获得更好的帮助

编辑: 看到表结构后,您可能需要在连接中包含一个附加表。但是要调试它,您可以开始在选择中包含更多数据并查看结果,直到找到您必须在作业条件中使用的问题列。

现在试一试:

    FROM users AS u
Inner join project_submissions as ps on
u.user_id = ps.user_id

INNER JOIN projects as pr ON u.user_id = pr.creator_id
And pr.project_id = ps.project_id
 INNER JOIN project_rooms as prr ON prr.project_id = pr.project_id
INNER JOIN project_properties as prp ON prp.room_id = prr.room_id
INNER JOIN project_files as prf ON prf.room_id = prp.room_id

答案 1 :(得分:0)

您房间的功能阵列似乎没有明确定义。 $projects[$counter]["rooms"]["feats"].

我会在$ projects之后设置它[$ counter] [" rooms"] = $ info;

$projects[$counter]["rooms"] = $info;
$projects[$counter]["rooms"]["feats"] = array();

然后在道具分支中使用它

if ($prop_id != $row["prop_id"]) {

  $projects[$counter]["rooms"]["feats"][] = array(
    "feature_name" => $row["feature_name"]
  );

  $prop_id = $row["prop_id"];
}

答案 2 :(得分:0)

感谢@ user3802077和@jeff,我终于隔离了我的代码,得到了我想要的回报。

这是结果代码:

$sqlQuery = "
SELECT 
     u.user_id, 
     u.user_username, 
     pr.project_id, 
     pr.created_date, 
     pr.closing_date, 
     pr.prize, 
     pr.project_desc, 
     pr.project_title, 
     pr.state, 
     prr.room_id, 
     prr.room_name, 
     prr.room_type, 
     prp.prop_id, 
     prp.comment_extra_details,
     prp.feature_name 
FROM users AS u 
     INNER JOIN projects as pr ON u.user_id = pr.creator_id 
     INNER JOIN project_rooms as prr ON prr.project_id = pr.project_id 
     INNER JOIN project_properties as prp ON prp.room_id = prr.room_id 
WHERE 
     state = ? 
ORDER BY 
     pr.created_date DESC";



$result = $conn->prepare($sqlQuery);
$result->execute(array('qualifying'));

$proj_id = 0;
$room_id = 0;
$file_id = 0;

$projects = array();
$rooms = array();
$files = array();
$properties = array();
$images = array();

while ($row = $result->fetch(PDO::FETCH_ASSOC)) {

    if ($proj_id != $row["project_id"]) {
        $rooms = array();
        $files = array();
        $properties = array();
        $projects[] = array(
            "user_id" => $row["user_id"],
            "username" => $row["user_username"],
            "project_id" => $row["project_id"],
            "created_date" => $row["created_date"],
            "closing_date" => $row["closing_date"],
            "prize" => $row["prize"],
            "project_desc" => $row["project_desc"],
            "project_title" => $row["project_title"],
            "rooms" => array()
        );

    $rooms[] = array(
        "room_id" => $row["room_id"],
        "room_name" => $row["room_name"],
        "room_type" => $row["room_type"],
        "comment_extra_details" => $row["comment_extra_details"],
        "files" => array(),
        "properties" => array()
    );

    $rooms[0]["properties"][] = array(
        "feature_name" => $row["feature_name"]
    );

    $f = "SELECT * FROM project_files where room_id = ?";
    $result2 = $conn->prepare($f);
    $result2->execute(array($row['room_id']));



    while ($rowF = $result2->fetch(PDO::FETCH_ASSOC)) {

        $rooms[0]['files'][] = array(
            "filename" => $rowF["filename"],
            "filetype" => $rowF["filetype"],
            "file_id" => $rowF["file_id"],
            "caption" => $rowF["caption"],
            "public_name" => $rowF["public_name"]
        );

    }

    $proj_id = $row["project_id"];
    $counter = sizeof($projects) - 1;
    $projects[$counter]["rooms"] = $rooms;

    } else {

        $rooms[0]["properties"][] = array(
            "feature_name" => $row["feature_name"]
        );
        $counter = sizeof($projects) - 1;
        $projects[$counter]["rooms"] = $rooms;
    }
}