Storing dynamic form values in Arrays for display & insert

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

    Storing dynamic form values in Arrays for display & insert

    Hi

    Developemnt on win2003 server. Final server will be linux
    Apache,Mysql and PHP is being used.

    I use 2 scripts(form and process).
    The form displays multiple dynamic rows with chechboxs, input box for units of service, description of the service and each row has its own dropdown list of unit fees that apply.
    Each dynamically created row will return 3 values fee1_choice, fee1_unit and fee1_money.

    Note The above informaton is coming from a mysql database.

    When the service is submitted the selected information is displayed to the secreen by the process.
    Note: When I get it displaying to the screen, I will work on inserting the information into a mysql database.


    Problem.

    I have identified my problem as being the arrays and I am stuck.
    For testing I tried 3 different types of loops to identify the problem with the values in arrays
    (the results are below).

    If I select the first row in the dynamically created rows the correct fee1_choice, fee1_unit and fee1_money values are passed to be displayed.

    If I select the second or any other row in the dynamically created rows the correct code_id only is passed. No fee1_unit and fee1_money are displayed


    Questions:

    1) What is the best way to for me to use arrays to collect dynamically created information with the multiple checkbox selections?

    2) should I use a two-dimentional array instead of seperate arrays for the 3 fields?

    3) When the user selects any number of checkboxes Should not only the selected item(s) be in the array?

    4) Should not the array be starting at index 0 for the while and for loop? Note: see results below.

    5) If 3 selection boxes are chosen should not the array only loop 3 times?



    Thanks in advance

    Form
    [html]
    <html>

    <!-----------------------form processor---------------------------->
    <form action="../process.php" method="post">

    <table>
    <!------------------------search button------------------------------------>
    <tr>
    <td width="99%"><in put type="submit" name="fee_butto n" value="Search" />
    </td>
    </tr>

    //php code below is placed here

    </table>
    </html>
    [/html]

    This PHP code will display 49 dynamic rows

    [php]
    <?php
    //this is placed in the form


    $data = "SELECT c.code_id, c.fee_code, c.description, m.general_fee,
    m.technical_fee , m.specialist_fe e, m.anaesthetist_ fee,
    m.non_anaesthet ist_fee
    FROM bill_ohip_fee_c ode c, $fee_master_tab le m
    WHERE c.fee_code = m.code
    AND c.section_code = '$services'
    AND premium != 'Y'
    ORDER BY c.fee_code";

    $result = mysqli_query($m ysqli,$data);
    while($row = mysqli_fetch_ar ray($result))
    {
    $code_id = $row['code_id'];
    $fee_code = $row['fee_code'];
    $description = $row['description'];
    $general_fee = $row['general_fee'];
    $technical_fee= $row['technical_fee'];
    $specialist_fee = $row['specialist_fee '];
    $anaesthetist_f ee= $row['anaesthetist_f ee'];
    $non_anaestheti st_fee= $row['non_anaestheti st_fee'];


    //format fee to 2 deciaml places
    $general = sprintf("%9.2f" ,$general_fee/100);
    $technical = sprintf("%9.2f" ,$technical_fee/100);
    $specialist = sprintf("%9.2f" ,$specialist_fe e/100);
    $anaesthetist = sprintf("%9.2f" ,$anaesthetist_ fee/100);
    $non_anaestheti st = sprintf("%9.2f" ,$non_anaesthet ist_fee/100);


    //dropdown list of fees that filter out 0.00
    $fee = "<select name=\"fee_mone y[]\">";
    $fee .= "<option value = > Select</option>";
    if($general > 0.00)
    $fee .= "<option value = $general>Gen: $general</option>";
    if($technical > 0.00)
    $fee .= "<option value = $technical>Tec: $technical</option>";
    if($specialist > 0.00)
    $fee .= "<option value = $specialist>Spe :
    $specialist</option>";
    if($anaesthetis t > 0.00)
    $fee .= "<option value = $anaesthetist>A na:
    $anaesthetist</option>";
    if($non_anaesth etist > 0.00)
    $fee .= "<option value = $non_anaestheti st>Non:
    $non_anaestheti st</option>";
    //input box is displayed if no fee values for all 5
    elseif($general == 0.00 && $technical == 0.00 && $specialist ==
    0.00 && $anaesthetist == 0.00 && $non_anaestheti st == 0.00)
    $fee .= "<input type=\"text\" name=\"fee_sele ct[]\" size=\"9\"
    maxlength=\"7\" value =\"$ohip_fee\ "/>\n";
    $fee .= "</select>";



    //looping to display dynamic rows
    echo"<tr height=\"10\">
    <td width=\"4%\" align=\"center\ ">
    <input type=\"checkbox \" name=\"fee1_cho ice[]\"
    value=\"$code_i d\"></td>
    <td width=\"7%\" ><span class=\"style20
    \"><strong>$fee _code</strong></span></td>
    <td width=\"3%\" height=\"10\">
    <input type=\"text\" name=\"fee1_uni t[]\" size=\"1\"
    maxlength=\"2\" value =\"$fee_unit\ "/>
    </td>
    <td width=\"79%\" class=\"style20 \"> $description </td>
    <td width=\"6%\" align=\"left\"> $fee </td>\n";
    echo"</tr>\n";
    }//end of while

    ?>
    [/php]


    Process to display user selection
    for loop

    [php]
    <?php
    $code_id = ($_POST['fee1_choice']); //array of code_id primary key
    $fee1_unit = ($_POST['fee1_unit']);//array with the number of units
    $fee1_money = ($_POST['fee1_money']);//array selected fee

    //using for loop to extract array values
    for($row = 0; $row < count($code_id) ; $row++)
    {
    echo $code_id[$row].", ".$fee1_uni t[$row].", ".$fee1_mon ey[$row];
    echo "<br/>";
    }
    ?>
    [/php]

    Result:
    2036, ,
    5287, ,
    2032, , 36.30


    The results should be:
    [2036 36.30
    5287 12.51
    2032 145.10


    Process to display user selection
    foreach loop

    [php]
    <?php
    //using foreach loop to extract array with service fee
    foreach($fee1_m oney as $key => $value)
    {
    echo "Foreach Key: .$key";
    echo "Foreach Service: .$value\n";
    echo "<br/>";
    }
    ?>
    [/php]

    Result:
    Foreach Key: .0Foreach Service: .
    Foreach Key: .1Foreach Service: .
    Foreach Key: .2Foreach Service: . 36.30
    Foreach Key: .3Foreach Service: . 12.51
    Foreach Key: .4Foreach Service: . 145.10
    Foreach Key: .5Foreach Service: .
    Foreach Key: .6Foreach Service: .
    Foreach Key: .7Foreach Service: .
    Foreach Key: .8Foreach Service: .
    Foreach Key: .9Foreach Service: .
    Foreach Key: .10Foreach Service: .
    etc. to............. .....
    Foreach Key: .48Foreach Service: .



    Process to display user selection
    while loop

    [php]
    <?php
    //using while loop to extract array with service fee
    while(list($key , $value) = each($fee1_mone y))
    {
    echo "While Key: .$key; While Service: .$value\n";
    echo "<br/>";
    }
    ?>
    [/php]

    Result:
    While Key: .0; While Service: .
    While Key: .1; While Service: .
    While Key: .2; While Service: . 36.30
    While Key: .3; While Service: . 12.51
    While Key: .4; While Service: . 145.10
    While Key: .5; While Service: .
    While Key: .6; While Service: .
    While Key: .7; While Service: .
    While Key: .8; While Service: .
    While Key: .9; While Service: .
    While Key: .10; While Service: .
    While Key: .11; While Service: .
    etc. to............. ......
    While Key: .48; While Service: .
  • assgar
    New Member
    • Aug 2006
    • 41

    #2
    Solution part A:
    Changing to a for loop and incrementing the array indexes using $i
    syncronize the arrays and solves on problem.

    [php]
    <?php
    $result = mysqli_query($m ysqli,$data);
    $num_rows = mysqli_num_rows ($result);
    for($i=0; $i < $num_rows; $i++)
    {
    $row = mysqli_fetch_ar ray($result);

    list($code_id, $fee1_code, $description, $general_fee,
    $technical_fee, $specialist_fee , $anaesthetist_f ee,
    $non_anaestheti st_fee) = $row;

    echo"<tr height=\"10\">
    <td width=\"4%\" bgcolor=\"#fff8 dc\" align=\"center\ ">
    <input type=\"checkbox \" name=\"fee1_cho ice[$i]\" value=\"$code_i d\"></td>
    <td width=\"7%\" bgcolor=\"#fff8 dc\" ><span class=\"style20 \"><strong>$fee 1_code</strong></span></td>
    <td width=\"3%\" bgcolor=\"#eeee e0\" height=\"10\">
    <input type=\"text\" name=\"fee1_uni t[$i]\" size=\"1\" maxlength=\"2\" value =\"$fee1_unit \"/>
    </td>
    <td width=\"79%\" bgcolor=\"#eeee e0\" class=\"style20 \"> $description </td>
    <td width=\"6%\" align=\"left\"> $s_fee</td>\n";
    echo"</tr>\n";

    ?>
    [/php]

    Solution part B:

    The code below works great in removing the unnecessary indexes with no values. Providing the same count for the 3 arrays so using a loop is now possible.

    [php]
    <?
    $code_id = (array_filter($ code_id));
    $fee1_unit = (array_filter($ fee1_unit));
    $fee1_money = (array_filter($ fee1_money));
    ?>
    [/php]

    Current Challenge

    I was previously using a for loop, the problem is if the array index does not start at 0 (zero) it throws your looping off so you can't combine count() and starting your loop at $row = 0.

    [php]
    <?
    for($row = 0; $row < count($code_id) ; $row++)
    {
    echo $code_id[$row].", ".$fee1_uni t[$row].", ".$fee1_mon ey[$row];
    echo "<br/>";
    }
    ?>
    [/php]


    The other options for looping which work are the foreach() and while() loops
    where you don't need to determine the length of the array.

    I have played with the foreach.


    [php]
    <?
    $all = array($code_id, $fee1_unit,$fee 1_money);
    foreach ($all as $key => $value)
    {
    echo "$key<br>";
    foreach($value as $key2 => $val2)
    {
    echo " $key2 => $val2<br>";
    }
    }
    ?>
    [/php]

    <b>
    Foreach() result:
    </b>
    0
    2 => 2036
    3 => 5287
    4 => 2032
    1
    2 => 2
    3 => 3
    4 => 4
    2
    2 => 36.30
    3 => 12.51
    4 => 145.10

    How do I structrue a foreach or while loop so I can sync up the arrays indexs and values for inserting as rows as array[index] like in the for loop?
    [php]
    <?
    echo $code_id[$row].", ".$fee1_uni t[$row].", ".$fee1_mon ey[$row];
    ?>
    [/php]

    Comment

    • assgar
      New Member
      • Aug 2006
      • 41

      #3
      Solution part C

      I think I have the method of accomplishing what I need.
      Do you see and down side to using this code or have another suggestion that would be best?

      [php]
      <?php>
      $code_id = (array_filter($ code_id));
      $fee1_unit = (array_filter($ fee1_unit));
      $fee1_money = (array_filter($ fee1_money));

      $indices1 = array_keys($cod e_id);
      foreach($indice s1 as $index1)
      {

      echo $code_id[$index1].", ";
      echo $fee1_unit[$index1].", ";
      echo $fee1_money[$index1], "<br/>";

      }
      ?>
      [/php]

      The results correspond with the database contents for the 4 service selected.
      Displayed are services, units and fee.


      Results
      5287, 1, 12.51
      2032, 2, 145.10
      2028, 3, 51.00
      2051, 4, 15.55

      Comment

      Working...