1
\$\begingroup\$

I need to print the many-to-many relation ship in my tablet. However, I don't know what to do for my case: I tried tons of ideas, however, I may have done something wrong because nothing worked.

Here is the table printed (the last one Surfactante I put the Marca result, however, Surfactante is in a N x N relationship with Product with a table of theirs on produto_surfactante with (codProd, codSurf)):

enter image description here

Here is the function I am using to print (one of the possibilities suggested the relation of a View table, so I kept part of the idea here):

public function dataviewProd($query)
{
    $stmt = $this->conn->prepare($query);
    $stmt->execute();

    if($stmt->rowCount()>0)
    {
        while($row=$stmt->fetch(PDO::FETCH_ASSOC))
        {
            //takes the name of "Marca"
            $codM = $row['codMarca'];
            $stmtM = $this->conn->prepare("SELECT nomeMarca FROM marca WHERE codMarca = :codM;"); 
            $stmtM->execute(array(":codM"=>$codM));
            $nomeMarca = $stmtM->fetch(PDO::FETCH_ASSOC)['nomeMarca'];

            ////takes the name of "Tipo de produto"
            $codTP = $row['codTipoProd'];
            $stmtTP = $this->conn->prepare("SELECT nomeTipoProd FROM tipodeproduto WHERE codTipoProd = :codTP;"); 
            $stmtTP->execute(array(":codTP"=>$codTP));
            $nomeTipoProd = $stmtTP->fetch(PDO::FETCH_ASSOC)['nomeTipoProd'];

            //this one should take the names of all "surfactantes" separated by ","
            $nomeProd = $row['nomeProd'];
            //$stmtPSV = $this->conn->prepare("SELECT s.nomeSurf
                                            //FROM produtosurfview psv
                                            //  INNER JOIN surfactante s
                                            //      ON psv.Surfactante = s.codSurf
                                            //WHERE psv.NomeProduto = {$nomeProd};");
            //$stmtPSV->execute(array(":nomeProd"=>$nomeProd));
            //$stmtPSV->setFetchMode(PDO::FETCH_ASSOC);
            //$prodsurf = $stmtPSV->fetch(PDO::FETCH_ASSOC);


            ?>
            <tr>
            <td><?php print($row['codProd']); ?></td>
            <td><?php print($row['nomeProd']); ?></td>
            <td><?php print($row['tipoCab']); ?></td>
            <td><?php print($nomeMarca); ?></td>
            <td><?php print($nomeTipoProd); ?></td>
            <td><?php print($nomeMarca); ?></td>
            <td align="center">
            <a class='btn btn-warning btn-sm' href="alterarProduto.php?edit_id=<?php print($row['codProd']); ?>" role='button'>
                <span class='glyphicon glyphicon-pencil' aria-hidden='true'></a>
            </td>
            <td align="center">
            <a class='btn btn-danger btn-sm' href="deletarProduto.php?delete_id=<?php print($row['codProd']); ?>" role='button'>
                <span class='glyphicon glyphicon-trash' aria-hidden='true'></span></a>
            </td>
            </tr>
            <?php
        }
    }
    else
    {
        ?>
        <tr>
        <td>Nada ainda cadastrado...</td>
        </tr>
        <?php
    }

}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.