Php 在数组中使用foreach显示多行
我正试着做那样的事,但没用 $resDisplayFormation是一个返回许多行的查询,我需要显示表中的所有行,这个文件是一个由ajax函数调用的文件,这就是为什么我必须这样做的原因 更新:Php 在数组中使用foreach显示多行,php,foreach,Php,Foreach,我正试着做那样的事,但没用 $resDisplayFormation是一个返回许多行的查询,我需要显示表中的所有行,这个文件是一个由ajax函数调用的文件,这就是为什么我必须这样做的原因 更新: <?php include('opendb2.php'); if(isset($_POST['idSalarie'])){ $displayFormation = $bdd->prepare('SELECT * FROM FORMATION WHERE form_id_user = :
<?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;
?>
用内部foreach代替
$data['formation'] = '
你必须写作
$data['formation'] .= '
$data['formation'] .= '
每次之后,你也必须写作
$data['formation'] .= '
$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作为$ligne){
$data['formation'].='
';
}
应该是这样的
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>';
}
foreach($resDisplayFormation作为$ligne){
$data['formation'].='
';
}
$data['formation'].='
’’
N
因蒂图雷
有机物
日期
杜雷(欧洲)
评估。乔德/弗罗伊德
利用权处置
';
foreach($resDisplayFormation作为$ligne){
$data['formation'].='
';
}
$data['formation'].='
';
}
echo json_编码($data);死亡
快速浏览一下,您可能想要$data['formation']。='
而不是$data['formation']='
在您的foreach
循环内和循环后。我编辑了我的代码,但它显示了同一行的6倍。如果在foreach
中不使用变量,您希望得到不同的行吗?你认为什么会使行不同?对于每一行,我需要重复我知道的是,但我需要重复我的所有输入,这是我的问题^^^除了主要问题之外,还有一个小提示:目前编写代码的方式,您会多次(每次)获得相同的id,这违反了标准,在根据javascriptwhat you want a screen或my html选择元素时可能会导致意外行为?我想在执行“echo json_encode($data);die”之前在“$data['formation']”变量中查看您的html;我已经编辑了我的帖子,您可以看到ajax函数,html只是两个div(formation和salarie)