使用两个查询的结果挂载阵列

时间:2018-07-19 01:26:51

标签: php arrays

我正在设置一个在表菜单中执行查询的函数,将查询的结果传递给数组,然后对该数组进行foreach。在此foreach内部,我使用包含在数组['id']中的ID对表项进行第二次查询,以使所有项均引用每个菜单。到目前为止,一切都很好。我需要将所有这些结果放入数组中。但是我不能。以下是我正在执行的功能,但是我停止在阵列的安装部分。

public function relatorioCardapio($seg="",$tr="",$data=""){

    $array = array();


    $sql = "SELECT c.id id,  s.seguimento seguimento, tr.refeicao refeicao, c.data data FROM
    cardapio c JOIN seguimento AS s ON s.id = c.id_seguimento JOIN tipo_refeicao AS tr
    ON tr.id = c.id_tipo WHERE ";

    $where = array();
    $where[] = "c.id_seguimento = :seg";

    if (!empty($tr)) {
        $where[] = "c.id_tipo = :tr";
    }
    if(!empty($data)){
        $where[] = "c.data = :data";
    }

    $sql .= implode(' AND ', $where);
    //print_r($sql);
    //exit();

    $sql = $this->db->prepare($sql);
    $sql->bindValue(":seg", $seg);  



    if(!empty($tr))
    {
        $sql->bindValue(":tr", $tr);



    }
    if(!empty($data)){

        $sql->bindValue(":data", $data);
    }

    $sql->execute();

    if($sql->rowCount() > 0){

        if($sql->rowCount() > 0){
            $array = $sql->fetchAll();
            foreach ($array as $i)
             {

                $sql = $this->db->prepare("SELECT i.item FROM cardapio c JOIN cad_item AS ci ON ci.id_cardapio = c.id
                                           JOIN item AS i ON i.id = ci.id_item WHERE ci.id_cardapio = :id");
                $sql->bindValue(":id", $i['id']);
                $sql->execute();

                if($sql->rowCount() > 0)
                {
                    $array['item'] = $sql->fetchAll();
                }

            }
            //print_r($array['item']);
            //exit();
        }

    }
    return $array;
}

0 个答案:

没有答案