By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,758 Members | 1,264 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,758 IT Pros & Developers. It's quick & easy.

Processing array values

P: 41
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.

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

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]
<?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">
<input type="submit" name="ohip_button" value="Submit"
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>
[/php]



Form display function
[php]

//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\">
</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";

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


?>

[/php]






Form processor:
[php]

<?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******/


[/php]
Jan 23 '07 #1
Share this Question
Share on Google+
1 Reply


ronverdonk
Expert 2.5K+
P: 4,258
There are so many array handling functions used in your code, that I cannot keep track of all these array conversions.

When the $_POST array is as you show, you can do a double foreach: 1 for the main array and 1 for each sub-array. I have made this little snippet that rebuilds your post array and processes it. The extracted results are stored in the variable of which the name is extracted from the index name of main array. See if this makes it any clearer for you.
[php]<?php
$post_array=array (
'unit' => Array (
0 => '',
1 => '',
2 => '',
3 => '',
4 => 3
),
'fee_money' => Array (
0 => '',
1 => '',
2 => '',
3 => '',
4 => 10.25
),
'choice' => Array (
0 => A008
)
);
// extract 1 value from each sub array
foreach ($post_array as $key => $sub_array) {
foreach ($sub_array as $index => $value)
if ($value != '')
$$key = $value;
}
echo "$unit - $fee_money - $choice";
?>[/php]
Ronald :cool:
Jan 23 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.