【问题标题】:Using loop to select data and display in div MySql php使用循环选择数据并在 div MySql php 中显示
【发布时间】:2017-07-11 05:57:13
【问题描述】:

我在 MySql 表中有一个包含多行记录的表(existingbankproducts 表):

我用来选择的代码来自数据库如下:

$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="&lt;?php echo $row['Financialinstitution'] " ?&gt; 为例,但是,所有的记录都出来了。

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

【问题讨论】:

  • 尽量只提供最短的所需代码,而不是 ctrl+a & ctrl+v。
  • 在您的选择查询中使用 order by 子句,以获取排序后的输出并对其进行循环。

标签: php html mysql sql database


【解决方案1】:

第一个:你需要像这样循环记录集

第二个:你的输入值应该用这样的右列填充

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

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

第三:使用准备好的语句是好的。您还需要使用 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
}

【讨论】:

    【解决方案2】:

    无论我从您的问题中了解到什么,您都可以如下所示:

    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
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-17
      • 1970-01-01
      • 1970-01-01
      • 2015-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多