473,683 Members | 3,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Storing dynamic form values in Arrays for display & insert

41 New Member

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.


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


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 processor---------------------------->
<form action="../process.php" method="post">

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

//php code below is placed here


This PHP code will display 49 dynamic rows

//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 :
if($anaesthetis t > 0.00)
$fee .= "<option value = $anaesthetist>A na:
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 width=\"79%\" class=\"style20 \"> $description </td>
<td width=\"6%\" align=\"left\"> $fee </td>\n";
}//end of while


Process to display user selection
for loop

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

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

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

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

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

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: .
Oct 27 '06 #1
2 7049
41 New Member
Solution part A:
Changing to a for loop and incrementing the array indexes using $i
syncronize the arrays and solves on problem.

$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 width=\"79%\" bgcolor=\"#eeee e0\" class=\"style20 \"> $description </td>
<td width=\"6%\" align=\"left\"> $s_fee</td>\n";


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.

$code_id = (array_filter($ code_id));
$fee1_unit = (array_filter($ fee1_unit));
$fee1_money = (array_filter($ fee1_money));

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.

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

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.

$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>";

Foreach() result:
2 => 2036
3 => 5287
4 => 2032
2 => 2
3 => 3
4 => 4
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?
echo $code_id[$row].", ".$fee1_uni t[$row].", ".$fee1_mon ey[$row];
Oct 28 '06 #2
41 New Member
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?

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


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

5287, 1, 12.51
2032, 2, 145.10
2028, 3, 51.00
2051, 4, 15.55
Oct 29 '06 #3

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

Similar topics

by: Dave Karmens | last post by:
If I have say 10 fixed variables, how can I set their values = to that of a form that is built dynamically? column1 column2 email = formvalue(0) fname = formvalue(1) lname = formvalue(2) etc..
by: Nick | last post by:
Loop to create an array from a dynamic form. I'm having trouble with an application, and I'll try to explain it as clearly as possible: 1. I have a form with two fields, say Apples and Oranges. The user selects from a drop down menu, between 1-10 of each and clicks submit. The resulting page will display a NEW form, with rows and a list of fields for the amount of each items selected.
by: Randell D. | last post by:
HELP! I am determined to stick with this... I'm getting there... for those who haven't read my earlier posts, I'm createing what should be a simple function that I can call to check that required fields in a form have values. I'm writing the values to the client using document.write only so that I can confirm that the values are there to be played with - this is not the final result so please (unless it's leading to the script failure)...
by: Venus | last post by:
Hello, Thanks for your reply. I understand that a control can be created dynamically in several ways: 1) using StringBuilder 2) using Controls.Add 3) using ASP PlaceHolder But this is just for the controls and not for the form itself. What I am trying to achieve is to create an entire form (including controls)
by: ak1dnar | last post by:
Hi, I have created a web application that enables to Delete Items in a product table using check boxes. I Need to Display each and every product in <TR> and with the product there is a corresponding check box to delete it, use can select the check box and then he will press the Update Button in the Form to submit for processing. //Here I will get the Values from MySQL Table //This Area is Inside a HTML form with Update button and On...
by: Jerim79 | last post by:
I need to create a form that takes a number that the user enters, and duplicates a question the number of times the user entered. For instance, if the customer enters 5 on the first page, when they press next the form generates "How old are you?" 5 times on the page. The customer will answer all 5 questions then press next. Finally, all the local variables get dynamically created and written to a database. I have already taken care of...
by: viki1967 | last post by:
Hi there. I have a problem in to recover the values of a dynamic form. The form is this: strSQL = "SELECT * " strSQL = strSQL & " FROM " strSQL = strSQL & " TBL "
by: Dilruba | last post by:
asp, vbscript, Ms Access I am using vbscript to insert & update ms accees. Insert operation is working , but update operation is not working. I have used the following codes: connectionString = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:\progam files\mail.mdb"
by: bmallett | last post by:
First off, i would like to thank everyone for any and all help with this. That being said, I am having a problem retrieving/posting my dynamic form data. I have a form that has multiple options within options. I have everything being dynamically named from the previously dynamically named element. (I hope this makes sense.) I am not able to retrieve any of the dynamically created values. I can view them on the source page but can't pull them...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.