简体   繁体   中英

Select multiple rows from multiple tables and print all withe php while loop

i have 3 table T1, T2, T3,
so, in T1 i want to select 1st row and select from T2 the rows related to the row selected in T1, and select from T3 rows related to T2 how have relation with row selected in T1 and print all this in one continer withe php while loop function see the images for more descriptions

IMG1

IMG2

Solved and this is the code i am happy :)

    $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   
}
?>

Base on what I understand with your question. This could be the answer

<?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>";
        }
    }
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM