使用数组中的foreach显示许多行

时间:2016-01-14 14:08:56

标签: php foreach

我试图做那样的事情,但它不起作用。

$ resDisplayFormation是一个返回很多行的查询,我需要显示我的表中的所有行,这个文件是一个由ajax函数调用的文件,这就是为什么我必须像我那样做我的foreach

更新:

<?php 

include('opendb2.php');
if(isset($_POST['idSalarie'])){ 
$displayFormation = $bdd->prepare('SELECT * FROM FORMATION WHERE form_id_user = :idSalarie ');
$displayFormation->bindParam(':idSalarie', $_POST['idSalarie']);
$displayFormation->execute();
$resDisplayFormation=$displayFormation->fetch(PDO::FETCH_ASSOC);

$displayForm = $bdd->prepare(
    'SELECT poste_nom, ups_type_contrat, serv_nom, serv_id_resp, user_credit_cpf, user_indice_salarial, form_intitule, form_organisme, form_date, form_duree,
     FLOOR( DATEDIFF( CURDATE( ) , user_dateentree ) /365 ) AS dateEntree 
     FROM USER 
     INNER JOIN USER_POSTE_SERVICE 
        ON USER.user_id= USER_POSTE_SERVICE.ups_poste_id  
     INNER JOIN POSTE 
        ON USER_POSTE_SERVICE. ups_poste_id = POSTE.poste_id 
     INNER JOIN SERVICE 
        ON USER_POSTE_SERVICE.ups_id_serv = SERVICE.serv_id
     INNER JOIN FORMATION
        ON FORMATION.form_id_user =  USER.user_id
     WHERE user_id = :idSalarie
        ORDER BY user_nom ASC');
$displayForm->bindParam(':idSalarie', $_POST['idSalarie']);
$displayForm->execute();
$resDisplayForm=$displayForm->fetch(PDO::FETCH_ASSOC);
$data = array( 'resDisplayForm'=>'');
foreach ($resDisplayForm as $key => $value) {
    $data['resDisplayForm'][$key] .=  $value;
}
$data['salarie'] = '          
    <div class="form-group">
        <label for="poste_nom" class="col-sm-2 control-label">Poste occupé</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="poste_nom" readonly>
        </div>
    </div> 
    <div class="form-group">
        <label for="ups_type_contrat" class="col-sm-2 control-label">Type de contrat</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="ups_type_contrat" readonly>
        </div>
    </div> 
    <div class="form-group">
        <label for="serv_nom" class="col-sm-2 control-label" readonly>Service</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="serv_nom" readonly>
        </div>
    </div> 
    <div class="form-group">
        <label for="serv_id_resp" class="col-sm-2 control-label" readonly>Responsable</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="serv_id_resp" readonly>
        </div>
    </div>
    <div class="form-group">
        <label for="user_credit_cpf" class="col-sm-2 control-label" readonly>Crédits CPF</label>
        <div class="col-sm-2">
            <input type="text" class="form-control" name="user_credit_cpf" readonly>
        </div>
        <label for="user_indice_salarial" class="col-sm-2 control-label" readonly>Indice Salarial</label>
        <div class="col-sm-1">
            <input type="text" class="form-control" name="user_indice_salarial" readonly>
        </div>
        <label for="dateEntree" class="col-sm-2 control-label">Ancienneté</label>
        <div class="col-sm-2 input-group">
            <input type="text" class="form-control" name="dateEntree" aria-describedby="basic-addon2" readonly>
            <span class="input-group-addon" id="basic-addon2">années</span>
        </div>
    </div>';

$data['formation'] .= '
    <div class="table-responsive">
        <table class="table table-bordered" style="width: auto !important;">
         <thead style="font-weight: bold;">’’
           <tr>
              <td>N</td>
              <td>Intitulé</td>
              <td>Organisme</td>
              <td>Date</td>
              <td>Durée (en heures)</td>
              <td>Eval. à chaud / à froid</td>
              <td>Dispositif utilisé</td>
            </tr>
          </thead>
          <tbody class="table-striped">';

            foreach ($resDisplayFormation as $ligne) {
            $data['formation'] .= '
            <tr>
                <td>

                </td>
                <td>
                    <input type="text" class="form-control" name="form_intitule" id="form_intitule" readonly>
                </td>
                <td>
                    <input type="text" class="form-control" name="form_organisme" id="form_organisme" readonly>
                </td>
                <td>
                    <input type="text" class="form-control" name="form_date" id="form_date" readonly>
                </td>
                <td>
                    <input type="text" class="form-control" name="form_duree" id="form_duree" readonly>
                </td>
                <td></td>
                <td></td>
            </tr>';
            }
            $data['formation'] .= '

          </tbody>
        </table>
    </div>';
}

echo json_encode($data);die;
?>

我的AJAX:

jQuery(document).ready(function($) {
$('#idSalarie').change(function(){
    //on recupere la valeur de l'attribut value pour afficher tel ou tel resultat
    var req=$('#idSalarie').val();
    //requête ajax, appel du fichier function.php
    $.ajax({
        type: "POST",
        url: "lib/function.php",
        data: "idSalarie="+req,
        dataType : "json",
        //affichage de l'erreur en cas de problème
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest + '--' + textStatus + '--' + errorThrown);
        },
        //function s'il n'y a pas de probleme
        success:function(data){
            //On affiche la réponse du serveur
            $('.salarie').empty();
            $('.salarie').prepend(data.salarie);
            $('input[name=poste_nom]').val(data.resDisplayForm.poste_nom).val();
            $('input[name=ups_type_contrat]').val(data.resDisplayForm.ups_type_contrat).val();
            $('input[name=serv_nom]').val(data.resDisplayForm.serv_nom).val();
            $('input[name=serv_id_resp]').val(data.resDisplayForm.serv_id_resp).val();
            $('input[name=user_credit_cpf]').val(data.resDisplayForm.user_credit_cpf).val();
            $('input[name=dateEntree]').val(data.resDisplayForm.dateEntree).val();
            $('input[name=user_indice_salarial]').val(data.resDisplayForm.user_indice_salarial).val();

            $('.formation').empty();
            $('.formation').prepend(data.formation)
            $('input[name=form_id]').val(data.resDisplayForm.form_id).val();
            $('input[name=form_intitule]').val(data.resDisplayForm.form_intitule).val();
            $('input[name=form_organisme]').val(data.resDisplayForm.form_organisme).val();
            $('input[name=form_date]').val(data.resDisplayForm.form_date).val();
            $('input[name=form_duree]').val(data.resDisplayForm.form_duree).val();


        }
    });
});
});  

