0

I have a table in MySql table with many rows of records (existingbankproducts table):

enter image description here

The code I use to select is from the database is below:

$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
}

I want to select them and insert to my HTML table, the code is below:

<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>

Actually, there are more rows, this is an example.

Also, I put value="<?php echo $row['Financialinstitution'] " ?> as an example, However, all the records are coming out.

Is there any way to display the result according to the HTML table in order.

2
  • try to provide only shortest possible required code instead of ctrl+a & ctrl+v. Commented Jul 11, 2017 at 5:59
  • use order by clause in your select query, to get the sorted output and loop over it. Commented Jul 11, 2017 at 6:00

2 Answers 2

1

1st : You need to loop the record set like this

2nd : your input value should filled with right column like this

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

Note : you need to echo the each column desired td input . i have echo only one column

3rd : using prepared statement is good . as well you need to use bindparam . like this

$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
}
Sign up to request clarification or add additional context in comments.

Comments

0

Whatever i understand from your question you can look like below:

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
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.