Hi
I need help solving a porblem.
I have a form that displays a checkbox, service code, description
and dropdown with fees on each row.
The fee_money and unit array only returns a selected value from the
first item (checkbox).
When I used the line below in the processor, it displayed the
correct selection.

echo '<pre>',print_r ($_POST, TRUE), '</pre>';//tool remove

Results:
array
(
[unit] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] => 3

)
[fee_money] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] => 10.25

)
[choice] => Array
(
[0] => A008
)
)

It looks like the processor is getting the selected data.
I think the problem might be the foreach loop.
Form:

<?php
  include("common_display.php");
?>
      <div>
 <table>
 <tr>
 <td height="8"></td>
 <td height="8"></td>
 <td height="8"></td>
 <td height="8"></td>
 <td height="8"></td>
 <td height="8"></td>
 </tr>
 <tr>
 </table>
 <table width="100%">
 <tr width=100%><td width="47%" bgcolor="#9bcd9b" align="left">
   <span class="style18 style21">Common Codes Fees</span></td>
   <td colspan="5" align="right" bgcolor="#9bcd9b">
    <input type="submit" name="test_button" value="Cancel"
             style=" color: #ff6600;font-weight:bold; margin-right: 5;"/>
           <input type="submit" name="ohip_button" value="Submit"
             style="color: #ff6600;font-weight:bold; margin-right: 5;"/>
          <input type="submit" name="ohip_button" value="New"
             style="color: #ff6600;font-weight:bold; margin-right: 5;"/></td>
   </tr>
 </table>
      </div>
   
  
   <?php
     //function to display common codes fees and description 
     common($org_code);
   ?>
   
</form>
</body>
</html>

Form display function