2 个答案:

答案 0 :(得分:0)

在foreach内部而不是

$data['formation'] = '

你必须写

$data['formation'] .= '

并且在foreach之后你还必须写

$data['formation'] .= '

这将在正确的HTML表格中连接所有行

更新

在这部分代码中,你没有从$ ligne param获得任何值,也不会在表格单元格中写入它。这就是为什么你会得到6个相同的行

foreach ($resDisplayFormation as $ligne) {
            $data['formation'] .= '
            <tr>
                <td>

                </td>
                <td>
                    <input type="text" class="form-control" name="form_intitule" id="form_intitule" readonly>
                </td>
                <td>
                    <input type="text" class="form-control" name="form_organisme" id="form_organisme" readonly>
                </td>
                <td>
                    <input type="text" class="form-control" name="form_date" id="form_date" readonly>
                </td>
                <td>
                    <input type="text" class="form-control" name="form_duree" id="form_duree" readonly>
                </td>
                <td></td>
                <td></td>
            </tr>';
            }

应该像这样

foreach ($resDisplayFormation as $ligne) {
            $data['formation'] .= '
            <tr>
                <td>

                </td>
                <td>
                    <input type="text" class="form-control" name="form_intitule" id="form_intitule" value='.$ligne["form_intitule"].' readonly>
                </td>
                <td>
                    <input type="text" class="form-control" name="form_organisme" id="form_organisme" value='.$ligne["form_organisme"].' readonly>
                </td>
                <td>
                    <input type="text" class="form-control" name="form_date" id="form_date" value='.$ligne["form_date"].' readonly>
                </td>
                <td>
                    <input type="text" class="form-control" name="form_duree" id="form_duree" value='.$ligne["form_duree"].' readonly>
                </td>
                <td></td>
                <td></td>
            </tr>';
            }

答案 1 :(得分:0)

    $data['formation'] .= '
    <div class="table-responsive">
        <table class="table table-bordered" style="width: auto !important;">
         <thead style="font-weight: bold;">’’
           <tr>
              <td>N</td>
              <td>Intitulé</td>
              <td>Organisme</td>
              <td>Date</td>
              <td>Durée (en heures)</td>
              <td>Eval. à chaud / à froid</td>
              <td>Dispositif utilisé</td>
            </tr>
          </thead>
          <tbody class="table-striped">';

            foreach ($resDisplayFormation as $ligne) {
            $data['formation'] .= '
            <tr>
                <td>

                </td>
                <td>
                    <input type="text" class="form-control" name="form_intitule" id="form_intitule" value="'.$ligne['form_intitule'].'" readonly>
                </td>
                <td>
                    <input type="text" class="form-control" name="form_organisme" id="form_organisme" value="'.$ligne['form_organisme'].'" readonly>
                </td>
                <td>
                    <input type="text" class="form-control" name="form_date" id="form_date" value="'.$ligne['form_date'].'" readonly>
                </td>
                <td>
                    <input type="text" class="form-control" name="form_duree" id="form_duree" value="'.$ligne['form_duree'].'" readonly>
                </td>
                <td></td>
                <td></td>
            </tr>';
            }
            $data['formation'] .= '

          </tbody>
        </table>
    </div>';
}

echo json_encode($data); die;