2017-07-11 88 views
0

我在MySQL表一表的记录许多行(existingbankproducts表):使用循环来选择数据和DIV MySql的PHP显示

enter image description here

我用它来选择的代码是从数据库低于:

$stmt2 = $DB_con->prepare("SELECT * FROM applicantpersonaldetails apd " 
     . "INNER JOIN existingbankproducts ext ON apd.ApplicantID = ext.ApplicantID " 
     . "WHERE apd.AccountID ='{$accountId}' AND apd.applicantType ='main';"); 

$stmt2->execute(); 

if ($stmt2->rowCount() > 0) { 
    while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) { 



     ?> 
     <?php 
    } 
} else { 
    ?> 
    <div class=""> 
     <div class="alert alert-warning"> 
      <span class="glyphicon glyphicon-info-sign"></span> &nbsp; No Data Found ... 
     </div> 
    </div> 
    <?php 
} 

我想选择他们,并插入到我的HTML表格,代码如下:

<table> 
<tr> 
<th>Financial Institution</th> 
<th>Product Type</th> 
<th>Balance</th> 
<th>Monthly Commitment</th> 


</tr> 
<tr> 
<td><input type = "text" name = "finanIns1" id = "finanIns1" value = ""readonly></td> 
<td> 
<input list = "proTypeList" name = "proType1" id = "proType1"readonly > 

</td> 
<td id = "balance"><input type = "number" name = "balance1" id = "balance1" value = "" min = "0"readonly></td> 
<td id = "MonthyComm"><input type = "number" name = "monthlyComm1" id = "monthlyComm1" value = "" min = "0"readonly></td> 

</tr> 

<tr> 
<td><input type = "text" name = "finanIns2" id = "finanIns2" value = ""readonly></td> 
<td> 
<input list = "proTypeList" name = "proType2" id = "proType2" readonly> 

</td> 
<td id = "balance"><input type = "number" name = "balance2" id = "balance2" value = "" min = "0"readonly></td> 
<td id = "MonthyComm"><input type = "number" name = "monthlyComm2" id = "monthlyComm2" value = "" min = "0"readonly></td> 

</tr> 

</table> 

实际上,还有更多的行,这是一个例子。

而且,我把value="<?php echo $row['Financialinstitution'] " ?>作为一个例子,但是,所有的记录都出来了。

有没有什么办法按照HTML表的顺序显示结果。

+0

尝试只提供最短的所需的代码,而不是CTRL + A和CTRL + V。 –

+0

在您的select查询中使用order by子句,以获取排序后的输出并循环播放它。 – Gunnrryy

回答

1

1:您需要循环设置这样

第二记录:您输入的数值应该充满右列如此

<input type = "text" name = "finanIns1" id = "finanIns1" value="<?php echo $row['Financialinstitution']; ?>" readonly> 

注意:您需要回显每列desired td input。我只回显了一列

第3条:使用预备好的语句是好的。以及你需要使用bindparam。这样

$stmt2 = $DB_con->prepare("SELECT * FROM applicantpersonaldetails apd " 
     . "INNER JOIN existingbankproducts ext ON apd.ApplicantID = ext.ApplicantID " 
     . "WHERE apd.AccountID =:accountId AND apd.applicantType ='main';"); 

$stmt2->bindParam(':accountId', $accountId, PDO::PARAM_INT); 
//if account id data type is varchar change the last parameter to `PDO::PARAM_str` 
$stmt2->execute(); 

PHP:

if ($stmt2->rowCount() > 0) { 

    ?> 
    <table> 
    <tr> 
    <th>Financial Institution</th> 
    <th>Product Type</th> 
    <th>Balance</th> 
    <th>Monthly Commitment</th> 
    </tr> 
    <?php 


    while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) { 
    ?> 
    <tr> 
     <td><input type = "text" name = "finanIns1" id = "finanIns1" value="<?php echo $row['Financialinstitution']; ?>" readonly></td> 
     // like above td you need to echo all your data for following td 
     <td> 
     <input list = "proTypeList" name = "proType1" id = "proType1" readonly > 

     </td> 
     <td id = "balance"><input type = "number" name = "balance1" id = "balance1" value = "" min = "0"readonly></td> 
     <td id = "MonthyComm"><input type = "number" name = "monthlyComm1" id = "monthlyComm1" value = "" min = "0"readonly></td> 

    </tr> 

     <?php 
    } 
} else { 
    ?> 
    <div class=""> 
     <div class="alert alert-warning"> 
      <span class="glyphicon glyphicon-info-sign"></span> &nbsp; No Data Found ... 
     </div> 
    </div> 
    <?php 
} 
0

无论我从你的问题理解你可以像下面:

if ($stmt2->rowCount() > 0) { 
    while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) { 
?> 
<tr> 
<td><input type = "text" name = "finanIns1" id = "finanIns1" value = "<?php $row['columnName']?>" readonly></td> 
<td> 
<input list = "proTypeList" name = "proType1" id = "proType1"readonly > 

</td> 
<td id = "balance"><input type = "number" name = "balance1" id = "balance1" value = "<?php $row['columnName']?>" min = "0"readonly></td> 
<td id = "MonthyComm"><input type = "number" name = "monthlyComm1" id = "monthlyComm1" value = "" min = "0"readonly></td> 

</tr> 

<tr> 
<td><input type = "text" name = "finanIns2" id = "finanIns2" value = "<?php $row['columnName']?>" readonly></td> 
<td> 
<input list = "proTypeList" name = "proType2" id = "proType2" readonly> 

</td> 
<td id = "balance"><input type = "number" name = "balance2" id = "balance2" value = "<?php $row['columnName']?>" min = "0"readonly></td> 
<td id = "MonthyComm"><input type = "number" name = "monthlyComm2" id = "monthlyComm2" value = "<?php $row['columnName']?>" min = "0"readonly></td> 

</tr> 
<?php 


     ?> 
     <?php 
    }