//retrive fees from DB for drop down and list the common codes and description
function common($org_code)
 {
       $mysqli = db_connect();
        db_select($mysqli, $org_code);
    echo "<div id=\"Layer2\" style=\"position:absolute; width:100%; height:800px; z-index:1; left: 10px; top: 265px;\">\n";
     
     //<!------------------------column tiltes-------------------------->
     echo "<table width=\"100%\"  border=\"0\" >\n";
     echo "<tr height=\"10\">
          <td width=\"9%\" align=\"right\"><span class=\"style1\">Code</span></td>
          <td align=\"center\"><span class=\"style1\">U</span></td>
          <td align=\"center\"><span class=\"style1\">Description</span></td>
          <td width=\"6%\" align=\"center\"><span class=\"style1\">Fee</span></td>
          <td width=\"9%\" align=\"right\"><span class=\"style1\">Code</span></td>
          <td align=\"center\"><span class=\"style1\">U</span></td>
          <td align=\"center\"><span class=\"style1\">Description</span></td>
          <td width=\"6%\" align=\"center\"><span class=\"style1\">Fee</span></td>
       </tr>\n";
  echo "<tr height=\"10\">\n";
        //<!--------------------a001------------------->
  $code = 'A001';
  $q = "SELECT general_fee AS g, technical_fee AS t, specialist_fee AS s,
                  anaesthetist_fee AS a, non_anaesthetist_fee AS n
      FROM bill_on_curr_master
      WHERE code = '$code'
        AND deleted = 'N'
              LIMIT 1";
     $r = mysqli_query ($mysqli, $q);
     while($row = mysqli_fetch_array($r))
       {
            list($g, $t, $s, $a, $n) = $row;
        //format fee to 2 deciaml places
        $g = sprintf("%9.2f",$g/100);
        $t = sprintf("%9.2f",$t/100);
        $s = sprintf("%9.2f",$s/100);
        $a = sprintf("%9.2f",$a/100);
        $n = sprintf("%9.2f",$n/100);
           
            //dropdown list of fees that filter out 0.00
            $p_fee = "<select name=\"fee_money[]\">";
           $p_fee .= "<option value =\"\" selected> Select</option>";
              if($g > 0.00)
                 $p_fee .= "<option value = $g>G: $g</option>";
              if($t > 0.00)
                 $p_fee .= "<option value = $t>T: $t</option>";
             if($s > 0.00)
                $p_fee .= "<option value = $s>S:  $s</option>";
          if($a > 0.00)
                $p_fee .= "<option value = $a>A:  $a</option>";
          if($n > 0.00)
                $p_fee .= "<option value = $n>N:  $n</option>";
            //input box is displayed if no fee values for all 5
          elseif($g == 0.00 && $t == 0.00 && $s == 0.00 && $a == 0.00 && $n == 0.00)
                   $p_fee = "<input type=\"text\" name=\"fee_money[$i]\" size=\"9\" maxlength=\"7\" value =\"$ohip_fee\"/>\n";
      $p_fee .= "</select>";
    echo "<tr height=\"10\">\n";
     echo "<td width=\"9%\" bgcolor=\"#fff8dc\" align=\"\"><span class=\"style15\">
        <input type=\"checkbox\" name=\"choice[]\" value=\"A001\">
              <span class=\"style1\" >A001</span></span></td>
            <td width=\"2%\" bgcolor=\"#eeeee0\" height=\"10\">
        <input type=\"text\" name=\"unit[]\" size=\"1\" maxlength=\"2\" value =\"$a001_unit\"/>
      </td>
            <td width=\"32%\" bgcolor=\"#eeeee0\" class=\"style11\">Minor assessment</td>
            <td width=\"6%\" >$p_fee</td>\n";
        }
  //  <!---------------------a003------------------->
  $code = 'A003';
  $q = "SELECT general_fee AS g, technical_fee AS t, specialist_fee AS s,
                  anaesthetist_fee AS a, non_anaesthetist_fee AS n
      FROM bill_on_curr_master
      WHERE code = '$code'
        AND deleted = 'N'
              LIMIT 1";
     $r = mysqli_query ($mysqli, $q);
     while($row = mysqli_fetch_array($r))
       {
            list($g, $t, $s, $a, $n) = $row;
        //format fee to 2 deciaml places
        $g = sprintf("%9.2f",$g/100);
        $t = sprintf("%9.2f",$t/100);
        $s = sprintf("%9.2f",$s/100);
        $a = sprintf("%9.2f",$a/100);
        $n = sprintf("%9.2f",$n/100);
         }
            //dropdown list of fees that filter out 0.00
            $p_fee = "<select name=\"fee_money[]\">";
           $p_fee .= "<option value =\"\" selected> Select</option>";
              if($g > 0.00)
                 $p_fee .= "<option value = $g>G: $g</option>";
              if($t > 0.00)
                 $p_fee .= "<option value = $t>T: $t</option>";
             if($s > 0.00)
                $p_fee .= "<option value = $s>S:  $s</option>";
          if($a > 0.00)
                $p_fee .= "<option value = $a>A:  $a</option>";
          if($n > 0.00)
                $p_fee .= "<option value = $n>N:  $n</option>";
            //input box is displayed if no fee values for all 5
          elseif($g == 0.00 && $t == 0.00 && $s == 0.00 && $a == 0.00 && $n == 0.00)
                   $p_fee = "<input type=\"text\" name=\"fee_money[$i]\" size=\"9\" maxlength=\"7\" value =\"$ohip_fee\"/>\n";
      $p_fee .= "</select>";
           echo "<td width=\"9%\" bgcolor=\"#fff8dc\" align=\"\"><span class=\"style15\">
        <input type=\"checkbox\" name=\"choice[]\" value=\"A003\">
              <span class=\"style1\" >A003</span></span></td>
            <td width=\"2%\" bgcolor=\"#eeeee0\" height=\"10\">
       <input type=\"text\" name=\"unit[]\" size=\"1\" maxlength=\"2\" value =\"$a003_unit\"/>
      </td>
            <td width=\"32%\" bgcolor=\"#eeeee0\" class=\"style11\">General assessment</td>
              <td width=\"6%\" >$p_fee</td>\n";
           echo "</tr>\n";
 

  //<!---------------- a004 ------------------->
$code = 'A004';
$q = "SELECT general_fee AS g, technical_fee AS t, specialist_fee AS s,
              anaesthetist_fee AS a, non_anaesthetist_fee AS n
    FROM bill_on_curr_master
    WHERE code = '$code'
      AND deleted = 'N'
      LIMIT 1";
  $r = mysqli_query ($mysqli, $q);
   while($row = mysqli_fetch_array($r))
       {
            list($g, $t, $s, $a, $n) = $row;
        //format fee to 2 deciaml places
        $g = sprintf("%9.2f",$g/100);
        $t = sprintf("%9.2f",$t/100);
        $s = sprintf("%9.2f",$s/100);
        $a = sprintf("%9.2f",$a/100);
        $n = sprintf("%9.2f",$n/100);
            //dropdown list of fees that filter out 0.00
            $p_fee = "<select name=\"fee_money[]\">";
           $p_fee .= "<option value =\"\" selected> Select</option>";
              if($g > 0.00)
                 $p_fee .= "<option value = $g>G: $g</option>";
              if($t > 0.00)
                 $p_fee .= "<option value = $t>T: $t</option>";
             if($s > 0.00)
                $p_fee .= "<option value = $s>S:  $s</option>";
          if($a > 0.00)
                $p_fee .= "<option value = $a>A:  $a</option>";
          if($n > 0.00)
                $p_fee .= "<option value = $n>N:  $n</option>";
            //input box is displayed if no fee values for all 5
          elseif($g == 0.00 && $t == 0.00 && $s == 0.00 && $a == 0.00 && $n == 0.00)
                   $p_fee = "<input type=\"text\" name=\"fee_money[]\" size=\"9\" maxlength=\"7\" value =\"$a004_fee\"/>\n";
      $p_fee .= "</select>";
           echo "<tr height=\"10\">\n";
           echo "<td width=\"9%\" bgcolor=\"#fff8dc\" align=\"\"><span class=\"style15\">
        <input type=\"checkbox\" name=\"choice[]\" value=\"A004\">
              <span class=\"style1\" >A004</span></span></td>
            <td width=\"2%\" bgcolor=\"#fff8dc\" height=\"10\">
       <input type=\"text\" name=\"unit[]\" size=\"1\" maxlength=\"2\" value =\"$a004_unit\"/>
      </td>
            <td width=\"32%\" bgcolor=\"#ebeae0\" class=\"style11\">General re-assessment</td>
              <td width=\"6%\" >$p_fee</td>\n";
     }
 
 // <!---------------- a007  ------------------->
$code = 'A007';
$q = "SELECT general_fee AS g, technical_fee AS t, specialist_fee AS s,
              anaesthetist_fee AS a, non_anaesthetist_fee AS n
    FROM bill_on_curr_master
    WHERE code = '$code'
      AND deleted = 'N'
      LIMIT 1";
  $r = mysqli_query ($mysqli, $q);
   while($row = mysqli_fetch_array($r))
       {
            list($g, $t, $s, $a, $n) = $row;
        //format fee to 2 deciaml places
        $g = sprintf("%9.2f",$g/100);
        $t = sprintf("%9.2f",$t/100);
        $s = sprintf("%9.2f",$s/100);
        $a = sprintf("%9.2f",$a/100);
        $n = sprintf("%9.2f",$n/100);
            //dropdown list of fees that filter out 0.00
            $p_fee = "<select name=\"fee_money[]\">";
           $p_fee .= "<option value =\"\" selected> Select</option>";
              if($g > 0.00)
                 $p_fee .= "<option value = $g>G: $g</option>";
              if($t > 0.00)
                 $p_fee .= "<option value = $t>T: $t</option>";
             if($s > 0.00)
                $p_fee .= "<option value = $s>S:  $s</option>";
          if($a > 0.00)
                $p_fee .= "<option value = $a>A:  $a</option>";
          if($n > 0.00)
                $p_fee .= "<option value = $n>N:  $n</option>";
            //input box is displayed if no fee values for all 5
          elseif($g == 0.00 && $t == 0.00 && $s == 0.00 && $a == 0.00 && $n == 0.00)
                   $p_fee = "<input type=\"text\" name=\"fee_money[]\" size=\"9\" maxlength=\"7\" value =\"$a007_fee\"/>\n";
      $p_fee .= "</select>";
           echo "<td width=\"9%\" bgcolor=\"#fff8dc\" align=\"\"><span class=\"style15\">
        <input type=\"checkbox\" name=\"choice[]\" value=\"A007\">
              <span class=\"style1\" >A007</span></span></td>
            <td width=\"2%\" bgcolor=\"#ebeae0\" height=\"10\">
       <input type=\"text\" name=\"unit[]\" size=\"1\" maxlength=\"2\" value =\"$a007_unit\"/>
      </td>
            <td width=\"32%\" bgcolor=\"#ebeae0\" class=\"style11\">Intermediate assessment or well baby care</td>
              <td width=\"6%\" >$p_fee</td>\n";
           echo "</tr>\n";
       }
   echo"</table>\n";
   echo "<table width =\"100%\">
        <tr><td colspan=\"6\" height=\"5\"><hr id=\"bill-footer\"></hr></td></tr>
       </table>\n";
  echo "</div>\n";

  $mysqli->close();//close connection to db
  
  $all_array = array(choice, unit, fee_money);
  return $all_array;
}
  //unpack array
  list($choice, $unit, $fee_money);

