Cecatrix
Cecatrix

Reputation: 151

PHP: Passing Foreach Data Values to Input Boxes while incrementing the number of their ID and Name

Hi guys I will summarize my problem here I hope you would understand.

Main Goal: My main Goal is to have a foreach of datas and pass it to their designated input boxes while, in the same way I'm appending the number of their ID and Name

Already Accomplished: I already have a forloop of x that can increment and pass their values as array in the input boxed ID and Name

HTML Code:

<table class="table table-bordered" id="addSubPaymentTable">
   <thead>
     <tr>
       <th>Particulars</th>
       <th>Balance</th>
       <th>Payment Amount</th>
   </thead>

   <tbody>
   <?php $arrayNumber = 0; for($x = 1; $x < 2; $x++) { ?> //This is where I make a loop
     that will increment x to supply diffrent number values to the same ID and Name that I
     have for my input boxes.

     <tr id="row<?php echo $x; ?>" class="<?php echo $arrayNumber; ?>"> //Increment Rows

       <td class="form-group">
         <input type="text" class="form-control" name="subparticulars[<?php echo $x; ?>]" id="subparticulars<?php echo $x; ?>" placeholder="Particulars" />     
       </td> //You will notice the x is inside my name and id of subparticulars.

       <td class="form-group">
         <input type="text" class="form-control" name="subpaymentbalance[<?php echo $x; ?>]" id="subpaymentbalance<?php echo $x; ?>" onclick="copyBalance(<?php echo $x; ?>)" placeholder="Balance" readonly />
       </td>

       <td class="form-group">
         <input type="text" class="form-control" name="subpaymentamount[<?php echo $x; ?>]" id="subpaymentamount<?php echo $x; ?>" onkeyup="calculateTotalAmount(); calculateRemainingBalance(<?php echo $x; ?>);" placeholder="Payment Amount" />
       </td>         

     </tr>

     <?php $arrayNumber++; } // end of foreach 
  ?>

  </tbody>
</table>

Screen Shot of Form enter image description here

Main Problem My Main problem is how I can possibly insert the values of my Data in for example you can use foreach ($DataValues as values) {} in the input boxes at the same time increment the ID and name number.

Purpose: The above code originated in an add row function that increments the rows when clicking add row, which is I needed to change for other goals which is I didn't need add row anymore and I have to transfer now all the values of for each instead of selecting the particulars, and it's a hassle to always add row to enter different particulars instead of showing it all and just add payment amounts.

Upvotes: 1

Views: 754

Answers (2)

liongrobert
liongrobert

Reputation: 88

Try this

`

<table class="table table-bordered" id="addSubPaymentTable">
   <thead>
     <tr>
       <th>Particulars</th>
       <th>Balance</th>
       <th>Payment Amount</th>
   </thead>
   <tbody>

   <?php $DataValues = [ 1111,2222,3333,4444 ]; // assuming This is the subparticulars datavalues;?> 
   <?php $arrayNumber = 0; for($x = 0; $x < count($DataValues); $x++) {?>

     <tr id="row<?php echo $x+1; ?>" class="<?php echo $x; ?>"> 

       <td class="form-group">
         <input type="text" class="form-control" name="subparticulars[<?php echo $x+1 ?>]" id="subparticulars<?php echo $x+1 ?>" placeholder="Particulars" value="<?php echo $DataValues[$x]?>" />     
       </td> 

       <td class="form-group">
         <input type="text" class="form-control" name="subpaymentbalance[<?php echo $x+1; ?>]" id="subpaymentbalance<?php echo $x+1; ?>" onclick="copyBalance(<?php echo $x; ?>)" placeholder="Balance" readonly />
       </td>

       <td class="form-group">
         <input type="text" class="form-control" name="subpaymentamount[<?php echo $x+1; ?>]" id="subpaymentamount<?php echo $x+1; ?>" onkeyup="calculateTotalAmount(); calculateRemainingBalance(<?php echo $x+1; ?>);" placeholder="Payment Amount" />
       </td>         

     </tr>
     <?php } ?>
  </tbody>
</table>

`

Upvotes: 0

Cecatrix
Cecatrix

Reputation: 151

The Answer for this Question:

Use foreach($studentfeesData as $x => $studentfees)

<table class="table table-bordered" id="addSubPaymentTable">
  <thead>
    <tr>
      <th style="width:36%;">Particulars</th>
      <th style="width:32%;">Balance</th>
      <th style="width:32%;">Payment Amount</th>      
  </thead>
  <tbody>
    <?php $arrayNumber = 0; foreach($studentfeesData as $x => $studentfees) { ?>
    <tr id="row<?php echo $x; ?>" class="<?php echo $arrayNumber; ?>">
      <td class="form-group">
        <select class="form-control" name="subparticulars[<?php echo $x; ?>]" id="subparticulars<?php echo $x; ?>" readonly/>
          <option value = "<?php echo $studentfees['feetype_id']; ?>"> <?php echo $studentfees['feetype_name']; ?> </option>
        </select>
      </td>

      <td class="form-group">
        <input type="text" class="form-control" name="subpaymentbalance[<?php echo $x; ?>]" id="subpaymentbalance<?php echo $x; ?>" onclick="copyBalance(<?php echo $x; ?>)" value="<?php echo $studentfees['feestudent_amount']; ?>" readonly />
      </td>

      <td class="form-group">
        <input type="text" class="form-control" name="subpaymentamount[<?php echo $x; ?>]" id="subpaymentamount<?php echo $x; ?>" onkeyup="calculateTotalAmount(); calculateRemainingBalance(<?php echo $x; ?>);" placeholder="Payment Amount" />
      </td>          
    </tr>

    <?php $arrayNumber++; } // /.foreach?>
  </tbody>
</table>

Upvotes: 1

Related Questions