Page 1 of 1
echo contents of two arrays in order?
Posted: Sun Mar 04, 2007 9:58 am
by me!
Posted: Sun Mar 04, 2007 10:28 am
by John Cartwright
I'm as confused as you are.. what exactly is both arrays?
Hint: print_r()
Posted: Sun Mar 04, 2007 10:49 am
by me!
LOL
$weeks, will have what date (week) some one will attend a camp program. This was selected by the user from a form.
Like "August 20-24, June 25 - 29 "
$days, Same as above but tells them what days of the week they will be attending.
Like "Monday - Friday, Tuesday & Thursday"
So I am trying to get an output like this:
August 20-24, Monday - Friday
June 25 - 29, Tuesday & Thursday
Posted: Sun Mar 04, 2007 10:50 am
by me!
This is what I get with the above code now:
August 20-24, Monday - Friday
June 25 - 29, Tuesday & Thursday
August 20-24, Monday - Friday
June 25 - 29, Tuesday & Thursday
Posted: Sun Mar 04, 2007 11:20 am
by me!
Ok, I got the desired output by using this code, but is there a better way?
Note: $week_X is the form field and empty by default.
Code: Select all
// print week selection results
echo '<b>You have selected:</b> <br>';
if (!empty($week_1)) echo ''.$weeks[week_1].', '.$days[w1].' - '.$rate[r1].'<br>';
if (!empty($week_2)) echo ''.$weeks[week_2].', '.$days[w2].' - '.$rate[r2].'<br>';
if (!empty($week_3)) echo ''.$weeks[week_3].', '.$days[w3].' - '.$rate[r3].'<br>';
if (!empty($week_4)) echo ''.$weeks[week_4].', '.$days[w4].' - '.$rate[r4].'<br>';
if (!empty($week_5)) echo ''.$weeks[week_5].', '.$days[w5].' - '.$rate[r5].'<br>';
if (!empty($week_6)) echo ''.$weeks[week_6].', '.$days[w6].' - '.$rate[r6].'<br>';
if (!empty($week_7)) echo ''.$weeks[week_7].', '.$days[w7].' - '.$rate[r7].'<br>';
if (!empty($week_8)) echo ''.$weeks[week_8].', '.$days[w8].' - '.$rate[r8].'<br>';
if (!empty($week_9)) echo ''.$weeks[week_9].', '.$days[w9].' - '.$rate[r9].'<br>';
if (!empty($week_10)) echo ''.$weeks[week_10].', '.$days[w10].' - '.$rate[r10].'<br>';
echo '<br>';
// show total amount due
echo '<br> Total: $'.array_sum($rate).'<br><br><br>';
output is:
You have selected:
June 25 - 29, Full Day, Monday - Friday - 385.00
August 20-24, Half Day, Monday, Wednesday, Friday - 58.00
Total: $443
Posted: Sun Mar 04, 2007 11:33 am
by feyd
So the elements of $weeks correspond directly with elements of $days and $rate?
Reorganize the three arrays into a single two dimensional array in the following format
Code: Select all
$weeks[] = array('week'=>'foo', 'days'=>'Sunday', 'rate'=>20.5);
Posted: Sat Mar 10, 2007 9:56 pm
by me!
Ok it took me the last 4 hours and a lot of reading bad examples to get this to work, but I think I got it now!
Code: Select all
$weeks = array();
$weeks[] = array('week'=>'Week 1', 'days'=>'Sunday', 'rate'=>20.5);
$weeks[] = array('week'=>'Week 2', 'days'=>'Sunday and Sunday', 'rate'=>45.5);
$weeks[] = array('week'=>'Week 3', 'days'=>'Monday - Friday', 'rate'=>120.5);
foreach ($weeks as $value) {
echo ''.$value['week'].' - '.$value['days'].', $'.$value['rate'].' <br />';
}
Output:
Week 1 - Sunday, $20.5
Week 2 - Sunday and Sunday, $45.5
Week 3 - Monday - Friday, $120.5
I had no problem understanding the arrays and how they worked, just was confused on how to save values to them. feyd's post did not make sense till a while into it after Jcart locked another post that he said was the same problem. Thanks guys for all them help
Posted: Sat Mar 10, 2007 10:14 pm
by John Cartwright
Glad you solved it

Posted: Sat Mar 10, 2007 10:33 pm
by me!
The only thing that is not working now it the total for all the items.
Before when I had them all in separate arrays I used array_sum($rates) to get the total amount due as far as I can tell I can't do that with this set up?

Posted: Sat Mar 10, 2007 10:48 pm
by me!
ok this works but is there something better to get the total?
Code: Select all
$weeks = array();
$total = array();
$weeks[] = array('week'=>'Week 1', 'days'=>'Sunday', 'rate'=>20.5);
$weeks[] = array('week'=>'Week 2', 'days'=>'Sunday and Sunday', 'rate'=>45.5);
$weeks[] = array('week'=>'Week 3', 'days'=>'Monday - Friday', 'rate'=>120.5);
foreach ($weeks as $value)
{
$total[] = $value['rate'];
echo ''.$value['week'].' - '.$value['days'].', $'.$value['rate'].' <br />';
}
echo '<br />';
echo array_sum($total);
Posted: Sat Mar 10, 2007 11:16 pm
by John Cartwright
Seems ok to me. I usually do these kinds of calculations on the database side.. but this is fine also.