Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2009
    Posts
    46
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Adding headers within loops

    I am currently trying to construct a menu system. You can view this at

    www.actioncomputing.co.uk/test/index.php please choose the thai option as that has entrys.

    I need to have a header for each mealType but no matter what I do I cant get it to appear ONLY before the first entry of a meal type. such as

    STARTERS

    Crispy Mini Spring Rolls Very tasty crispy Mini Spring Rolls 1 Thai Starter £4.60

    --------------------------------------------------------------------------------

    a starter a starter a starter a starter 1 Thai Starter £3.60

    --------------------------------------------------------------------------------

    Chicken Satay Finest Chicken Satay 1 Thai Starter £5.70

    --------------------------------------------------------------------------------
    MAINS

    b main b main b main b main 2 Thai Main £2.60

    --------------------------------------------------------------------------------
    Desserts

    Thai green chicken Very Nice Thai green chicken 3 Thai Dessert £7.40


    I have added the key and the mealType at the end of the echo just to help see whats going on.

    this is my code
    [CODE]

    <?php

    $passedMenuType = (@$_GET['menuType']);
    $query = "SELECT * FROM meal LEFT JOIN mealType ON meal.mealType=mealType.mealType Order by mealTypeOrder";

    $result5 = mysqli_query($cxn,$query)
    or die ("Couldn't execute query.");

    echo "<h1>$passedMenuType menu</h1>";

    echo "<table cellpadding='5' border='0' cellspacing='15' width='580px'> <tr> <td colspan='3'><hr /></td></tr>";

    while($row = mysqli_fetch_assoc($result5))
    {
    extract($row);
    $f_mealPrice = number_format($mealPrice,2);

    if($menuType == $passedMenuType) {


    echo "<tr>";
    echo "
    <td>$mealName</td>
    <td>$mealDescription</td>
    <td>$mealTypeOrder</td>
    <td>$menuType</td>
    <td>$mealType</td>
    <td>£$f_mealPrice</td></tr>" ;
    echo"<tr> <td colspan='3'><hr /></td></tr>";

    }
    }
    echo"</table>"; ?>
    [ICODE]

    Can anyone suggest how I can create these headers within this loop?

  • #2
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    You can always use a variable that stores the last meal type used. Then you just check to see if the meal type from the DB is the same as the last meal type variable. If it isn't, print the header; otherwise, don't print it. This of course relies on the fact that all the meal types are grouped together, meaning you've ORDERed on meal type.

    For example:

    PHP Code:
    if($mealTypeOrder != $lastMealTypeOrder)
    {
        echo 
    "<h2>" $mealType "</h2>";
        
    $lastMealTypeOrder $mealTypeOrder;

    Note: Be sure you initialize the $mealTypeOrder variable before the loop and with a value you wouldn't find in
    $mealTypeOrder, yet is of the same type. This means if $mealTypeOrder holds integers, initialize it with an integer that you would never get in the DB, i.e. -1 if all mealTypeOrders are 0 - 5.

    -Shane
    Last edited by TheShaner; 05-28-2010 at 10:43 PM.

  • #3
    New Coder
    Join Date
    Nov 2009
    Posts
    46
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by TheShaner View Post
    You can always use a variable that stores the last meal type used. Then you just check to see if the meal type from the DB is the same as the last meal type variable. If it isn't, print the header; otherwise, don't print it. This of course relies on the fact that all the meal types are grouped together, meaning you've ORDERed on meal type.

    For example:

    PHP Code:
    if($mealTypeOrder != $lastMealTypeOrder)
    {
        echo 
    "<h2>" $mealType "</h2>";
        
    $lastMealTypeOrder $mealTypeOrder;

    Note: Be sure you initialize the $mealTypeOrder variable before the loop and with a value you wouldn't find in
    $mealTypeOrder, yet is of the same type. This means if $mealTypeOrder holds integers, initialize it with an integer that you would never get in the DB, i.e. -1 if all mealTypeOrders are 0 - 5.

    -Shane
    Hi Shaner,

    I tried this but it echo all the headers before any of the meals as you can see if u re visit my site, can you implement it in my code from above please and I will check that

  • #4
    New Coder
    Join Date
    Nov 2009
    Posts
    46
    Thanks
    5
    Thanked 0 Times in 0 Posts
    all sorted thanks for your help


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •