Re: Embedding foreach loops
| From: | Jonathan Tapicer | Date: | Mon, 10 Aug 2009 21:51:32 +0000 |
| Subject: | Re: Embedding foreach loops | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-296570@lists.php.net to get a copy of this message | ||
On Mon, Aug 10, 2009 at 6:44 PM, Allen McCabe<allenmccabe@gmail.com> wrote:
> Gmail automatically sent my last email, apologies.
>
> I am creating an order form for tickets for a list of performances at a
> performing arts center.
>
> Currently, the form is on paper, and is set up as follows:
> -Title - date - time - price - soldout - quantity - total($)
> -Nutcracker - Tues 10/13 - 9am - $4.00 - yes/no - ________ - ______
> -Nutcracker - Tues 10/13 - 11am - $4.00 - yes/no - ________ - ______
> -Mayhem P.. - Thur 01/21 - 9am - $4.00 - yes/no - ________ - ______
> -Mayhem P.. - Thur 01/21 - 11am - $4.00 - yes/no - ________ - ______
> -Max and... - Tues 04/21 - 9am - $4.00 - yes/no - ________ - ______
>
> A given show may have between 1 and 4 performances, and I figured the best
> way to approach this was to consider each show time for each show as an
> entity. There are 19 unique titles, but given different showtimes, it
> becomes 38 'shows'.
>
> I have the shows in an array ($shows), and the details for each show in its
> own array (eg. $show_01) embedded into the show array.
>
> I need to generate a row for each show (so 38 rows), and assign quantity and
> total input fields a unique name and id.
>
> I can't seem to get my foreach loops to work, will PHP parse embedded loops?
>
> Here is an example of my embedded arrays:
>
> [code=shows.php]
>
> $shows = array();
>
> $shows['show_01'] = $show_01;
> $show_01 = array();
> $show_01['title'] = 'Van Cliburn Gold Medal Winner';
> $show_01['date'] = 'Tues. 10/13/2009';
> $show_01['time'] = '11am';
> $show_01['price'] = 4.00;
> $show_01['soldout'] = 0; //IF THE SHOW SELLS OUT, CHANGE "0" to
> "1"
> (without quotations).
>
> $shows['show_02'] = $show_02;
> $show_02 = array();
> $show_02['title'] = 'Jack and the Beanstalk';
> $show_02['date'] = 'Fri. 10/23/2009';
> $show_02['time'] = '11am';
> $show_02['price'] = 4.00;
> $show_02['soldout'] = 0; //IF THE SHOW SELLS OUT, CHANGE "0" to
> "1"
> (without quotations).
>
> [/code]
>
> And here are the foreach loops I'm trying to build the rows with:
>
> [code=order.php]
>
> <?php
> foreach ($shows as $key => $value) {
> foreach ($value as $key2 => $value2) {
> print ' <td bgcolor="#DDDDDD">'. $value2
> .'</td>';
> }
> print '<tr><td>';
> print ' <td colspan="3" bgcolor="#DDDDDD"><input
> name="'.$value.'_qty" type="text"
> id="'.$value.'_qty" size="5" /></td>';
> print ' <th bgcolor="#DDDDDD"><span
> class="style6">$</span>';
> print ' <input name="'.$value.'_total"
> type="text"
> id="'.$value.'_total" size="15" maxlength="6"
> /></th>';
> print ' <th bgcolor="#DDDDDD"
> class="instruct">yes/no</th>';
> print '</tr>';
> }
> ?>
>
> [/code]
>
> In case you were wondering why I embedded the foreach statement, I need each
> array (eg. $show_22) to display in a row, and I need PHP to build as many
> rows are there are shows.
>
> Is this something I need to have in a database to work?
>
> Thanks!
>
Embedded loops are OK, actually I don't know a language where you can't do it :)
I think that your problem is the $shows array creation, you do this:
> $shows = array();
>
> $shows['show_01'] = $show_01;
> $show_01 = array();
Note that you are assigning $show_01 to a key in $shows before
creating $show_01, you should first create and fill $show_01 and then
assign it to a key in $shows.
Hope that helps.
Jonathan