从多个表中选择多行,并在while循环中使用php打印所有内容

时间:2018-09-16 02:16:56

标签: php mysql

我有3个表T1,T2,T3,
因此,在T1中,我想选择第一行,并从T2中选择与T1中选择的行相关的行,然后从T3中与T2相关的行中选择与T1中选择的行有何关系,并使用php将其打印在一个Continer中循环功能 查看图片以获取更多说明

img1

Img2

2 个答案:

答案 0 :(得分:0)

解决了,这是我很高兴的代码:)

    $ReqFindRows = "SELECT * FROM commandes WHERE commande_status = 0 AND date(commande_le) = CURDATE()";
$STMTFindRows = $connect->stmt_init();
if(!$STMTFindRows->prepare($ReqFindRows)){
echo "No Results";
}
else {
$STMTFindRows->execute();
$ResultsFindRows = $STMTFindRows->get_result();
$x = 0;
while($ArrayCat = $ResultsFindRows->fetch_assoc()){ 
switch($ArrayCat['commande_importance']){
case 0 : 
$importance = 'Normal';
$bgclas = "bg-blue";
break;
case 1 : 
$importance = 'Urgent';
$bgclas = "bg-yellow";
break;
case 2 : 
$importance = 'Immediat';
$bgclas = "bg-red";
break;
};
$prods = array();   
$GetProductsInCommandeQuery = "SELECT * FROM commandes_produits WHERE commande_id = ?";             
$GetProductsInCommandeSTMT = $connect->stmt_init();
if(!$GetProductsInCommandeSTMT->prepare($GetProductsInCommandeQuery)){
echo $connect->error;
}
else {
$id_toserch = $ArrayCat["commande_id"];
$GetProductsInCommandeSTMT->bind_param("s", $id_toserch );
$GetProductsInCommandeSTMT->execute();
$GetProductsInCommandeResults = $GetProductsInCommandeSTMT->get_result();
$Prodss = "";
while($GetProductsInCommandeArray = $GetProductsInCommandeResults->fetch_array()){
$prods = $GetProductsInCommandeArray["recette_id"];
$GetSupQuery = "SELECT * FROM commandes_supp WHERE tr_number = ? AND commande_id = ?";
$GetSupSTMT = $connect->stmt_init();
if(!$GetSupSTMT->prepare($GetSupQuery)){
echo $connect->error;
}
else {
$trNumber = $GetProductsInCommandeArray["tr_number"];
$GetSupSTMT->bind_param("ss", $trNumber, $id_toserch );
$GetSupSTMT->execute();
$GetSupResults = $GetSupSTMT->get_result();
$Supp = "";
while($GetSupArray = $GetSupResults->fetch_array()){
$Suppss = $GetSupArray["supp_id"];
$Supp .= '<p style="color:#F00">'.$Suppss.' </p>';
}
}
$Prodss .=  
'<tr>
<td>
'.$prods.'
'.$Supp.'
</td>
<td>
A table
</td>
<td>
<button type="button" class="btn btn-sm">Servis</button>
</td>
</tr>   ';                                  
}
}                   
?>
                    <div class="col-xs-6 col-md-4" style="margin-bottom:10px;">
                      <div class="tiket">
                        <div class="tikeheader <?php echo $bgclas ?>">
                          <span class="commandenumber">
                            <i class="fa fa-star iconheader">
                            </i>
                            <?php echo $ArrayCat["commande_id"] ?> A Table
                          </span>
                          <span class="importance">
                            <?php echo $importance?>
                          </span>
                        </div>
                        <div class="tiketbody">
                          <table  class="tiketbodytable">
                            <tbody>
                              <?php echo $Prodss ?>
                            </tbody>
                          </table>
                        </div>
                      </div>
                    </div>  
                    <?php               
}
}
?>                      
                  </div>
                </div>
              </div>
            </div>
            <?php   
}
?>

答案 1 :(得分:0)

根据我对您的问题的理解。这可能是答案

<?php
$db = new PDO("mysql:host=localhost", "username", "password");

$q1 = $db->prepare("SELECT * FROM T1");
$q1->execute();
$f1 = $q1->fetchAll(PDO::FETCH_ASSOC);
foreach ($f1 as $f1_items) {
    echo "<tr><td>".$f1['id_cat']."</td></tr>";
    $q2 = $db->prepare("SELECT * FROM T2 WHERE id_cat = ".$f1['id_cat'].";");
    $q2->execute();
    $f2 = $q2->fetchAll(PDO::FETCH_ASSOC);
    foreach ($f2 as $f2_items) {
        echo "<tr><td>\t".$f2['id_tr']."</td></tr>";
        $q3 = $db->prepare("SELECT * FROM T3 WHERE id_tr = ".$f2['id_cus'].";");
        $q3->execute();
        $f3 = $q3->fetchAll(PDO::FETCH_ASSOC);
        foreach ($f3 as $f3_items) {
            echo "<tr><td>\t\t".$f2['id_cus']."</td></tr>";
        }
    }
}
?>