Grouping

4 posts by 2 authors in: Forums > CMS Builder
Last Post: February 6, 2017   (RSS)

Hi,

I can't find a way in the code generator for a WHERE statement ... so, can you tell me how I might group stuff? I am running a food menu categorised as Starters, Mains, Desserts. I want to achieve the following grouping for all three courses which previously I would have used a WHERE statement:

COURSE (e.g. Starts, Mains or Desserts)

Item

Item

Item

The cumbersome way I am doing it, is to hard code the Course name first and then use:

<h4>Starters</h4>
<?php if ($record['course']=="Starters"): ?>
    <div class="name-wrap">
        <div class="box">
            <h6 class="box_aside"><?php echo htmlencode($record['name_of_dish']) ?></h6>
            <span class="box_right"></span>
        </div>
        <p class="brd"><?php if($record['description']):?><?php echo htmlencode($record['description']) ?><?php endif; ?></p>
    </div>
<?php endif; ?>

Thanks

Terry

By ross - February 3, 2017

Hi Terry

Thanks for posting.

Let's take a look at how to setup a "where" statement with your code.

First off, my recommendation is to actually use three different "getRecords" so you can have one list for each course.

Next, let's suppose the code generator gives you the following:

// load record from 'meals'
  list($mealsRecords) = getRecords(array(
    'tableName'   => 'meals',
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));

Here's how you'd add a "where" to that:

// load record from 'meals'
  list($starters) = getRecords(array(
    'tableName'   => 'meals',
    'where'       => "course = 'starters'", // load starters
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));

Note: I'm assuming your table name is called "meals" so many sure to update that if it's something else instead.

See the "where" in that?  

To take this to the next step, you can duplicate that block twice more (giving you a total of 3).  

Make sure to adjust the "Where" part so it loads the right course. Also, make sure to change the variable name for "$starters". You can use $mains and $desserts.

Does this all make sense?

Let me know any questions.

Thanks! 

-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

Hi Ross,

Thanks for your response. I see that while the WHERE clause is no longer part of the code generator it can still be added back as per the older version. Good news!

Looking at it some more, I thought that using the WHERE clause was not the most efficient method because in the event that I added another course then I could need that hardcode that into the template and FTP it to the server, so I used an associative array to group the courses instead and that works much better and is a one-stop shop. For reference, in the event that anyone else is looking for a similar solution, here is what I did:

First create the associative array:

          $ValMeals = array(); 

          foreach ($valentines_dayRecords as $meal) { 
            $ValCourse = $meal['course']; 

            if (!array_key_exists($ValCourse, $ValMeals)) { 
             $ValMeals[$ValCourse] = array(); 
            } 

            $ValMeals[$ValCourse][] = $meal; 
          } 

Then loop through it:

    <?php foreach ($ValMeals as $courses => $meals): ?>
    <h4><?php echo $courses; ?></h4>
    <?php foreach ($meals as $meal): ?> 
      <div class="name-wrap">
          <div class="box">
              <h6 class="box_aside"><?php echo htmlencode($meal['name_of_dish']) ?></h6>
              <span class="box_right"></span>
          </div>
          <p class="brd"><?php echo htmlencode($meal['description']) ?></p>
      </div>
        <?php endforeach ?> 
        <br><br>
      <?php endforeach ?>

The output was the following with one set of code rather than having to repeat the code for each course, if I were using the WHERE clause.

    STARTERS
    Soup of the Day (v)
    Smoked Salmon and Prawn Cocktail
    Ham Hock Terrine, Apple Relish and Toast
    Baked Camembert

    MAIN
    7oz Ribeye Steak
    Baked Chicken Breast
    Roast Monkfish
    Gnocchi

    DESSERT
    Chocolate Pot and Dipping Fruit
    Sticky Toffee Pudding with Vanilla Ice-cream
    Passion Fruit Pannacotta
    Cheeseboard

Best regards

Terry