虽然循环不适用于PHP

时间:2015-01-28 16:02:30

标签: php mysql while-loop

<?php

if (!($database = mysqli_connect("localhost", "root", "","gem"))) {                           
    die( "Connexion non possible à la base de données." );
}

$requete = "SELECT nom_eta FROM etablissements ";
$query = "SELECT * FROM formations";

if ( !( $result = mysqli_query($database, $requete) ) OR !( $resulta = mysqli_query($database, $query) )) {
    print( "Impossible d'exécuter la requête! <br />" );
    die( mysqli_error($database) );
}

while ($row = mysqli_fetch_assoc($result) ) {
    $data=$row;
?>
    <a href="#"><?php echo $data['nom_eta'];?></a>
<?php
    while ($ligne = mysqli_fetch_assoc($resulta) ){
        $donnee=$ligne;
        if($data['nom_eta']==$donnee['nom_eta']) {
        ?>
            <ul>
                <a href="#"><?php echo $donnee['formation'];?></a>
            </ul>
        <?php
        }
    } 
}
?>

Myproblem是,还有其他数据应显示在ENSAT&amp; ENCGT但是在重新执行WHILE-LOOP时存在某种问题..

1 个答案:

答案 0 :(得分:0)

你不应该嵌套whiles,而是先运行第二个并将结果分配给一个数组,然后你可以在另一个内部循环数组

<?php
if ( !( $database = mysqli_connect( "localhost","root", "","gem")))                           
  die( "Connexion non possible à la base de données." );
$requete = "SELECT nom_eta FROM etablissements ";
$query = "SELECT * FROM formations";
if ( !( $result = mysqli_query($database, $requete) ) OR !( $resulta = mysqli_query($database, $query) )) {
  print( "Impossible d'exécuter la requête! <br />" );
  die( mysqli_error($database) );
}

$ligne = array();
while ($row = mysqli_fetch_assoc($resulta)) {
  $ligne[] = $row;
}

while ($row = mysqli_fetch_assoc($result) ){
  $data=$row;
  ?><a href="#"><?php echo $data['nom_eta'];?></a><?php

  foreach ($ligne as $donee) {

    if($data['nom_eta']==$donnee['nom_eta']){
      ?><ul><a href="#"><?php echo $donnee['formation'];?></a><?php ?></ul><?php
    }
  } 
}
?>