?>

Form processor:

<?php
  /*** arrays passed from the form with selected service variables ***/
 $fee_code = $_POST['choice']; //array of code_id primary key
 $fee_unit = $_POST['unit'];//array with the number of units
 $fee_money = $_POST['fee_money'];//array selected fee

   //filter array $description
 if(is_array($fee_code))
     {
   $fee_code = array_filter($fee_code);
  }
  else
   {
     $fee_code = array($fee_code);
      $fee_code = array_filter($fee_code);
    }
   
   
    //filter array $fee_unit
 if(is_array($fee_unit))
     {
   $fee_unit = array_filter($fee_unit);
  }
  else
   {
     $fee_unit = array($fee_unit);
      $fee_unit = array_filter($fee_unit);
    }
   //filter array $fee_money
 if(is_array($fee_money))
     {
   $fee_money = array_filter($fee_money);
   echo "if money";
  }
  else
   {
       $fee_money = array($fee_money);
       $fee_money = array_filter($fee_money);
       echo "else money";
    }
 
    $indices1 = array_keys($fee_code);
    foreach($indices1 as $index1)
        {
         //individual value validation from 3 arrays
               $fee_code[$index1];//fee code
               $fee_unit[$index1];//units of time
               $fee_money[$index1];//service fee
   
    
      //get fee_code and description for related code_id
      $query = mysqli_query($mysqli,"SELECT fee_code, description
                                     FROM bill_fee_code
            WHERE fee_code = '$fee_code[$index1]'
       AND deleted = 'N'
       LIMIT 1");
      while($result = mysqli_fetch_array($query))
          list($fee_code1, $description)= $result; //listing output
     
            //unit validated strip_tag and trim is returned for use below
       $field_unit = $fee_unit[$index1];//assign field to array for function
        check_unit_field($field_unit);//funtion to validate user entered numbers or message sent
     //money validated strip_tag and trim is returned for use below
       $field_money = $fee_money[$index1];//assign field to array for function
        check_money_field($field_money);//function validate the user entered characters are number or message sent
        required_field($field_money);//function check required field is not empty
            
            /****** Datatbase insert goes here******/

Since the unchecked checkboxes are not submitted to php, you loose the index of the selected row. So for example if you select a single row, the index will always be 0, no matter which row is that.

You need to specify the index explicitly:

<input type=\"checkbox\" name=\"choice[$i]\" value=\"A001\">

Be sure to increment $i:

$i=0;
while($result = mysqli_fetch_array($query)){
  //...
  $i++
}

$i is shared among all loops, so you need to clear $i before the first one only.

Speaking of the loops, wouldn't it be better to run just one query instead of 7?

$q = "SELECT general_fee AS g, technical_fee AS t, specialist_fee AS s,
                  anaesthetist_fee AS a, non_anaesthetist_fee AS n
      FROM bill_on_curr_master
      WHERE code IN ('A001','A002','A003','A004','A005','A006','A007')
        AND deleted = 'N'
              LIMIT 1";
This article has been dead for over six months. Start a new discussion instead.