0

I looked around for grouped by but i didn't find anything that either made sense or worked.

im trying to make a menu with grouped headings like salads/pizza/pasta etc i got it to save to the database fine and i have it reading as an array( i think its called multidimensional[if you know what the dumped array is called properly please tell me id like to know].

here is my array dump

Array
(
    [heading] => Array
    (
        [0] => Salads
        [1] => Salads
        [2] => Pasta
    )

    [special] => Array
    (
        [0] => 
        [1] => 
        [2] => 
    )

    [item] => Array
    (
        [0] => Small Green
        [1] => Regular Caesar
        [2] => Baked Lasagna
    )

    [description] => Array
    (
        [0] => Grape tomatoes, onions, green peppers and cucumbers on the bed of crisp lettuce.
        [1] => Classic recipe with romaine lettuce and croutons
        [2] => With meat sauce, tomato vegetarian sauce or Alfredo sauce
    )

    [price] => Array
    (
        [0] => See Desc
        [1] => $5.99
        [2] => $9.69
    )

    [notes] => Array
    (
        [0] => Available in Small ($2.99), Regular ($5.99)
        [1] => 
        [2] => 
    )

    [submit_val] => Submit
)

im using this as the php to output the data(minus the mysql info)

for($i = 0; $i < count($array['heading']); $i++) {
    ?>
    <tr>    <td colspan="2" align="center" class="section"><?=$array['heading'][$i]; ?></td></tr>
    <tr>    <td colspan="2">&nbsp;</td></tr>

    <tr>    <td class="selection"><b><?=$array['item'][$i]; ?>.
    </b>
    &nbsp;<i><?=$array['description'][$i]; ?></i><br>
    <SMALL><I><?=$array['special'][$i]; ?></I></SMALL><SMALL><?=$array['notes'][$i]; ?></SMALL>
    </td>
    <td class="selection" align="right" valign="top"><?=$array['price'][$i]; ?></td>
    </tr>
    <?

}// for i loop

here is what the menu looks like now screenshot of menu

so how does one make it so the salads are in one heading and not two please, mind you i have multiple items in multiple headings(with same headings ie various pizzas etc)

Thank you in advance for any help you may provide

4

2 回答 2

1

您可以缓存最后一个标题并执行 if 语句:

<?php
$last_heading = '';
for($i = 0; $i < count($array['heading']); $i++) {
    if ($last_heading !== $array['heading'][$i]) { ?>
    <tr>    <td colspan="2" align="center" class="section"><?=$array['heading'][$i]; ?></td></tr>
    <?php $last_heading = $array['heading'][$i]; } // end if ?>
    <tr>    <td colspan="2">&nbsp;</td></tr>

    <tr>    <td class="selection"><b><?=$array['item'][$i]; ?>.
    </b>
    &nbsp;<i><?=$array['description'][$i]; ?></i><br>
    <SMALL><I><?=$array['special'][$i]; ?></I></SMALL><SMALL><?=$array['notes'][$i]; ?></SMALL>
    </td>
    <td class="selection" align="right" valign="top"><?=$array['price'][$i]; ?></td>
    </tr>
    <?

}// for i loop

这确实假设您的所有标题都将像这样排列在一起。

于 2013-08-09T19:51:22.523 回答
0

假设您通过数据库中的标题对结果进行分组,这会将同一标题的所有项目保持在一起,那么您可能会使用以下内容:

<?php    
    for($i = 0; $i < count($array['heading']); $i++) {
        if($array['heading'][$i] != $array['heading'][$i-1]){ // Compare headings
?>
    <tr>    <td colspan="2" align="center" class="section"><?=$array['heading'][$i]; ?></td></tr>
<?php 
        } //endif compare 
?>
    <tr>    <td colspan="2">&nbsp;</td></tr>
    <tr>    <td class="selection"><b><?=$array['item'][$i]; ?>.</b>
    &nbsp;<i><?=$array['description'][$i]; ?></i><br>
    <SMALL><I><?=$array['special'][$i]; ?></I></SMALL><SMALL><?=$array['notes'][$i]; ?></SMALL>
    </td>
    <td class="selection" align="right" valign="top"><?=$array['price'][$i]; ?></td>
    </tr>
<?

    }// for i loop
?>
于 2013-08-09T19:59:32.190 回答