1

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.

6
  • Where is $data in your sample code? Also, what's a sample of the $data? Commented Aug 14, 2017 at 14:47
  • Here is an example for the values of particulars <?php foreach ($particularsData as $particulars) { <input type="text" value="<?php echo $particulars['feetype_id']; ?>"> Commented Aug 14, 2017 at 14:56
  • I'll edit it so you would understand more clearly Commented Aug 14, 2017 at 14:59
  • 1
    Is your $particularsData array ordered with no missing values? You could do $paricularsData[$arrayNumber]['feetype_id']; or $paricularsData[$x]['feetype_id'];. If not, then instead of looping $x, use the key as your $x increments -> foreach($particularsData as $x => $particulars) Commented Aug 14, 2017 at 15:03
  • 1
    @Sean It already worked! Thanks, Ill post the correct answer later, as I will input the values. Commented Aug 14, 2017 at 15:12

2 Answers 2

1

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

Comments

0

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>

`

2 Comments

This not answer my question as it will not insert the values to the corresponding input boxes. I already answer the question with the correct one.
OK...having the real data and understanding the structure arrays in it, it's easier to do the looping,.. good job to you

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.