Forum Moderators: coopster

Message Too Old, No Replies

I am lost in the world of arrays

         

Smad

12:04 pm on Mar 29, 2006 (gmt 0)

10+ Year Member



Hi all

Please can someone help me get this working:

Basically my form asks how many adults you require a cost for:
so example they put into the form 3 and update it. 3 sets of form fields will appear. atm looking like this:

<form action="<?php echo $PHP_SELF;?>" method="post" name="generator" id="generator">

<fieldset>
<legend>Adults</legend><p>To find out the family monthly payment please enter the number of adults <input name="num" type="text" size="3" /> <input name="update" type="submit" id="update" value="Update" /></p>

<?
if ($num){?>

<h3>Annual requirements</h3>
<? for($i=0;$i<$num;$i++){?>
<table width="500" border="1">
<tr>
<td>name</td>
<td>examinations</td>
<td>hygiene</td>
</tr>
<tr>
<td><input name="name[]" type="text" /></td>
<td><select name="exam[]">
<option>select </option>
<option value="5.50">1 </option>
<option value="9.50">2 </option>
</select></td>
<td><select name="hygiene[]">
<option selected="selected">select </option>
<option value="0.00">0 </option>
<option value="2.50">1 </option>
<option value="5.00">2 </option>
<option value="7.50">3 </option>
<option value="10.00">4 </option>
</select></td>
</tr>
</table>
<? }
$num = $num + 1;?>
<br />
<input name="Calculate" type="submit" value="Calculate" /> <? }?>
</fieldset>
</form>

Now my problem is i want it to then give feed back to the visitor like like so:

$num = $_POST['num'];

function tidy ($form_string) {
$form_string = trim(stripslashes($form_string));
return $form_string;
}

if (isset($_POST['name']) && $_POST['name']!='')
{

foreach($_POST['name'] as $key => $line)
{
$member = tidy($line);
if ($member) {
print("The cost for your membership per month " . $member . " is £" . $cost . "<br />");
} }
}

BUT i can not work out how to get it to loop through the form entries and then display. each name with the total cost of an exam and hygiene visit in one line and a cost summary at the end.

the idea is that it will generate a monthly cost for a family based on what they require and display this as a total with breakdowns.

really appreciate some help here as everytime i read an article about it is seems to misdirect me.

TIA

scriptmasterdel

12:30 pm on Mar 29, 2006 (gmt 0)

10+ Year Member



<?
foreach($_POST['name'] as $line => $key)
{
$member = tidy($key);
$examcost = ($_REQUEST['exam'][$line]);
$hygienecost = ($_REQUEST['hygiene'][$line]);
$cost = $examcost+$hygienecost;
if ($member)
{
print("<br>The cost for your membership per month " .$member." is £" . $cost . "<br />");
}
}
?>

Smad

12:36 pm on Mar 29, 2006 (gmt 0)

10+ Year Member



thank you :)

scriptmasterdel

12:40 pm on Mar 29, 2006 (gmt 0)

10+ Year Member



No Problem!

Del

Smad

12:49 pm on Mar 29, 2006 (gmt 0)

10+ Year Member



sorry to be a pain, but the $cost in each loop can i store this and give a total figure at the end?if not no worries.

Smad

scriptmasterdel

1:15 pm on Mar 29, 2006 (gmt 0)

10+ Year Member



Apologies .... i should have ested the code

Just replace $cost with number_format($cost,2) to add a decimal place ;)

What do you mean by store?

> If you mean so that this information can be used throughout the site why not set them to session variables?

> If you wish to carry the variables to another form then just set the requested variables into some hidden input tags

> If you don't mean this then please explain what you want and i will help.

Cheers,
Del

Smad

1:31 pm on Mar 29, 2006 (gmt 0)

10+ Year Member



basically if 3 family members enter their requirements i would like a total figure for all 3 at the end.

also if i store this info as sessions, for example bob wants 2 exam and 1 hygiene if bob is first entered is he name[1] and exam[1] and hygeine[1] in the array? just trying to understand how to get to the data.

scriptmasterdel

1:42 pm on Mar 29, 2006 (gmt 0)

10+ Year Member



To Count the total of an array use array_sum ...

[code]
if (isset($_POST['name']) && $_POST['name']!='')
{
foreach($_POST['name'] as $line => $key)
{
$member = tidy($key);
$examcost = $_REQUEST['exam'][$line];
$hygienecost = $_REQUEST['hygiene'][$line];
$cost = $examcost+$hygienecost;

$countExam = array_sum($_REQUEST['exam']);
$countHygiene = array_sum($_REQUEST['hygiene']);
$sum = number_format($countExam+$countHygiene,2);

if ($member)
{
print("<br><br><br>The cost for your membership per month " .$member." is £" . number_format($cost,2) . "<br />");
}
}
echo $sum;
}
[code]

Smad

1:55 pm on Mar 29, 2006 (gmt 0)

10+ Year Member



perfect thank you again

Smad

scriptmasterdel

2:02 pm on Mar 29, 2006 (gmt 0)

10+ Year Member



about the sessions .....

You will need to put this in your foreach loop.

$_SESSION['name'][$line] = $member;
$_SESSION['hygiene'][$line] = $_REQUEST['hygiene'][$line];
$_SESSION['exam'][$line] = $_REQUEST['exam'][$line];

Once the variable as been set to a session variable then you can use it whenever you want untill the session is destory and/or the browser is closed.

Don't forget session_start(); at the top of your document!

Glad to be of Service,
Del