Processing array values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • assgar
    New Member
    • Aug 2006
    • 41

    Processing array values

    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="#9bcd9 b" align="left">
    <input type="submit" name="ohip_butt on" 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_cod e);
    ?>


    </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_cod e)
    {

    $mysqli = db_connect();
    db_select($mysq li, $org_code);

    echo "<div id=\"Layer2\" style=\"positio n: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_fe e AS a, non_anaesthetis t_fee AS n
    FROM bill_on_curr_ma ster
    WHERE code = '$code'
    AND deleted = 'N'
    LIMIT 1";
    $r = mysqli_query ($mysqli, $q);
    while($row = mysqli_fetch_ar ray($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_mone y[]\">";
    $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_mone y[$i]\" size=\"9\" maxlength=\"7\" value =\"$ohip_fee\ "/>\n";
    $p_fee .= "</select>";

    echo "<tr height=\"10\">\ n";
    echo "<td width=\"9%\" bgcolor=\"#fff8 dc\" align=\"\"><spa n class=\"style15 \">
    <input type=\"checkbox \" name=\"choice[]\" value=\"A001\">
    <span class=\"style1\ " >A001</span></span></td>
    <td width=\"2%\" bgcolor=\"#eeee e0\" height=\"10\">
    <input type=\"text\" name=\"unit[]\" size=\"1\" maxlength=\"2\" value =\"$a001_unit \"/>
    </td>
    <td width=\"32%\" bgcolor=\"#eeee e0\" 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_fe e AS a, non_anaesthetis t_fee AS n
    FROM bill_on_curr_ma ster
    WHERE code = '$code'
    AND deleted = 'N'
    LIMIT 1";
    $r = mysqli_query ($mysqli, $q);
    while($row = mysqli_fetch_ar ray($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_mone y[]\">";
    $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_mone y[$i]\" size=\"9\" maxlength=\"7\" value =\"$ohip_fee\ "/>\n";
    $p_fee .= "</select>";

    echo "<td width=\"9%\" bgcolor=\"#fff8 dc\" align=\"\"><spa n class=\"style15 \">
    <input type=\"checkbox \" name=\"choice[]\" value=\"A003\">
    <span class=\"style1\ " >A003</span></span></td>
    <td width=\"2%\" bgcolor=\"#eeee e0\" height=\"10\">
    <input type=\"text\" name=\"unit[]\" size=\"1\" maxlength=\"2\" value =\"$a003_unit \"/>
    </td>
    <td width=\"32%\" bgcolor=\"#eeee e0\" 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($fe e_code))
    {
    $fee_code = array_filter($f ee_code);
    }
    else
    {
    $fee_code = array($fee_code );
    $fee_code = array_filter($f ee_code);
    }


    //filter array $fee_unit
    if(is_array($fe e_unit))
    {
    $fee_unit = array_filter($f ee_unit);
    }
    else
    {
    $fee_unit = array($fee_unit );
    $fee_unit = array_filter($f ee_unit);
    }
    //filter array $fee_money
    if(is_array($fe e_money))
    {
    $fee_money = array_filter($f ee_money);
    echo "if money";
    }
    else
    {
    $fee_money = array($fee_mone y);
    $fee_money = array_filter($f ee_money);
    echo "else money";
    }


    $indices1 = array_keys($fee _code);
    foreach($indice s1 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($m ysqli,"SELECT fee_code, description
    FROM bill_fee_code
    WHERE fee_code = '$fee_code[$index1]'
    AND deleted = 'N'
    LIMIT 1");
    while($result = mysqli_fetch_ar ray($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_fiel d($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_fie ld($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]
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    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=arr ay (
    '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:

    Comment

    Working...